Xolani
asked on
Reading text file in Java
I want to read a text file from the FileDialog to the textarea but my read is generating error will u please give me the one which is using readline:
my read look as follows:
__________________________ ______
public void read(String fl) throws IOException
{
FileInputStream fis = new FileInputStream(fl);
BufferedInputStream bis = new BufferedInputStream(fis);
File F = new File(fl);
int r;
byte [] buf = new byte[64000];
try
{
while((r = bis.read(buf))!=-1);
r = bis.read(buf);
}
catch(Exception ex)
{
JOptionPane.showMessageDia log(null,e x.toString () + String.valueOf("Read Error"));
}
ta.setText(new String(buf,0));
fis.close();
bis.close();
throw new IOException();
}
__________________________ __________
my read look as follows:
__________________________
public void read(String fl) throws IOException
{
FileInputStream fis = new FileInputStream(fl);
BufferedInputStream bis = new BufferedInputStream(fis);
File F = new File(fl);
int r;
byte [] buf = new byte[64000];
try
{
while((r = bis.read(buf))!=-1);
r = bis.read(buf);
}
catch(Exception ex)
{
JOptionPane.showMessageDia
}
ta.setText(new String(buf,0));
fis.close();
bis.close();
throw new IOException();
}
__________________________
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I have also learn how to use JFileChooser!!!
I do not understand how your points system works but your answer is good!!
I do not understand how your points system works but your answer is good!!
jim, if the file is very large and the system is going to use the swap then your solution will give less performance than reading repeatly in an proper buffer; like the sorting or pattern matching algorithms there are best perfomance in some situations with one algorithm and best perfomance in other situations with other algorithms.
If the file are usualy short in size the your solution is better, but if the file are usualy large I think to repeatly read in a smaller buffer is a better solution. Of course there are other problems, e.g. when you load all large content of the file in JTextArea component but this is another discussion ;)
regards
ovidiu
If the file are usualy short in size the your solution is better, but if the file are usualy large I think to repeatly read in a smaller buffer is a better solution. Of course there are other problems, e.g. when you load all large content of the file in JTextArea component but this is another discussion ;)
regards
ovidiu
try {
File file = new File(name);
FileInputStream in = new FileInputStream(file);
byte[] buf = new byte[file.length()];
in.read(buf);
in.close();
} catch (Exception e) {
// or be more explicit if you want
}
String contents = new String(buf);
Here you create a byte[] sized as large as the file and read the entire file into it. Since your byte[] serves as the buffer, I don't think a BufferedInputStream will help performance at all. In fact, it would have a potential negative effect because of moving data around between internal buffers and your buffer.
Best regards,
Jim Cakalic