S
S
STVDI2020-10-15 16:25:47
Java
STVDI, 2020-10-15 16:25:47

JAVA, why doesn't filewriter write anything to notepad?

I want to write a short message to the desktop in notepad, it doesn't work. It's still empty.

Public class Record{
public static void main(String[] args) {
try {
String data = "Получилось!";
FileWriter fWriter = new FileWriter("C:\\Users\\handi\\OneDrive\\Рабочий стол\\новый.txt");
fWriter.write(data);
}
catch ( IOException e ) {
e.printStackTrace();
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2020-10-15
@STVDI

fWriter.close()Forgot to add.
There is another better way (Java 7+):

try (FileWriter fWriter = new FileWriter("C:\\Users\\handi\\OneDrive\\Рабочий стол\\новый.txt")) {
            String data = "Получилось!";
            fWriter.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }

It's called try-with-resources

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question