Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a text file

Status
Not open for further replies.

KrisLake

Programmer
Feb 4, 2002
3
FI
I'm tryin to read a text file into a single
string. I only manage to read the firs line from the file.How can i read every line and make them into one string?

this is the part where i'm tryin to read
the file

File inputFile = new File ("help.txt");
FileReader fis =new FileReader(inputFile);
BufferedReader bis = new BufferedReader(fis);

String test=bis.readLine();
String tmp= "";
String nextLine;

while((nextLine=bis.readLine()!= null)) {
nextLine=nextLine.trim();
 
Hello,

First of all, I don't know how you even got that code to compile... when I copied and pasted it, the compiler wasn't having it. Anyways, first let me suggest that you move away from using compound statements for assignment and assertion checks inside a while condition. It's unreadable at worst, and confusing at best.

The following code will work as you wish:
Code:
    public static void main( String[] args )
    {
        StringBuffer fileContentsBuffer = 
            new StringBuffer();
        
        try
        {
            File inputFile = new File ("help.txt");
            FileReader fis =new FileReader(inputFile);
            BufferedReader bis = new BufferedReader(fis);
            
            for ( String nextLine = bis.readLine(); 
                  nextLine != null; 
                  nextLine = bis.readLine() )
            {
                fileContentsBuffer.append( 
                    nextLine.trim() );
            }
        }
        catch ( FileNotFoundException fnfe )
        {
            fnfe.printStackTrace();
        }
        catch ( IOException ioe )
        {
            ioe.printStackTrace();
        }
        
        System.out.println( 
            "Contents of file = " + 
            fileContentsBuffer.toString() );
    }

Best,
Adam Rice
 
NO offense Adam but compound statements are the way that experienced programmers tend to write code. You should see some of the C code I've had to maintain.

For instance, to copy an array of chars in C, you can tersely do:

char *a = "hello world";
char b[12];
char *pA = a;
char *pB = b;
while (pB++=pA++);

It all works because it is syntactically correct even though it is confusing if you don't understand the finer points of the language.

Anyways, I know this is off topic but I've seen you reprimanding someone else about this and I while I think your intentions are good, they are misguided.

Regards,

Charles
 
Sorry, I suppose I should clarify a bit....

I understand that more experienced programmers tend to use compound statements, but it seemed to me like he was having some issues with semantics more than anything, so I was trying to steer him in a different direction.

As a side note, I often use compound statements in my code, but was suggesting the alternative in order to make it easier for him. I realize that in a lot of instances compound statements actually make code easier to read - using the ternary operator in Java (especially on JSPs), for example.

I apologize if that came out sounding like a reprimand. That certainly wasn't my intention.

Best,
Adam
 
Thanks for clearing that up. I see your point and I agree with the idea of helping someone simplify things in order to make errors more obvious.

Happy coding!

Charles
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top