Results 1 to 12 of 12
  1. #1
    hasrulhussin is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default about FileReader and BufferedReader - to read multiple files

    hello all experts,

    i'm just new to java and have an assignment.
    can somebody tell me what method to use to read multiple files.

    let's say i have 4 files ; 'file_01.dat', 'file_02.dat', 'file_03' and 'file_04.dat'. For each file, they have their unique data..let's say for 'file_01.dat' contains data '0.1234', file_02.dat' contains data value '0.5455' and so on. (Every data is in String datatype).

    I want to get all the data value (for all files) by writing all filenames lines by lines (file01.dat - file02.dat) and put it in a new file called 'File_List.dat'. I've used BufferedReader to read every line for that 'File_List.dat. and it shows only the file names I've written before.

    My problem is, i want to get every value (data) from every files and not the file names. Can somebody help me? I hope my text is clear enough because i'm not good in english writing.

    thanks for the help .have a nice day ^_^

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    i want to get every value (data) from every files
    To do that you would read each file one at a time and do something with the data you read - like write it to the File_List.dat file.

    I'm not sure I understand what is going wrong. Perhaps you can post the code you are using and say what output occurs. Also can you confirm that there is only one line of data in each file as you seemed to describe?

  3. #3
    hasrulhussin is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Ok. This is my code;

    Java Code:
    import java.io.*;
    
    class List_EuclideanDistance
    {
     public static void main(String args[])
      {
      try{
    
      FileInputStream fstream = new   FileInputStream("File_List.dat");
    
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
    
      while ((strLine = br.readLine()) != null){
      System.out.println (strLine);
    }
      in.close();
        }catch (Exception e){
      System.err.println("Error: " + e.getMessage());
      }
      }
    }

    p/s: In the FileList.dat, I wrote this statement line by line... file01.dat (line1), file02.dat (line) and file03.dat (line3). All these 3 filename is belong to 3 files with the same name and in every file they have their own value (in String datatype).

    >>>With the code above, I'm only able to display the data from File_List.dat that is file01.dat, file02.dat & file03.dat. I dont my program to display these output but all I want is to display every value from every file and my program should display it line by line. Is it clear? Sorry, I dont know what the suitable term for this method but I have found similar method to this but it's using BufferedImage. What is suitable BufferedImage for String? Can you give me some idea?

  4. #4
    Zman3359's Avatar
    Zman3359 is offline Member
    Join Date
    Jan 2011
    Posts
    42
    Rep Power
    0

    Default

    Your just printing what you are reading in from the first file. To do what I think you want to do you need to read in the file names from "File_List.dat", store them in an array or something, then use the stored file names to read from those files.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes, you need to actually read the files once you have their names as ZMan says. You don't necessarily need to put them into an array though. You could just read your way through the lines of File_List.dat and as you get a file name read that file.

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadFiles {
    	public static void main(String[] args) throws IOException {
    		BufferedReader listReader = new BufferedReader(
    				new FileReader("c:/File_list.dat"));
    		String fileName;
    		while((fileName = listReader.readLine()) != null) {
    			BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
    			String line;
    			while((line = fileReader.readLine()) != null) {
    				System.out.println(line);
    			}
    			fileReader.close();
    		}
    		listReader.close();
    	}
    }

  6. #6
    hasrulhussin is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    Yes, you need to actually read the files once you have their names as ZMan says. You don't necessarily need to put them into an array though. You could just read your way through the lines of File_List.dat and as you get a file name read that file.

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadFiles {
    	public static void main(String[] args) throws IOException {
    		BufferedReader listReader = new BufferedReader(
    				new FileReader("c:/File_list.dat"));
    		String fileName;
    		while((fileName = listReader.readLine()) != null) {
    			BufferedReader fileReader = new BufferedReader(new FileReader(fileName));
    			String line;
    			while((line = fileReader.readLine()) != null) {
    				System.out.println(line);
    			}
    			fileReader.close();
    		}
    		listReader.close();
    	}
    }
    hello sir,,,thanks for the help but when i put the filename ("File_List.dat") into the listReader and fileReader , i only get output displaying the filename 3 times... ("file01.dat","file02.dat","file03.dat", "file01.dat","file02.dat","file03.dat", "file01.dat","file02.dat","file03.dat")..i dont want these outputbut i want the value inside each file...can you explain more details because i'm not good and not so clever in programming...please sir..thanks for the help..

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Show us the code you're running, because that shouldn't happen with the code above.

  8. #8
    hasrulhussin is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Show us the code you're running, because that shouldn't happen with the code above.
    this is my code i run;

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.*;
    
    public class List_EuclideanDistance {
    	public static void main(String[] args) throws IOException {
    		BufferedReader listReader = new BufferedReader(
    				new FileReader("_EUCLIDEANDISTANCE_RESULT//0DIST_BLOKA.dat"));
    		String fileName;
    		String line;
    		while((fileName = listReader.readLine()) != null) {
    			BufferedReader fileReader = new BufferedReader(new FileReader("_EUCLIDEANDISTANCE_RESULT//0DIST_BLOKA.dat"));
    
    			while((line = fileReader.readLine()) != null) {
    				System.out.println(line);
    			}
    			fileReader.close();
    		}
    		listReader.close();
    	}
    }
    *0DIST_BLOKA.dat = file_list.dat ---------------------

    i got output like this:------------------------
    file01.dat
    file02.dat
    file03.dat
    file01.dat
    file02.dat
    file03.dat
    file01.dat
    file02.dat
    file03.dat
    ------------end of output-----------------

    can you show me how to get every file data in there? i mean i want to get data from every file01.dat, file02.dat & file03.dat.... do you have any suggestion??

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Look at pbrockway2's code again.
    Especially the second BufferedReader...

  10. #10
    hasrulhussin is offline Member
    Join Date
    May 2010
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Look at pbrockway2's code again.
    Especially the second BufferedReader...
    hmmm? do you think that i must change the name file to 'fileName' ? i've tried this before but when i try to execute, it give error 'exception in thread 'main' java.io.FileNotFoundException: dist_A01-A02.dat (The system cannot find the file specified)
    at java.io.FileInputStream.open<native method>
    at java.io.FileInputStream<init><FileInputStream.java :120>
    at java.io.FileInputStream<init><FileInputStream.java :79>
    at java.io.FileReader.<init><FileReader.java:41>
    at List_EuclideanDistance.main<List_EuclideanDistance .java:13>
    -----------------------------------------------------------------------------------------------

    can u explain to me why i get this error? anyway thanks for the help

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    That error means it cannot find that file.
    Since you say that the base file contains a list of filenames that exist, then it clearly doesn't.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    do you think that i must change the name file to 'fileName' ? i've tried this before but when i try to execute, it give error 'exception in thread 'main'
    Avoid the temptation of changing things just to make them wrok: be guided by knowledge rather than hope. Try your best to understand what the code is doing (read it closely, read the api documnentation, play with it by running it and seeing the results...), but always feel free to ask if don't understand it, or to check if your understanding is right.

    -----

    You have to use your file names, not the ones in my example.

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadFiles {
    	public static void main(String[] args) throws IOException {
    		BufferedReader listReader = new BufferedReader(
    				new FileReader("[color=blue]c:/File_list.dat[/color]"));
    		[color=red]String fileName;[/color]
    		while((fileName = listReader.readLine()) != null) {
    			BufferedReader fileReader = new BufferedReader(new FileReader([color=red]fileName[/color]));
    			String line;
    			while((line = fileReader.readLine()) != null) {
    				System.out.println(line);
    			}
    			fileReader.close();
    		}
    		listReader.close();
    	}
    }
    c:/File_list.dat: this one is whatever you are calling the file that has the list of filenames in it.

    fileName: the variable you use does not matter, but its value will be exactly one line from the other file.

    A couple of things about file names: they have to be valid file names on your system. It is more usual to use a backslash literal (\\, which means a single backslash) as in _EUCLIDEANDISTANCE_RESULT\\0DIST_BLOKA.dat or a single forward slash _EUCLIDEANDISTANCE_RESULT/0DIST_BLOKA.dat. (although what you have written will be accepted by Windows, often.)

    Secondly, if the name you give is a relative file name it will be interpreted relative to the current working directory. In this case the file name comes from a data file that already exists, but the relative path will only be resolved at runtime. This can cause problems and a good way to see what is going on is to add debugging code so that you can see exactly the files that your program is looking for.

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadFiles {
    	public static void main(String[] args) throws IOException {
    		[color=green]File f = new File("c:/File_list.dat");
    		System.out.println("Looking for " + f.getCanonicalPath());[/color]
    		BufferedReader listReader = new BufferedReader(new FileReader(f));
    		
    		String fileName;
    		while((fileName = listReader.readLine()) != null) {
    			[color=green]System.out.println("Found file name " + fileName);
    			f = new File(fileName);
    			System.out.println("Looking for " + f.getCanonicalPath());[/color]
    			BufferedReader fileReader = new BufferedReader(new FileReader(f));
    			String line;
    			while((line = fileReader.readLine()) != null) {
    				System.out.println(line);
    			}
    			fileReader.close();
    		}
    		listReader.close();
    	}
    }
    The point about code like this is that you are trying to get more information about why the file was not found.

    -----

    Exactly how you construct the files (or buffered readers) that provide the data will depend on how they are specified in the file that has the file names, and where they exist on the file system. A close reading of the File api documentation may be useful.

Similar Threads

  1. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  2. How to read multiple ZipEntry using single InputStream?
    By prasannadavid in forum New To Java
    Replies: 5
    Last Post: 09-08-2010, 01:31 PM
  3. Replies: 1
    Last Post: 05-06-2010, 10:06 PM
  4. cannot find symbol using BufferedReader.read()
    By mrs_sheep in forum Networking
    Replies: 4
    Last Post: 04-09-2010, 07:09 PM
  5. Library to read from multiple sources
    By bellurashwin in forum Advanced Java
    Replies: 1
    Last Post: 10-08-2009, 10:09 AM

Posting Permissions

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