package com.hm.cn;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class InputStreamTest {
/**
* @throws IOException
* @Description:字节流练习,将F盘中1.txt中文本复制到2.txt中
*/
public static void main(String[] args){
InputStream is = null;
OutputStream ops = null;
try {
is = new FileInputStream("F:1.txt");
ops = new FileOutputStream("F:2.txt");
int b;
while(true){
b = is.read();//每次读取一个字节
if(b==-1){
break;
}else{
ops.write(b);
}
}
} catch (FileNotFoundException e) {//捕获找不到文件的异常
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();//IO异常
} finally{
try {
ops.flush();//刷新此输出流并强制写出所有缓冲的输出字节
is.close();//关闭输入流
ops.close();//关闭输出流
System.out.println("成功!2.txt文件已经建立并将1.txt中内容复制到2.txt中");
} catch (IOException e){
e.printStackTrace();
}
}
}
}
|
|