Solved

Reading text file in Java

Posted on 2000-05-19
4
229 Views
Last Modified: 2010-04-01
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.showMessageDialog(null,ex.toString() + String.valueOf("Read Error"));
  }
 ta.setText(new String(buf,0));
 fis.close();
 bis.close();
 throw new IOException();
}
____________________________________
0
Comment
Question by:Xolani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
ovidiucraciun earned 50 total points
ID: 2824817
here you have your functin a litle bit modified, and works ;)
/////////////////////////////
package noel.test;

import java.io.*;
import javax.swing.*;
import java.awt.*;

public class FileReadTest
{
  private static JFrame main = new JFrame( "test read file" );
  private static JTextArea ta = new JTextArea();
 
  public void read(String fl) throws IOException
  {
    FileInputStream fis = new FileInputStream(fl);
    BufferedInputStream bis = new BufferedInputStream(fis);
    int r;
    byte [] buf = new byte[64000];
    try
    {
      while( (r = bis.read(buf)) != -1 )
      {
        ta.append( new String( buf, 0, r-1 ) );
      }
    }
    catch(Exception ex)
    {
        System.out.println("Reading error");
    }
    fis.close();
    bis.close();
  }
  public static void main( String args[] )
  {
    main.getContentPane().setLayout( new BorderLayout( ) );
    main.getContentPane().add( new JScrollPane( ta ) );
    main.setSize( 500, 800 );
    main.setVisible( true );
   
    FileReadTest test = new FileReadTest();
    JFileChooser fc = new JFileChooser(".");
    if ( JFileChooser.APPROVE_OPTION == fc.showOpenDialog( main ) )
    {
      try
      {
        test.read( fc.getSelectedFile().getAbsolutePath() );
      }
      catch( IOException ex )
      {
        JOptionPane.showMessageDialog(null,ex.toString() + String.valueOf("error reading file"));
        ex.printStackTrace();
      }
    }
    else
    {
      JOptionPane.showMessageDialog(null,String.valueOf("no file selected"));
    }
  }
}
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2825614
How about this

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
   
0
 

Author Comment

by:Xolani
ID: 2825961
I have also learn how to use JFileChooser!!!
I do not understand how your points system works but your answer is good!!
0
 
LVL 3

Expert Comment

by:ovidiucraciun
ID: 2826064
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
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hibernate example using maven 12 104
Convert from a json string array to a Java object 3 76
Java class and jar 3 57
passing enum to a method 4 47
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question