I need to create a method that will write all the contents entered by the user from the console to the file in the specified path, without overwriting the contents of the file. And writing to the file should be done after entering the wr command on a separate line. I did the method, but as a result of his work, the first cath works right away. When the program shows a debug, the process cannot access the file, as this file is being used by another process.

public static void writeToFileFromConsole(String path){ InputStreamReader reader = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(reader); FileWriter writer = null; BufferedWriter bufferedWriter = null; System.out.println("Enter file content to write in the file:"); try { String command = "wr"; String line; line = br.readLine(); if ((line = br.readLine()).equals(command)) { writer = new FileWriter(path, true); bufferedWriter = new BufferedWriter(writer); bufferedWriter.append("\n"); bufferedWriter.append(line); } }catch (IOException e){ System.err.println("Can't write to file with path " + path); } finally { try { reader.close(); br.close(); if (writer != null){ writer.close(); } if (bufferedWriter != null){ bufferedWriter.close(); } }catch (IOException e){ System.err.println("File with path " + path + " not found" ); } } } 

What is my mistake? And how to fix that the data entered in the console was recorded in the file.

  • Are you trying to call writeToFileFromConsole in a loop? ............. In general, flows should be closed in the reverse order. at the beginning you need to close the bufferedWriter and then the writer ........... + you can read / write using try with resources - Alexey Shimansky
  • No, I don’t call in a loop ... I created a class Demo and in it I test the work of the program, just calling this method - YuriiS
  • And closing the threads in the reverse order did nothing. - YuriiS
  • Try restarting the computer, it is possible that the process hangs during the debugging process and the Windows do not allow access to the file anymore, maybe you forgot to close the running process in the debugger or open some file for editing - Viacheslav Vedenin

1 answer 1

I checked your code, no errors occurred, except for the exception that the file does not exist.

Redid it to not occur:

 private static void writeToFileFromConsole(String path) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter file content to write in the file:"); try (OutputStream output = Files .newOutputStream( Paths.get(path), StandardOpenOption.APPEND, StandardOpenOption.CREATE); BufferedOutputStream outputStream = new BufferedOutputStream(output)) { String line; while (!"rw".equalsIgnoreCase(line = reader.readLine())) { outputStream.write(System.lineSeparator().getBytes()); outputStream.write(line.getBytes()); } } catch (IOException e) { e.printStackTrace(); } } 
  • And what in my version is not correct? After starting the method, the console displays the following: C: \ Users \ Skorodielov \ Desktop \ Test1.txt - YuriiS
  • check the path, or it is not true or not have rights to write to this file - Artem Konovalov
  • I checked ... I myself created this file and wrote data into it, only by two methods ... I read data from the console with one, and wrote another with this file ... and now I tried to combine the two methods into one and did not write - YuriiS
  • Try restarting the computer, it is possible that the process hangs during the debugging process and the Windows do not give access to the file anymore, maybe you forgot to close the running process in the debugger or open some file for editing - Viacheslav Vedenin
  • one
    Thank you all for your help. Understood, it was necessary to add the variable line1 and transfer it to an entry in the file - YuriiS