title: Java How to read/write files to/from any user's desktop
body: <p>I want to read a file from the user's desktop, and then write a new file to the desktop. Below is the code to do this on my own computer, but I'm not sure how to do it for any user. </p>
  
  <p>I found <a href="https://stackoverflow.com/questions/17357554/how-do-i-read-write-files-to-an-unknown-user-directory">this</a> previously-asked question, but I'm not sure how to implement such a method in Java. Would I possibly use the <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/System.html#getProperty(java.lang.String)" rel="nofollow noreferrer"><code>System.getProperty()</code></a> method?</p>
  
  <p>Any help or advice is appreciated. Thanks!</p>
  
  <pre><code>public static String [] read()
  {
      ArrayList&lt;String&gt; fileArrList = new ArrayList&lt;&gt;();
      Scanner scan = new Scanner(System.in);
      System.out.print("File name &gt; ");
      String fileName = scan.nextLine();
  
      try
      {
          Scanner file = new Scanner(new File("C:\\Users\\pez\\Desktop\\" + fileName));
  
          while (file.hasNextLine())
          {
              String line = file.nextLine();
              fileArrList.add(line);
          }
  
          file.close();
  
          String [] fileArr = new String[fileArrList.size()];
          fileArr = fileArrList.toArray(fileArr);
  
          return fileArr;
      }
      catch (FileNotFoundException fnfe)
      {
          System.out.println("File not found");
      }
  
      return null;
  }
  
  public static void writeNewFile(ArrayList outputArr)
  {
      try
      {
          FileOutputStream fos = new FileOutputStream("C:\\Users\\pez\\Desktop\\output.txt", false);
          PrintWriter pw = new PrintWriter(fos);
  
          for (Object i : outputArr)
          {
              pw.println(i);
          }
  
          pw.close();
      }
      catch (FileNotFoundException fnfe)
      {
      }    
  }  
  </code></pre>