wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Reading a file and storing its contents into a String Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Reading a file and storing its contents into a String Array" Watch "Reading a file and storing its contents into a String Array" New topic
Author

Reading a file and storing its contents into a String Array

roopa chakra
Greenhorn

Joined: Jul 12, 2005
Posts: 13
Hi,

I have file called fives.txt which has
abcds
yngds
rteds
uoiyt
pouyi
'
'
'
'
upto 1000
How do I read these five lettered words and store it in a String array.
I tried in following program. file.read();doesn't work.

Can anyone help me with this.

try {
File inputFile=new File("Fives.txt");
int fileSize=(int) inputFile.length();
FileInputStream file=new FileInputStream(inputFile);
String linArray[]=new String[fileSize];
for(int i=0;i<5;i++)
{
linArray[i]=file.read();
System.out.println(linArray[i]);
}
file.close();
}

catch(IOException e){

System.out.println("I could not read the file");

}


roopa chakra
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Roopa
Could you use a FileReader, wrap it up with a BufferedReader and use the readLine() method to read the contents of the file.
roopa chakra
Greenhorn

Joined: Jul 12, 2005
Posts: 13
Thanks Ameeral

But still I am not getting the output.

String linArray[i]=file.read();//Incompatible types.

This is what I wrote
try {
BufferedReader br=new BufferedReader(new FileReader("Fives.txt"));
while(br.readLine()!=null)
{
File inputFile=new File("Fives.txt");
int fileSize=(int)inputFile.length();
FileInputStream file=new FileInputStream(inputFile);
String linArray[]=new String[fileSize];
for(int i=0;i<5;i++)
{
linArray[i]=file.read();
System.out.println(linArray[i]);
}

file.close();
}

}
catch(IOException e){

System.out.println("I could not read the file");

}


please help.
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
It helps us if you will copy and post the contents of the console when you run your program so we can see the error messages or output.
It also helps if you CODE formatting for your program. See buttons below text input area.

In your program, you are mixing two input methods. One is the br.readLine() and the other is file.read(). Why is that?
To read strings, the br.readLine() is a good way.
Get rid of the other code and process what is read by br.readLine();

Have you read the doc for the readLine() method?
Does it return a value? What does your program do with that value?
Nothing that I can see.
You need to save what is read in a variable:
will read a line from the file into rec.
Then you can put rec into the array.
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Roopa
There are quite a few problems with the code you have written. One of the problems is that you need to know the number of lines in your file, using the following code will not help.

The length() method in the File object will written the length in bytes and not the number of lines in your file. So the above approach is incorrect.

I assume you are using the length() method so that you can initialize your array. You can instead use a Vector() it has a default size and will increase as you add objects to it.

Please read up on exceptions too, because you will use them frequently while using the java.io.* package.

Summing it altogether try the code below. Please get back if you have any problems.


goodLuck !
roopa chakra
Greenhorn

Joined: Jul 12, 2005
Posts: 13
Thanks guys,

It was very helpful. I think I am closer to getting output.
This is my assignment. I cann't use vector only String Array.
I have to find length of the file so that I can specify array size.
I will use counter to get the length of the file.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading a file and storing its contents into a String Array
 
Similar Threads
Implementing download - XLS files - JSP scriptlet
FileNotFoundException,(help please)
How to use Mark and reset for inputstream ?
Reading file and putting in an array
Help! java.io.FileNotFoundException