新io练习
刚刚复习了一下java新io,备份一下!
这个是手动写通过管道来复制文件
public class ChannelCopy { private static final int BSIZE = 1024; /** * @param args the command line arguments */ public static void main(String[] args) { try { File file = new File("D:/dta.txt"); File daf = new File("D:/data.txt"); FileChannel in = new FileInputStream(file).getChannel(); FileChannel out = new FileOutputStream(daf).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BSIZE); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } } catch (Exception ex) { Logger.getLogger(ChannelCopy.class.getName()).log(Level.SEVERE, null, ex); } } }
效率更高的方式:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.channels.FileChannel; import java.util.logging.Level; import java.util.logging.Logger; public class ChannelCopy { private static final int BSIZE = 1024; /** * @param args the command line arguments */ public static void main(String[] args) { try { File file = new File("D:/dta.txt"); File daf = new File("D:/data.txt"); FileChannel in = new FileInputStream(file).getChannel(); FileChannel out = new FileOutputStream(daf).getChannel(); in.transferTo(0, in.size(), out); //or // out.transferFrom(in, 0, in.size()); } catch (Exception ex) { Logger.getLogger(ChannelCopy.class.getName()).log(Level.SEVERE, null, ex); } } }
ByteBuffer练习:
public class App { public static void main(String[] args) throws IOException { //write a file File file = new File("D:/dta.txt"); System.out.println("file is exist:"+file.exists()); FileChannel fc = new FileOutputStream(file).getChannel(); fc.position(fc.size()); fc.write(ByteBuffer.wrap("Some more 123456".getBytes())); fc.close(); //add to the end of the file fc=new RandomAccessFile(file, "rw").getChannel(); fc.position(fc.size());//move to the end fc.write(ByteBuffer.wrap("TestSome more".getBytes())); fc.close(); //read the file fc=new FileInputStream(file).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(1024); fc.read(buffer); buffer.flip(); while(buffer.hasRemaining()){ System.out.println((char)buffer.get()); } } }
设置编码格式:
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; import java.util.logging.Level; import java.util.logging.Logger; public class BufferToText { private static final int BSIZE = 1024; /** * @param args the command line arguments */ public static void main(String[] args) { try { File file = new File("D:/dta.txt"); File fileOut = new File("D:/data.txt"); FileChannel fc = new FileOutputStream(fileOut).getChannel(); fc.write(ByteBuffer.wrap("Some text".getBytes())); fc.close(); fc = new FileInputStream(fileOut).getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BSIZE); fc.read(buffer); buffer.flip(); //Doesn't work,这里打印出来出现乱码:卯浥?數 System.out.println(buffer.asCharBuffer()); //Decode using this System's default Charset buffer.rewind(); String encoding = System.getProperty("file.encoding"); System.out.println("Decoded using "+encoding+" : "+Charset.forName(encoding).decode(buffer)); //Or,we could encode with something that will print fc = new FileOutputStream(fileOut).getChannel(); fc.write(ByteBuffer.wrap("Some text 1234".getBytes("UTF-16BE"))); fc.close(); //Now try reading again fc= new FileInputStream(fileOut).getChannel(); buffer.clear(); fc.read(buffer); buffer.flip(); System.out.println(buffer.asCharBuffer()); //Use a CharBuffer to write through fc= new FileOutputStream(fileOut).getChannel(); buffer = ByteBuffer.allocate(24); buffer.asCharBuffer().put("Some text ttererer"); fc.write(buffer); fc.close(); //Read and display fc= new FileInputStream(fileOut).getChannel(); buffer.clear(); fc.read(buffer); buffer.flip(); System.out.println("Read and display"); System.out.println(buffer.asCharBuffer()); } catch (Exception ex) { Logger.getLogger(BufferToText.class.getName()).log(Level.SEVERE, null, ex); } } }