0

I'm trying to write text to file.

If I use file = "C:\Temp\Dir\Test.txt", then I have no problem

If I use file with cyrillic in path = "C:\Temp\Папка на русском\Test.txt", then I have exception

Note. directories aren't created before code running

public Boolean writeStringToFile(String content, File file) {
    status = false;
    try (FileOutputStream fos = new FileOutputStream(file, false);
                    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
                    BufferedWriter fileWriter = new BufferedWriter(osw)) {
        fileWriter.write(content);
        status = true;
    } catch (FileNotFoundException e) {
        logger.error("File {}/{} can not be created.", file.getPath(), file.getName(), e);
    }
    return status;
}

for testing:

writeStringToFile("writeStringToFile is ok. Кодировка UTF-8", 
    new File(Files.temporaryFolderPath() + "Папка на русском" + File.separator + "Test.txt"))
assertTrue(file.exists());
Oleg
  • 19
  • 3
  • this is expected behavior, just dont use cyrillic in file name or path – Vault23 Oct 08 '19 at 11:51
  • please post [mcve] (e.g. "C:\Temp..." is not a valid java string) – user85421 Oct 08 '19 at 12:07
  • 1
    @Vault23 really? based on what? working fine for me (using "C:\\tmp\\Папка на русском\\A1.txt" and having directoies already created) – user85421 Oct 08 '19 at 12:16
  • @Vault23 works for me too. JDK 13 on Windows 10 and I also created the directories via Java code. – Abra Oct 08 '19 at 12:43
  • [Create whole path automatically when writing to a new file](https://stackoverflow.com/questions/2833853/create-whole-path-automatically-when-writing-to-a-new-file) – Abra Oct 08 '19 at 12:52
  • Most likely, your file was saved with an encoding which is different from the encoding the compiler expects. For instance, if you saved the file as an ISO 8859-5 file, but the compiler expects all source files to be UTF-8 files, you would get a FileNotFoundException, because the compiler interpreted the String in your source differently. – VGR Oct 08 '19 at 15:50
  • So I'm understand now, problem is creating new file in path with cyrillic symbols using BufferedWriter. Thanks – Oleg Oct 09 '19 at 12:20

2 Answers2

0
  1. In the "..." the backslashes must be escaped as \\ or replaced by /.
  2. The java compiler must use the same encoding as the editor, for String literals. You can test this by replacing "Папка" with u-escaped "\u041F\u0430\u043F\u043A\u0430".
  3. Directories must be created.

        Path path = Paths.get("C:\\Temp\\Папка на русском\\Test.txt");
        Files.createDirectories(path.getParent());
        Files.write(path,
            "C:\\Temp\\Папка на русском\\Test.txt".getBytes(StandardCharsets.UTF_8));
    
        writeStringToFile( "C:\\Temp\\Папка на русском\\Test.txt",
            new File( "C:\\Temp\\Папка на русском\\Test2.txt"));
    
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
-1

Invalid file path. Use "C:\Temp\Папка на русском\Test.txt".

Read please: https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html

It's best to write a cross-platform path (for *nix and Windows OS)

Dmitry Rakovets
  • 557
  • 1
  • 6
  • 15
  • I also initially got _Invalid file path_ because the directory didn't exist when I ran my code. Then I changed my code so that it also created the directory if it didn't exist and the program ran without exception, i.e. it created the directory and the file. I would say your answer is more of a recommendation and does not answer the OP's question as to why he failed to create the file in his java code. – Abra Oct 08 '19 at 12:48
  • also be aware that given link is about the `Path` class (`java.nio`), OP is using `File` (`java.io`) - this does not invalidate this *answer* ("write cross-platform path" could be explained...) – user85421 Oct 08 '19 at 14:01