1

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.

I found this previously-asked question, but I'm not sure how to implement such a method in Java. Would I possibly use the System.getProperty() method?

Any help or advice is appreciated. Thanks!

public static String [] read()
{
    ArrayList<String> fileArrList = new ArrayList<>();
    Scanner scan = new Scanner(System.in);
    System.out.print("File name > ");
    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)
    {
    }    
}  
Community
  • 1
  • 1
pez
  • 3,859
  • 12
  • 40
  • 72

2 Answers2

2

You can use the system property user.home:

    File desktop = new File(System.getProperty("user.home"), "/Desktop"); 
    Scanner file = new Scanner(new File(desktop, fileName));

I assume you know you are limited to windows environments this way.

Ozan
  • 4,345
  • 2
  • 23
  • 35
  • Thanks! Is there a way to maintain usability across all platforms, without specifically asking the user to input the location of the file? – pez Aug 09 '14 at 22:37
  • 1
    `user.home` is platform-independent, but the location of the desktop directory varies between different desktop environments in linux – Ozan Aug 09 '14 at 22:40
  • Thanks, that worked. How can I modify the `writeNewFile()` function to automatically write to the desktop and name the file `output.txt`? I've tried several things and the code compiles and runs, but the file never shows up on the desktop. – pez Aug 09 '14 at 23:28
  • Make sure to create the file with `File output = new File(desktop, "output.txt")` and `PrintWriter pw = new PrintWriter(output)`. Maybe debug and inspect the resulting path of `output`. – Ozan Aug 09 '14 at 23:55
1

I'll give you simple examples that you can implement in your project. To get windows username you can get the property user.name:

System.getProperty("user.name");

To write on a file you can use this simple code:

PrintWriter writer = new PrintWriter("filename.txt", "UTF-8");
writer.println("Some text");
writer.println("Some other text");
writer.close();

To read a text file you can use this:

BufferedReader br = new BufferedReader(new FileReader("/path/to/filename.txt"));
String textLine = null;
while ((textLine = br.readLine()) != null) {
    System.out.println(textLine);
}

You can implement all this code in your project easily. I hope you find this usefull, sorry for my english.

Marcello Davi
  • 433
  • 4
  • 16