title: Multithreading, Read multiple files and write to new Files
body: <pre><code>import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.nio.MappedByteBuffer;
  import java.nio.channels.FileChannel;
  import java.nio.charset.Charset;
  import java.util.ArrayList;
  import java.util.concurrent.ExecutorService;
  import java.util.concurrent.Executors;
  
  public class Test6 implements Runnable {
  
      private File file;
      private int totalNumberOfFiles = 0;
      private static int nextFile = -1;
      private static ArrayList&lt;String&gt; allFilesArrayList = new ArrayList&lt;String&gt;();
      private static ExecutorService executorService = null;
  
      public Test6(File file) {
          this.file = file;
      }
  
      private String readFileToString(String fileAddress) {
          FileInputStream stream = null;
          MappedByteBuffer bb = null;
          String stringFromFile = "";
          try {
              stream = new FileInputStream(new File(fileAddress));
              FileChannel fc = stream.getChannel();
              bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
              /* Instead of using default, pass in a decoder. */
              stringFromFile = Charset.defaultCharset().decode(bb).toString();
          } catch (IOException e) {
              System.out.println("readFileToString IOException");
              e.printStackTrace();
          } finally {
              try {
                  stream.close();
              } catch (IOException e) {
                  System.out.println("readFileToString IOException");
                  e.printStackTrace();
              }
          }
          return stringFromFile;
      }
  
      private void toFile(String message, String fileName) {
          try {
              FileWriter fstream = new FileWriter("C:/Users/Nomi/Desktop/Workspace2/Test6/TestWritten/" + fileName);
              System.out.println("printing to file: ".concat(fileName));
              BufferedWriter out = new BufferedWriter(fstream);
              out.write(message);
              out.close();
          } catch (Exception e) {
              System.out.println("toFile() Exception");
              System.err.println("Error: " + e.getMessage());
          }
      }
  
  //  private void listFilesForFolder(final File fileOrFolder) {
  //      String temp = "";
  //      if (fileOrFolder.isDirectory()) {
  //          for (final File fileEntry : fileOrFolder.listFiles()) {
  //              if (fileEntry.isFile()) {
  //                  temp = fileEntry.getName();
  //                  toFile(readFileToString(temp), "Copy".concat(temp));
  //              }
  //          }
  //      }
  //      if (fileOrFolder.isFile()) {
  //          temp = fileOrFolder.getName();
  //          toFile(readFileToString(temp), "Copy".concat(temp));
  //      }
  //  }
  
      public void getAllFilesInArrayList(final File fileOrFolder) {
          String temp = "";
          System.out.println("getAllFilesInArrayList fileOrFolder.getAbsolutePath()" + fileOrFolder.getAbsolutePath());
          if (fileOrFolder.isDirectory()) {
              for (final File fileEntry : fileOrFolder.listFiles()) {
                  if (fileEntry.isFile()) {
                      temp = fileEntry.getAbsolutePath();
                      allFilesArrayList.add(temp);
                  }
              }
          }
          if (fileOrFolder.isFile()) {
              temp = fileOrFolder.getAbsolutePath();
              allFilesArrayList.add(temp);
          }
          totalNumberOfFiles = allFilesArrayList.size();
          for (int i = 0; i &lt; allFilesArrayList.size(); i++) {
              System.out.println("getAllFilesInArrayList path: " + allFilesArrayList.get(i));
          }
      }
  
      public synchronized String getNextFile() {
          nextFile++;
          if (nextFile &lt; allFilesArrayList.size()) {
  //          File tempFile = new File(allFilesArrayList.get(nextFile));
              return allFilesArrayList.get(nextFile);
          } else {
              return null;
          }
      }
  
      @Override
      public void run() {
          getAllFilesInArrayList(file);
          executorService = Executors.newFixedThreadPool(allFilesArrayList.size());
          while(nextFile &lt; totalNumberOfFiles)
          {
              String tempGetFile = getNextFile();
              File tempFile = new File(allFilesArrayList.get(nextFile));
              toFile(readFileToString(tempFile.getAbsolutePath()), "Copy".concat(tempFile.getName()));
          }
      }
  
      public static void main(String[] args) {
          Test6 test6 = new Test6(new File("C:/Users/Nomi/Desktop/Workspace2/Test6/Test Files/"));
          Thread thread = new Thread(test6);
          thread.start();
  //      executorService.execute(test6);
  //      test6.listFilesForFolder(new File("C:/Users/Nomi/Desktop/Workspace2/Test6/"));
      }
  }
  </code></pre>
  
  <p>The programs' doing what's expected. It goes into the folder, grabs a file, reads it into a string and then writes the contents to a new file.
  I would like to do this multi threaded. If the folder has N number of files, I need N number of threads. Also I would like to use executor framework if possible. I'm thinking that there can be a method along this line: </p>
  
  <pre><code>public synchronized void getAllFilesInArrayList() {
          return nextFile;
      }
  </code></pre>
  
  <p>So each new thread could pick the next file.
  Thank you for your help.</p>
  
  <p>Error:</p>
  
  <pre><code>Exception in thread "Thread-0" java.lang.IllegalArgumentException
      at java.util.concurrent.ThreadPoolExecutor.&lt;init&gt;(ThreadPoolExecutor.java:589)
      at java.util.concurrent.ThreadPoolExecutor.&lt;init&gt;(ThreadPoolExecutor.java:480)
      at java.util.concurrent.Executors.newFixedThreadPool(Executors.java:59)
      at Test6.run(Test6.java:112)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>