• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with java input file

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jonas,

you are using the "FileReader(String fileName)" constructor in "new FileReader("questionFile.txt"))". So any string will do.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic