一 复制文件采用 BufferedReader和 BufferedWriter 作为缓存标签:java,String,BufferedWriter,复制,bw,new,close,txt,图片 From: https://blog.51cto.com/u_15862653/5828937
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br=
new BufferedReader(new FileReader("c:\\2.txt"));
String b = null;
//写入文件
BufferedWriter bw =
new BufferedWriter(new FileWriter("c:\\c.txt"));
//每次读一行 对于读取 txt或者html 等有换行符是可以的 while ((b = br.readLine()) != null) {
bw.write(b);//输出字符串
bw.newLine();
//将缓存写入文件
bw.flush();
}
br.close();
bw.close();
}
二 复制图片
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis = new FileInputStream("c:\\a.jpg");
FileOutputStream fos = new FileOutputStream("c:\\b.jpg");
byte[] buffer = new byte[2048];
int len =0;
while((len=fis.read(buffer))>0){
fos.write(buffer, 0, len);
}
fis.close();
fos.close();
}