There is an example code:

private void analyzeCurve() throws IOException { if (listX.isEmpty() || listY.isEmpty()) { WorkspaceWindow.showWarningMessage(EMPTY_DRAWING_LIST); } else { ArrayList<Integer> lx; ArrayList<Integer> ly; File xmlFile = new File(File.separator + "tmp" + File.separator + "xmlFile.xml"); //создание файла FileWriter fr = new FileWriter(xmlFile); BufferedWriter writer = new BufferedWriter(fr); //действия с файлом... writer.flush(); writer.close(); } } 

When running under Linux, the file is successfully created and written to. But during the launch of the code in Windows file is not created.
We get an exception:

 java.io.FileNotFoundException: \tmp\xmlFile (Системе не удается найти указанный путь) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(FileOutputStream.java:270) at java.io.FileOutputStream.<init>(FileOutputStream.java:213) at java.io.FileOutputStream.<init>(FileOutputStream.java:162) at java.io.FileWriter.<init>(FileWriter.java:90) at client.logic.Analyzer.analyzeCurve(Analyzer.java:69) at client.logic.Analyzer.run(Analyzer.java:43) 

This is due, as I understand it, to the absence of the \ tmp \ directory in Windows, but then the question arises:
1. How to create files in Windows and Linux in the tmp folder, that is, in the folder for temporary files?
2. Is it possible to somehow understand what system we are in and, depending on this, set the path for creating files?

    2 answers 2

    1. There is a ready-made static method that solves your Files.createTempFile () task .

    2. If for some other task you need to know the current OS, use this snippet:

       String os = System.getProperty("os.name") 
    • Thank you, but it is assumed that the file that I create is intermediate, which should then be saved under a new name in a new place on the disk. Can I do this with a temporary file? - VVRud
    • @VVRud, of course you can. From the point of view of the OS, there is no such thing as “save a file under a new name”. You either move the existing file Files.copy() ( Files.copy() ) or save the data that you have in your memory to some other arbitrary file. - Nofate 4:39 pm

    Try using the java.nio.file.Path class:

     Path path = Paths.get("tmp", "file.txt"); Path file = Files.createFile(path);