Results 1 to 4 of 4
Thread: File Reader with multiple files
- 06-01-2011, 02:59 PM #1
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
File Reader with multiple files
Currently I am trying to program a simple Artificial Intelligence by using a series of text files for the user name login, and the questions being asked. Currently, I am trying to set it so you can only pick one of four questions via a drop down menu in the Java GUI builder. I want it so I only have to use one BufferedReader for all of the files, so I put them in an Array like this...
I also have two methods that get the number of lines for a text file and stores the Strings in the file into an ArrayList...Java Code:public int[] QuestionLines() throws IOException { String[] filepath = {"How are you.txt", "What is your name.txt","What is 2+2.txt", "Say something random.txt"}; String Line; for(i = 0; i < 4; i ++) { FileReader NameReader = new FileReader(filepath[i]); BufferedReader reader = new BufferedReader(NameReader); } }
I was wondering if there was a way to do the same thing but with the multiple files in one reader with the Array for the file names. This is what I have come up with so far but have not been able to test it, being that it is not yet used in my program...Java Code:public ArrayList<String> FileData() throws IOException { FileReader NameReader = new FileReader("UserNames.txt"); BufferedReader linereader = new BufferedReader(NameReader); numberoflines = ReadLines(); ArrayList<String> userdata = new ArrayList<String>(); int c; for(c=0; c < numberoflines; c++) { if(userdata != null) { userdata.add(c, "User" + (c+1)); userdata.set(c, linereader.readLine()); } } linereader.close(); return userdata; } //------------------------------------------------------------------------- public int ReadLines() throws FileNotFoundException, IOException { FileReader NameReader = new FileReader("UserNames.txt"); BufferedReader reader = new BufferedReader(NameReader); String Line; numberoflines = 0; while ((Line = reader.readLine()) != null) { numberoflines++; } reader.close(); return numberoflines; }
If anyone has any advice or ideas that you can give me, it would be most appreciated. Thanks ^.^Java Code:public ArrayList<String> QuestionData() throws IOException { String[] filepath = {"How are you.txt", "What is your name.txt","What is 2+2.txt","Say something random.txt"}; ArrayList<String> questiondata = new ArrayList<String>(); int c; for(i = 0; i < 4; i ++) { FileReader NameReader = new FileReader(filepath[i]); BufferedReader linereader = new BufferedReader(NameReader); for(c=0; c < questionlines[i]; c++) { if(questiondata != null) { questiondata.add(c, "Answer" + (c+1)); questiondata.set(c, linereader.readLine()); } linereader.close(); } } return questiondata; } //------------------------------------------------------------------------- public int[] QuestionLines() throws IOException { String[] filepath = {"How are you.txt", "What is your name.txt","What is 2+2.txt", "Say something random.txt"}; String Line; for(i = 0; i < 4; i ++) { FileReader NameReader = new FileReader(filepath[i]); BufferedReader reader = new BufferedReader(NameReader); while ((Line = reader.readLine()) != null) { questionlines[i]++; } reader.close(); } return questionlines; } }
- 06-01-2011, 03:25 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
This is not using a single reader:
It is creating a new reader for each file, which it has to do since a reader is for a specific thing, generally.Java Code:for(i = 0; i < 4; i ++) { FileReader NameReader = new FileReader(filepath[i]); BufferedReader reader = new BufferedReader(NameReader); }
That aside, I'm having problems actually reading your code, since the formatting's all screwy.
- 06-01-2011, 08:00 PM #3
Member
- Join Date
- May 2011
- Posts
- 10
- Rep Power
- 0
I meant you only had to use the Reader once, I realize there are multiple readers created, but only one coded. And sorry for the formatting, I took it right out of a text document, since not all of the computers here at school have NetBeans on them that was the only way to actually get the code.
Anyway, I guess that I'm on the right track? I was just wondering if there was an easier way to do it, cause honestly, I'm just typing away only half knowing if what I'm doing is right because I can't check it in my code yet. Well, that's pretty much how I've gotten most of my code so far... >.>;
- 06-02-2011, 09:30 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Similar Threads
-
java file reader, jgrasp can't find the file
By aramiky818 in forum New To JavaReplies: 3Last Post: 04-22-2011, 02:06 AM -
Java file reader...?
By prabhurangan in forum New To JavaReplies: 3Last Post: 11-21-2008, 08:19 AM -
[SOLVED] Need help with file reader
By syed.shuvo in forum New To JavaReplies: 6Last Post: 09-27-2008, 07:43 PM -
Opeing multiple pdf files in different acrobat reader windows
By shweta.ahuja in forum Web FrameworksReplies: 2Last Post: 05-07-2008, 12:33 PM -
help with file reader
By jason27131 in forum New To JavaReplies: 1Last Post: 08-01-2007, 03:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks