title: Sysout list of files and transfer files from a directory in java
body: <p>I'm trying to figure out how to get a list of files on the server and how to download them individually. Can anyone guide me in the right direction?</p>
  
  <p>I'm getting file permissions error or (is a directory errors), thanks</p>
  
  <p>UPDATE: 
  The error is</p>
  
  <pre><code>Exception in thread "main" java.io.FileNotFoundException: /myClientFiles (Permission denied)
  at java.io.FileOutputStream.open(Native Method)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:213)
  at java.io.FileOutputStream.&lt;init&gt;(FileOutputStream.java:101)
  at 
  xxxxxxpackegename.com.fr(Client.java:38)
  </code></pre>
  
  <p>Client.java</p>
  
  <pre><code>public class Client {
  
   private static final int PORT = 2665;
   private static String HOST = "localhost";
  
  public static void main(String[] args) throws UnknownHostException, IOException {
  
  
      int filesize = 5000000;   //buffer size 5mb
      int bytesRead;
      int currentTotalNumberOfBytes = 0;
  
       //connect to port on server - server waits for this after running socket.accept() in the Server class
      Socket socket = new Socket(HOST, PORT);  
  
      byte[] byteArray = new byte[filesize];   //create a byte array of 5mb        
      InputStream inputStream = socket.getInputStream();  //channel to write to server
      FileOutputStream fileOutStream = new FileOutputStream("/myClientFiles");      
      BufferedOutputStream bufferOutStream = new BufferedOutputStream(fileOutStream);
  
      bytesRead = inputStream.read(byteArray, 0, byteArray.length);
      currentTotalNumberOfBytes = bytesRead;
  
  
      do { //read till the end and store total in bytesRead and add it to currentTotalNumberOfBytes
          bytesRead = inputStream.read(byteArray, currentTotalNumberOfBytes, (byteArray.length-currentTotalNumberOfBytes)); 
          if(bytesRead &gt;= 0) currentTotalNumberOfBytes += bytesRead; 
          }while(bytesRead &gt; -1); // when bytesRead == -1, there's no more data left and we exit the loop
  
  
      bufferOutStream.write(byteArray, 0 , currentTotalNumberOfBytes); //write the bytes to the file
  
      bufferOutStream.flush(); 
      bufferOutStream.close(); 
      socket.close(); 
  
  
  
   }
  
  
  }
  </code></pre>
  
  <p>Server.java </p>
  
  <pre><code>ServerSocket serverSocket = new ServerSocket(2665);
  
      Socket socket = serverSocket.accept();                  
      System.out.println("Connected to: " + socket);
  
      //File transferFile = new File("Allcrisis.doc");              //get local file
  
      File[] transferFiles = new File("/myServerFiles").listFiles(); //array to store pathnames of files in myServerFiles folder
  
      byte[] bytearray = new byte[(int)transferFiles.length];  
  
  
      for(File file: transferFiles){    
      //FileInputStream fileInputStream = new FileInputStream(transferFiles); 
      FileInputStream fileInputStream = new FileInputStream(file);
      BufferedInputStream butterInputStream = new    BufferedInputStream(fileInputStream); 
      butterInputStream.read(bytearray, 0, bytearray.length); 
  
  
      OutputStream outStream = socket.getOutputStream();                
      System.out.println("Sending...");                    
      outStream.write(bytearray, 0, bytearray.length);                
  
      outStream.flush();   
      }
  
  
  
      socket.close();  
  </code></pre>