• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with java input file

 
jonas okwara
Ranch Hand
Posts: 58
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please
I need help with this code. I want to be able to change "questionFile.txt" to correspond to the name of any file I input into the application.



try {

BufferedReader inputfile = new BufferedReader( new FileReader("questionFile.txt"));
for ( int i = 0; i < 3; i++){
question[i] = inputfile.readLine();
answer1[i] = inputfile.readLine();
answer2[i] = inputfile.readLine();
answer3[i] = inputfile.readLine();
answer4[i] = inputfile.readLine();
answer5[i] = inputfile.readLine();
}

inputfile.close();
}catch (IOException e) {
System.err.println(e);
System.exit(1);
}
 
Irina Goble
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jonas,

you are using the "FileReader(String fileName)" constructor in "new FileReader("questionFile.txt"))". So any string will do.
 
usha nataraj
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jonas,
Pass the file name you want to read as Input parameter to main pgm like
> java pgmname "input file".
In your main function read it using args[0] into a string say strFile, where args[] is the main() function argument.
In BufferedReader constructor, you say -
BufferedReader br = new BufferedReader(strFile);
Hope this helps you out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic