java.io与java.nio

java.io: 包装

BufferedInputStream bin = new BufferedInputStream(new FileInputStream(new File("D:/11.txt")));  

java.nio:信道

public static void main(String[] args) throws Exception {  
        // 写文件  
        FileChannel fc = new FileOutputStream("data.txt").getChannel();  
        fc.write(ByteBuffer.wrap("Some text ".getBytes()));  
        fc.close();  
        // 在文件尾写入  
        fc = new RandomAccessFile("data.txt", "rw").getChannel();  
        fc.position(fc.size()); // 移到文件尾  
        fc.write(ByteBuffer.wrap("Some more".getBytes()));  
        fc.close();  
        // 读文件  
        fc = new FileInputStream("data.txt").getChannel();  
        ByteBuffer buff = ByteBuffer.allocate(1024);  
        fc.read(buff);  
        buff.flip();  
        while (buff.hasRemaining())  
            System.out.print((char) buff.get());  
}

你可能感兴趣的:(java)