IO流基础学习## IO流基础输入输出流,含文件复制综合案例
流的分类
1.根据传输处理的单位:字节流 字符流
2.根据流的方向:输入流 输出流
字节输入流
InputStream–抽象父级,无法实例化
FileInputStream–操作文件的字节输入流,构造要:File/路径
private static void method1() {
//1.创建流对象
FileInputStream f=null;
//InputStream i=new FileInputStream();
try {
// FileInputStream f=new FileInputStream(
// new File("D:\\Test\\ready\\1.txt"));
f=new FileInputStream("D:\\Test\\ready\\1.txt");
//2.使用流对象进行读取、
int b;
while ((b=f.read())!=-1){//只要读到的数据不是-1,就说明还有数据
System.out.println(b);//打印本轮读到的数据
}
} catch (Exception e) {
e.printStackTrace();
}finally {//一定会被执行到的代码
//3.关流
try {//关流操作也有可能出异常,所以要try catch
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
BufferedInputStream–高效字节输入流,构造要:InputStream
private static void method2() {
BufferedInputStream b=null;
try {
//1.创建流对象
b=new BufferedInputStream(
new FileInputStream("D:\\Test\\ready\\1.txt"));
//2.使用流对象
int a;
while ((a=b.read())!=-1){
System.out.println(a);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字节输出流
OutputStream–抽象父级,无法实例化
FileOutPutStream–操作文件字节输出流,构造函数有:(File),(File,boolean append), (String name)(String name,boolean append)
private static void method1() {
//1.定义在整个方法都生效的局部变量
FileOutputStream f = null;
try {
//创建流对象
//f=new FileOutputStream(new File("D:\\Test\\ready\\1.txt"));
//f=new FileOutputStream("D:\\Test\\ready\\1.txt");
//f=new FileOutputStream(new File("D:\\Test\\ready\\1.txt"),true);//追加,拼接
f = new FileOutputStream("D:\\Test\\ready\\1.txt", true);//追加,拼接
f.write(97);
f.write(98);
f.write(99);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedOutoutStream–高效字节输出流,构造要:OutputStream
private static void method2() {
BufferedOutputStream b = null;
try {
// b=new BufferedOutputStream(
// new FileOutputStream(new File("D:\\Test\\ready\\1.txt")));
b = new BufferedOutputStream(new FileOutputStream("D:\\Test\\ready\\1.txt",true));//true,追加输出
b.write(100);
b.write(100);
b.write(100);
b.write(100);
b.write(100);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输入流
Reader–抽象父级,无法实例化
FileReader–操作文件的字符输入流,构造要:File:/路径
private static void method1() {
FileReader f=null;
try {
//1.创建流对象
//FileReader f=new FileReader(new File("D:\\Test\\ready"))
f= new FileReader("D:\\Test\\ready\\1.txt");
//2.使用流对象
int b;
while ((b=f.read())!=-1){
System.out.println(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
BufferedReader–高效字符输入流,构造要:Reader
private static void method2() {
//1.定义在整个方法中都生效的局部变量
BufferedReader b = null;
//2.由于IO操作可能会发生多种异常,所以要先完成try-catch-finally结构
try {
//3.创建流对象
//b = new BufferedReader(new FileReader(new File("D:\\Test\\ready\\1.txt")));
b=new BufferedReader(new FileReader("D:\\Test\\ready\\1.txt"));
//4.使用流对象
int a;
while ((a=b.read())!=-1){
System.out.println(a);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
字符输出流
Writer–抽象父级,无法实例化
FileWriter–操作文件的字符输出流,构造函数有:(File)(File,boolean append) (String name) (String name,boolean append)
private static void method1() {
FileWriter f = null;
try {
//f=new FileWriter(new File("D:\\Test\\ready\\1.txt"));
f = new FileWriter("D:\\Test\\ready\\1.txt", true);//true,追加输出
f.write(97);
f.write(97);
f.write(97);
f.write(97);
f.write(97);
f.write(97);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Bufferedwriter–高效字符输出流,构造要:writer
private static void method2() {
BufferedOutputStream b = null;
try {
b = new BufferedOutputStream(new FileOutputStream("D:\\Test\\ready\\1.txt", true));
b.write(99);
b.write(99);
b.write(99);
b.write(99);
b.write(99);
b.write(99);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
IO流文件复制综合案例
public class TestCopyFile {
public static void main(String[] args) {
//1.提示并接收用户输入的两个路径
System.out.println("请输入源文件路径");
String t=new Scanner(System.in).nextLine();//被复制的文件
System.out.println("请输入新文件的路径");
String f=new Scanner(System.in).nextLine();//复制好的新文件
//2.调用创建好的自定义方法,完成文件的复制
//ZFCopy(t,f);//使用字符流完成复制操作
ZJCopy(t,f);//使用字节流完成复制操作
}
private static void ZJCopy(String t, String f) {
//1.定义在本方法中都生效的局部变量
BufferedInputStream in=null;//高效字节输入流,用于读取
BufferedOutputStream out=null;//高效字节输出流,用于写出
try {
in=new BufferedInputStream(new FileInputStream(t));//创建一个高效字节输入流对象,用于读取源文件
out=new BufferedOutputStream(new FileOutputStream(f));//创建一个高效字节输出流,用于向新文件输出数据
int b;
while ((b=in.read())!=-1){
out.write(b);
}
System.out.println("复制成功");
}catch (Exception e){
System.out.println("复制失败");
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//字符流复制操作
private static void ZFCopy(String t, String f) {
//1.定义两个在本方法中都生效的字符流
BufferedReader in=null;//高效字符输入流,用于读取
BufferedWriter out=null;//高效字符输出流,用于写出
try {
//3.创建高效字符输入流对象,用于读取源文件内容
in=new BufferedReader(new FileReader(t));
//4.创建高效字符输出流对象,用于将读到的数据写出到新文件中
out=new BufferedWriter(new FileWriter(f));
//5.使用流对象完成复制操作
//5.1定义变量用来保存读到的数据
int a;
//5.2循环读取源文件
while ((a=in.read())!=-1){
//5.3将本轮读到的数据,写出到新文件中,读一个写一个
out.write(a);
}
System.out.println("复制成功");
}catch (Exception e){
System.out.println("复制失败");
e.printStackTrace();
}finally {
/*关流是有顺序的,如果有多个流,最后创建的流,最先关闭
* 多条关流语句需要各自try-catch*/
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}