Java tutorial
import java.io.File; import java.nio.channels.FileChannel; import java.io.IOException; import java.nio.ByteBuffer; import java.io.FileOutputStream; public class Main { public static void main(String[] args) { File outputFile = new File("test.txt"); try (FileChannel fileChannel = new FileOutputStream(outputFile).getChannel()) { String text = getText(); byte[] byteData = text.toString().getBytes("UTF-8"); ByteBuffer buffer = ByteBuffer.wrap(byteData); fileChannel.write(buffer); } catch (IOException e1) { e1.printStackTrace(); } } public static String getText() { String lineSeparator = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder(); sb.append("test"); sb.append(lineSeparator); sb.append("test"); sb.append(lineSeparator); sb.append("test"); sb.append(lineSeparator); sb.append("test"); return sb.toString(); } }