• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Reading a file and storing its contents into a String Array

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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");

}
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 5035
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic