Java examples for File Path IO:File Channel
Write to a text file with FileChannel
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.EnumSet; public class Main { public static void main(String[] args) { Path path = Paths.get("C:/folder1/email", "test.txt"); ByteBuffer buffer = ByteBuffer.wrap("this is a test".getBytes()); try (FileChannel fileChannel = (FileChannel.open(path, EnumSet.of(StandardOpenOption.READ, StandardOpenOption.WRITE)))) { fileChannel.position(0);/*w w w. ja v a 2 s .c o m*/ fileChannel.write(buffer); } catch (IOException ex) { System.err.println(ex); } } }