Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading a file and storing its contents into a String Array

 
roopa chakra
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");

}
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic