Java how to write to txt filerel:ot refautturi.comimport java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;
import java.nio.file.StandardOpenOption;
/**
* @author lautturi.com
* Java example:
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) throws IOException {
try
{
String str = "Hello";
Path path = Paths.get("F:/", "temp", "test.txt");
byte[] strToBytes = str.getBytes();
//Write content to file in java
Files.write(path, strToBytes);
//Verify file content
String read = Files.readAllLines(path).get(0);
System.out.println(read);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}