1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| import java.io.File; import java.io.FileOutputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel;
//主类 //Function : FileChannel_demo public class FileChannel_demo {
public static void main(String[] args) throws Exception{ // TODO 自动生成的方法存根 String info[] = {"123","456","789"}; File f = new File("/home/common/software/coding/HelloWord/HelloWord/out.txt");//路径 FileOutputStream output = null; output = new FileOutputStream(f); FileChannel fout = null; //声明输出的通道 fout = output.getChannel(); //得到输出的文件通道 ByteBuffer buf = ByteBuffer.allocate(1024); //开辟缓冲 for(int i=0;i<info.length;i++){ buf.put(info[i].getBytes()); } buf.flip(); //重设缓冲区,准备输出 fout.write(buf); //输出 fout.close(); output.close(); }
}
|