I would like to add more text to an existing text file in Java? When i try to add more text it just replaces the existing text.
Asked
Active
Viewed 64 times
1 Answers
0
This will append mycontent
to the outputFile
:
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
public class Test {
public static void main(String[] args) {
try {
String content = "mycontent";
File outputFile = new File("PATH/test.txt");
Files.write(outputFile.toPath(), content.getBytes(), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Available since Java 1.7.

flavio.donze
- 7,432
- 9
- 58
- 91
-
-
-
-
I use jdk1.8.0_77, and I run it from Eclipse. What Java version are you on? – flavio.donze Apr 21 '16 at 08:27