Hi ,all..
How can i read more than one java file but ,without repeating the section of bufferedreader in every time i read file!!....so if i have 20 file ...this way is unpractical??
for example:
this code:

public class  buffer{

 public static void main(String[]args) throws IOException {
            String filname1= "C:/filename/filename1";
            String filname2= "C:/filename/filename2";
            String filname3= "C:/filename/filename2";


try{
           FileReader fr = new FileReader((filname1));
            BufferedReader br = new BufferedReader(fr);
             line = br.readLine();
            while (line != null) {


            System.out.println(line);
            line = br.readLine();
            }
        } catch (IOException e) {
         e.printStackTrace();
        }  


try{
           FileReader fr2 = new FileReader((filname2));
            BufferedReader br2 = new BufferedReader(fr2);
             line = br2.readLine();
            while (line != null) {


            System.out.println(line);
            line = br2.readLine();
            }
        } catch (IOException e) {
         e.printStackTrace();
        }       

        try{
           FileReader fr3 = new FileReader((filname3));
            BufferedReader br3 = new BufferedReader(fr3);
             line = br3.readLine();
            while (line != null) {


            System.out.println(line);
            line = br3.readLine();
            }
        } catch (IOException e) {
         e.printStackTrace();
        }  

You simply put the code for one file into a method, taking the file name as a parameter. Then you can call that method as many times as you like, passing a different file name each time.

The only way is to spawn multiple threads and do the read in each. That will parallelize the I/O, though throughput will not likely improve over your current serialized approach. You can also put the read/code (try/catch blocks) in a separate function which you would call 3 (or N) times sequentially. That way, you only have 1 try/catch/open/read/close block of code. Useful when you want to go from 3 files to 99 files... :-)

FWIW, if you do the threaded approach, you will still need that separate function to deal with file I/O.

Edited 8 Months Ago by rubberman

Mr. JamesCherrill , thanks for your answer I know I must use a method and pass file path as parameter but I don't know how to do that exactly ... I write it previously ..can you help me in simple example please ? depending on my code that I write it...

It's as simple as

void readFile(String fileName) {
   try {
        FileReader fr = new FileReader((fileName));
        etc
}

then

readFile("file_one.txt");
readFile("file_two.txt");
etc

Thanks Mr. rubberman,,
It's a usful way, I will read about it and try to use it.. thank you.

Rubberman is right, but if you are still finding your way with writing simple methods then it's too soon for you to get into multiple threads. Do one thing at a time.

You are right ....
Mr. JamesCherrill. So I want to help me how to write function and passing path ...
one time I write it but it doesn't work and I don't know whats error or how should write this function include section try catch that I write it in my code ..

Edited 8 Months Ago by Mar. Na.

Mr. JamesCherrill. ..
If for example : I read file and inside it I put condition if for example:if line of the file contain "m", so open the second file and if second file contain "m", so open third file and so on.........
In your code the function void and not return any thing so how can I read file and make condition to open second file and so on.....???

I mean ...
I read the lines of first file and if any line contain the string "m", so ..read second file and if second file contain " m ", so. . read the third file and so on......
How can I do this ?

Edited 8 Months Ago by Mar. Na.

Either return a boolean from the method showing whether or not the "m" was found,
or
test the condition inside the method and call it for the next file from there.

If you make one more post saying "I write it but it doesn't work" without posting the actual code I will stop answering your questions.

Ok..Mr. JamesCherrill ...thank you very much for your answer ...
I am sorry ,
your answer is perfect ...and try it and it's work.
Thanks alot.

This article has been dead for over six months. Start a new discussion instead.