6 Replies Latest reply: Jun 12, 2007 6:15 AM by 807605 RSS

    read group of files

    807605
      hi. i've got an issue. my code is prepared for one file reading:

      FileReader fr = new FileReader(file_name);
      BufferedReader br = new BufferedReader(fr);
      FileWriter fw = new FileWriter("out.txt");

      now i have to read a hole directory in a loop for example and write it to one out.txt. how can i do it, what should i put into:

      FileReader(?)?. thanks for any advice, best regards!
        • 1. Re: read group of files
          807605
          you mean you want to concatenate the files in any order?
          • 2. Re: read group of files
            800322
            now i have to read a hole directory in a loop for
            example and write it to one out.txt. how can i do it,
            FireWriters have an append flag for their c'tors, if that's what you want to know. For more information, consider reading the API docs. if you don't know how to use a loop, Google for "Java tutorial".
            what should i put into:

            FileReader(?)?. thanks for any advice, best regards!
            A reference to a File instance. Best, one obtained from theDirFile.listFiles().
            • 3. Re: read group of files
              807605
              Use file.list(0 to get an array of the files in the folder. Then loop through the array to get the specific files which you can perform the reading, saving each read file contents in a temporary string. Each time a new file is read, append the contents of that file to the temporary string, finally taking the string and saving it into the one single big file that you want.
              • 4. Re: read group of files
                800322
                Each time a new file is read, append the contents of
                that file to the temporary string, finally taking the
                string and saving it into the one single big file
                that you want.
                No technical need to store the files...
                • 5. Re: read group of files
                  807605
                  file.list()
                  you mean file.listFiles()

                  Like:
                  File directory = new File("c:/temp");
                  File[] allFiles = directory.listFiles();
                  Then make a loop and have a reader for each file in allFiles.
                  • 6. Re: read group of files
                    807605
                    thanks for advice, problem issue solved. REGARDS:)