aspose file tools*
The moose likes Java in General and the fly likes BufferedReader contents into Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "BufferedReader contents into Array " Watch "BufferedReader contents into Array " New topic
Author

BufferedReader contents into Array

Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
Hey guys.

I read in a csv file which contains 2 small lines. and print out the contents of the csv file

String line = null;
try {

FileReader fr = new FileReader("c:/product/products2.csv");
BufferedReader br = new BufferedReader(fr);

while( null != (line = br.readLine() ) ) {
System.out.print(line +"\n");
}
}
catch (Exception e) {
e.printStackTrace();
}
System.out.print(line +"\n");

works grand, but the print statement outside of the try catch prints null, why is this?
thanks
Mark
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
is there a way to convert br to a string or string array?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Originally posted by Mark Hughes:
the print statement outside of the try catch prints null, why is this?


Because the exit condition for your loop is that "line" is null. After that loop, line is guaranteed to be null.


[Jess in Action][AskingGoodQuestions]
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
sorry yea, tired head.

so how do i get the 2 lines of the csv file into an array,
if i do String[] textfile = {line}; in the while loop it just puts [Ljava.lang.String;@82ba41 into the array too represent the lines of the csv file which i can not do anything with eg use split() to serperate the commas in the csv file which is i want to do
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
If you know in advance how many lines there will be you create the array before the loop and then you will need a counter which you use an an index into the array. You increment the counter each time through the loop.

If the number of lines is variable then add the lines to an ArrayList in the loop and then convert the ArrayList to an array after the loop


Joanne
Mark Hughes
Ranch Hand

Joined: Jul 14, 2006
Posts: 146
yep thanks got it there,
declared arraylist outside loop, then in loop just did arraylist.add(line) and that pretty much did it, thanks
Mark
K Terr
Greenhorn

Joined: Jun 20, 2006
Posts: 14
Hi,

I was wondering if you could help me. I have a .csv file printing to the screen. However i am trying to get the .csv file to go into a java table with the same headings and columns as in the file. The problem is i have no idea how to do this or where to even begin.

Can anyone please help me. Its for a project i'm doing.

Thanks,

K Terr.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
The Swing tutorial will teach you how to create tables, add headers and add rows. Once you've read and understood that, you just need to replace the line in your code that writes to the console, with code to add a row to the table.

Have a go and if you have any problems come back and ask.
K Terr
Greenhorn

Joined: Jun 20, 2006
Posts: 14
Hi,

I am serious trouble here, i am trying to store the content of the .csv file in a list and then place the list contents into an SWT table. the problem is when i print the contents of the .csv file to the command line it prints them out one by one, but when i try to add each line to the list and print the list everthing is on one line.

can anyone plese help i am really stuck, i have never done anything like this before.

Thanks in advance,

K Terr

Here is the code i am using. i dont know how many line will be in the .csv file hense why i used a list.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
Instead of adding the lines to a List, why don't you just add them to the table ?

K Terr
Greenhorn

Joined: Jun 20, 2006
Posts: 14
hi,

would you know how to split the line into tokens and store each part in its own separate variable:


can you please help me i am really stuck on this,

thanks for the help

K Terr
K Terr
Greenhorn

Joined: Jun 20, 2006
Posts: 14
Hi,

its ok i got it to work,

i used:


K Terr
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BufferedReader contents into Array
 
Similar Threads
Problem in reading empty line
Help Needed regarding reading the csv file
No such element error
Reading File contents to a String
Reading a text file using FileReader & BufferedReader : Unexpected output !