采用字节流的方式对文件进行读取写入,首先将文件内容读入字节数组buffer,再将buffer里的内容写入文件,从而实现文件的复制粘贴。
public class OpenFile {
public static void main(String
[] args
) throws IOException
{
try{
FileInputStream rf
= new FileInputStream("D:\\myjava\\test.txt");
int n
=512,c
=0;
byte buffer
[] = new byte[n
];
FileOutputStream wf
= new FileOutputStream("D:\\myjava\\test1.txt");
while((c
=rf
.read(buffer
,0,n
))!= -1){
wf
.write(buffer
,0,n
);
}
rf
.close();
wf
.close();
System
.out
.println("Save to test1.txt!");
}catch(IOException e
){
System
.out
.println(e
);
}
}
}
转载请注明原文地址: https://yun.8miu.com/read-106902.html