V
V
Viktor Zorin2017-11-01 21:19:14
Java
Viktor Zorin, 2017-11-01 21:19:14

How to write an integer to a text file?

I am programming in Java. I cannot write the value of a variable of type Int to a text file. I used the DataOutputStream class in which I nested the FileOutputStream

DataOutputStream writer = new DataOutputStream(new FileOutputStream("D:\\Eclipse\\workspace\\MyExperimentalProject\\files\\output.txt"));

then output the value of the variable to a file writer.writeInt(a);
As a result, the file still turns out to be empty! help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
D3lphi, 2017-11-01
@FutureAnge1

Read what the DataOutputStream.writeInt() method does:
An integer can be written to a file like this:

try (FileWriter fileWriter = new FileWriter("output.txt")) {
    fileWriter.write(String.valueOf(5));
    fileWriter.flush();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question