Java examples for File Path IO:File Channel
Query the position using the SeekableByteChannel
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; public class Main { public static void main(String[] args) throws IOException { int bufferSize = 8; Path path = Paths.get("/home/docs/users.txt"); ////from w w w. j a v a2s. com final String newLine = System.getProperty("line.separator"); try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.WRITE)) { ByteBuffer buffer; long position = sbc.size(); sbc.position(position); System.out.println("Position: " + sbc.position()); buffer = ByteBuffer.wrap((newLine + "Paul").getBytes()); sbc.write(buffer); System.out.println("Position: " + sbc.position()); buffer = ByteBuffer.wrap((newLine + "Carol").getBytes()); sbc.write(buffer); System.out.println("Position: " + sbc.position()); buffer = ByteBuffer.wrap((newLine + "Fred").getBytes()); sbc.write(buffer); System.out.println("Position: " + sbc.position()); } } }