|
import java.nio.*; import java.nio.channels.FileChannel; public class Try3 { public static void main(String[] args) { int[] array={1,2,3,4,5,6,7,8,9}; String str="weidongping"; double dbl=100.0; File aFile=new File("e:/jf/wdp1/file.text"); FileOutputStream file=null; try{ file=new FileOutputStream(aFile); }catch(FileNotFoundException e){System.out.println("Usage:FileNotFoundException is catched");} FileChannel channel=file.getChannel(); ByteBuffer[] bytBuff=new ByteBuffer[3]; bytBuff[0]=bytBuff[0].allocate(8); bytBuff[1]=bytBuff[1].allocate(8); bytBuff[0].put(str.getBytes()).flip(); bytBuff[1].putDouble(dbl); bytBuff[2].allocate(str.length()); bytBuff[2].put(str.getBytes()).flip(); try{ file.write(bytBuff); }catch(IOException e){System.out.println("write file error"); } bytBuff[0].clear(); bytBuff[1].clear(); bytBuff[2].clear(); try{ file.close(); }catch(IOException e ){ } } } 编译错误提示: write()方法错误,这是怎么回事?????谢谢啦!!!1 |
|
|
|
FileOutputStream.write(byte[])
|
|
|
void FileOutputStream.write(byte[] b) 而不是write (java.nio.ByteBuffer[])
比如file.write(bytBuff[0].array()); 就可以罗 ! |
|
|
好像书上说有个 FileOutputStream.write(ByteBuffers[] buffers) 方法啊,它是将多个通道里的数据一起写入到一个文件中去!!
|
|