Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By r035198x

Thread: Reading Multiple Files.

  1. #1
    gilmap is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default Reading Multiple Files.

    Hi, I am new in Java,

    I have 10 files (say a1.txt, a2.txt, a3.txt,..,a10.txt) each with 5,000 data (single column). Now I can write a program to read a single file and display using JTable.

    But, I can not find a way to write a program that can read all the files, and display them (in different columns).

    Could someone give me some ideas on how to write a program to read multiple files either using vectors or array; and display those files using JTable (or else).

    Thanks

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Change your program that read one file so that it contains a method which takes a fileName (String) and returns a list of lines in the file (List<String>).
    You can then call that method multiple times from a different method passing in different values for the fileName.
    gilmap likes this.

  3. #3
    gilmap is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Change your program that read one file so that it contains a method which takes a fileName (String) and returns a list of lines in the file (List<String>).
    You can then call that method multiple times from a different method passing in different values for the fileName.
    can you give me a sample code?

    The code that I have look like this:

    public void initVectors() {
    String aLine ;
    data = new Vector();
    columnNames = new Vector();
    try {
    FileInputStream fin = new FileInputStream(datafile);
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));
    // extract column names
    StringTokenizer st1 =
    new StringTokenizer(br.readLine(), ",");
    while(st1.hasMoreTokens())
    columnNames.addElement(st1.nextToken());
    // extract data
    while ((aLine = br.readLine()) != null) {
    StringTokenizer st2 =
    new StringTokenizer(aLine, ",");
    while(st2.hasMoreTokens())
    data.addElement(st2.nextToken());
    }
    br.close();
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Read my reply again and try the things I said and post if you get issues implementing it.

    Other things you need to change in your code are
    1.) Prefer ArrayList to Vector. Google Vector vs ArrayList to find out why.
    2.) Prefer the String.split method to StringTokenizer. Read the latest API specs for StringTokenizer to find out why.
    3.) Use either FileReader with BufferedReader for reading text files instead of FileInputStream.

  5. #5
    gilmap is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    With that kind of codes, I can only be able to read and manipulate data from a single file. So far I have failed to get the LOGIC on how I can read data from multiple files as described above. Probably I may need a simple example and use it as a platform.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    gilmap, if you have it working for one column it is as simple as implementing the working code more than once. Make each column finish printing data to the screen before loading the next one.

    However, I'm not an expert but I'm pretty sure you may run into some Heap Space errors if you try to save all the data. So if you plan on doing something else with the data, you might want to look at a different method for getting the information.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    gilmap is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    gilmap, if you have it working for one column it is as simple as implementing the working code more than once. Make each column finish printing data to the screen before loading the next one.

    However, I'm not an expert but I'm pretty sure you may run into some Heap Space errors if you try to save all the data. So if you plan on doing something else with the data, you might want to look at a different method for getting the information.
    That is what happened when I was trying to use arrays. Now the idea is to read data from outside file (txt, dat). But it seems am running out of ideas,because I have to get all 10 files from (a1.txt, a2,txt ..., a10.txt).

    how do I do this?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    Why do you need the contents of all the files in memory at the same time?

  9. #9
    gilmap is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Why do you need the contents of all the files in memory at the same time?
    It is not necessary that all the files should be in the memory...But I need a code that can loop through all the files;

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    If you put the names of the files in an array, your loop can use an index into that array to get the name of the file to read.

  11. #11
    gilmap is offline Member
    Join Date
    Jul 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    If you put the names of the files in an array, your loop can use an index into that array to get the name of the file to read.
    Yes ..that is good one.

    All that I need is a program that can read those text files and display them in different columns in JTable in this form:

    a1.txt a2.txt a3.txt ... a10.txt

    aaa 1235 abcd jjjj

    aaa 3333 defgh kkkk

    bbb . .. ...

    bbb .


    I hope somebody comes with a program to read those files and display their contents in the JTable as above.

    Thanks
    Last edited by gilmap; 07-19-2011 at 07:08 PM.

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default

    I hope somebody comes with a program
    Don't wait too long. The best hope for you to get a program is to write one.

  13. #13
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Instead of storing the data, which will require huge amounts of memory, read/print/remove the information by using a single variable for your readLine. Every time you change the object your variable references the hungry hungry GC will come and munch on your tasty useless object.

    You also should probably want to look at the String class, instead of using StringTokenizer. This is why, a direct quote from the StringTokenizer API.
    StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
    String (Java Platform SE 6)

    Now I would suggest you do a double loop. A while loop inside of a for loop. Write all the file names to an array, and then use the length of the array for your for loop. Then the while loop will continue going through your file continuing until nextLine is null. You could write to a different column by inserting the first loops counter into however you decide to detect each column.

    Make sure you don't save your data, because otherwise you'll just flood the heap with useless variables.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  2. Reading from .txt files
    By rileannas in forum Java Applets
    Replies: 4
    Last Post: 05-10-2011, 05:16 PM
  3. Reading .txt files
    By cvcs1 in forum New To Java
    Replies: 3
    Last Post: 01-20-2010, 09:07 AM
  4. Multiple ComboBox - Reading Text
    By jorgelex008 in forum AWT / Swing
    Replies: 6
    Last Post: 01-04-2010, 11:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •