黑马程序员_java基础之IO(输入,输出流)

------- android培训java培训、期待与您交流! ----------

1.IO流

1.1分类

流的方向分类:
输入流: 读数据
输出流: 写数据
流的类型分类:
字节流:
字节输入流:InputStream(此抽象类是表示字节输入流的超类)
 FileInputStream 基本的字节输入流
 BufferedInputStream 高效的字节缓冲输入流
字节输出流: OutputStream
FileOutputStream 基本的字节输出流(此抽象类是表示字节输出流的超类)
 BufferdOutputStream 高效的字节缓冲输出流

字符流:
字符输入流: Reader(此抽象类是表示字符输入流的超类)
 转换流 InputStreamReader 可以完成解码的操作
 FileReader 对文件进行读取的便捷类
 BufferedReader 高效的字符缓冲输入流
字符输出流: Writer(此抽象类是表示字符输出流的超类)
 转换流 OutputStreamWriter 可以完成编码的操作
 FileWriter 对文件进行写入的便捷类
 BufferedWriter 高效的字符缓冲输出流

1.2字节流

写数据的方式4种
基本的流,一次一个字节
基本的流,一次一个字节数组
高效的流,一次一个字节
高效的流,一次一个字节数组
读数据的方式4种
基本的流,一次读一个字节
基本的流,一次读一个字节数组
高效的流,一次读一个字节
高效的流,一次读一个字节数组

1.3字符流

写数据的方式5种
一次一个字符
一次一个字符数组
一次一个字符数组一部分
一次一个字符串
一次一个字符串一部分
特殊方式:
通过BufferedWriter 高效的流
newLine() 写一个换行符

读数据的方式2种
一次一个字符
一次一个字符数组
特殊方式
通过BufferedReader 高效的流
readLine() 读取一行字符串

1.4FileOutputStream

构造方法:
public FileOutputStream(File file) 通过File对象 , 创建 文件字节输出流对象
public FileOutputStream(String name)通过文件的字符串路径 , 创建 文件字节输出流对象
FileOutputStream构造方法如果参数append 设置为true, 那么,会在原有数据的基础上,追加新的数据:
public FileOutputStream(File file, boolean append)
public FileOutputStream(String name, boolean append)
写数据的方法 public abstract void write(int b)
BufferedOutputStream: 字节缓冲输出流
特点: 在字节输出流的基础上,内部包含了一个用来缓冲数据的数组
作用: 为基本的字节输出流,提高效率,而产生的高效流,那么这个高效流的底层还是通过的流 完成的操作
  构造方法:  public BufferedOutputStream(OutputStream out)采用默认大小的缓冲区8192个字节,创建字节缓冲输出流对象
 public BufferedOutputStream(OutputStream out, int size) 指定缓冲区大小,创建自己而缓冲输出流对象

1.5FileInputStream

构造方法:
public FileInputStream(File file) 通过File对象, 得到一个字节输入流对象
public FileInputStream(String name) 通过文件的字符串路径,得到一个字节输入流对象
读取数据的方式
InputStream: 方式1: public abstract int read() 一次读取一个字节
方式2:public int read(byte[] b) 一次读取一个字节数组
BufferedInputStream: 字节缓冲输入流
特点: 把一个基本的流,进行了高效的包装,自身包含了一个缓冲数组
构造方法: public BufferedInputStream(InputStream in)
public BufferedInputStream(InputStream in, int size)
1.6四种方式完成avi文件复制
package cn.IOtest_4method;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FourMethodAvi {
	public static void main(String[] args) throws IOException {
		//method1("20.14_IO.avi", "Copy1.avi");
		//method2("20.14_IO.avi", "Copy3.avi");
		//method3("20.14_IO.avi", "Copy3.avi");
		method4("20.14_IO.avi", "Copy4.avi");
	}
			
	private static void method4(String src, String dest) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
		byte[] bys = new byte[1024];
		int len = -1;
		while((len = bis.read(bys)) != -1){
			bos.write(bys, 0, len);
		}
		bos.close();
		bis.close();
	}

	private static void method3(String src, String dest) throws IOException {
		FileInputStream bis = new FileInputStream(src);
		FileOutputStream bos = new FileOutputStream(dest);
		byte[] bys = new byte[1024];
		int len = -1;
		while((len = bis.read(bys)) != -1){
		bos.write(bys, 0, len);
		}
		bis.close();
		bos.close();
	}

	private static void method2(String src, String dest) throws IOException {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
		int ch = -1;
		while((ch = bis.read()) != -1){
			bos.write(ch);
		}
		bis.close();
		bos.close();
	}

	public static void method1(String src , String dest) throws IOException{	
		FileInputStream fis = new FileInputStream(src);
		FileOutputStream fos = new FileOutputStream(dest);
		int ch = -1;
		while((ch = fis.read()) != -1){
			fos.write(ch);
		}
		fos.close();
		fis.close();
	}
}

2.打印流

打印流: 只要输出操作,不能输入数据可以输出任意类型的数据
分类: 字节打印流: PrintStream
字符打印流:PrintWriter
 特点:如果启用了自动刷新,则只有在调用 println、printf 或 format 可以完成数据的自动刷新
 构造方法: PrintWriter(File file)
  PrintWriter(String filename)
PrintWriter(OutputStream out)
PrintWriter(Writer out)
  实现自动刷新的构造方法: PrintWriter(OutputStream out, boolean autoFlush)
  PrintWriter(Writer out, boolean autoFlush)
public class PrintWriterDemo {
	public static void main(String[] args) throws IOException {
		//创建打印流对象
		PrintWriter pw = new PrintWriter(new FileWriter("pw.txt"), true);//开启自动刷新
		//写数据
		pw.println(3.14);
		//关闭
	}
}

3.print() 与 println()方法的区别

print方法:
 方法必须有参数
不能进行换行
  如果开启了自动刷新功能,不能实现数据的自动刷新
println方法:
  方法可以没有参数
  可以实现换行
如果开启了自动刷新功能,能实现数据的自动刷新

4.序列化流和反序列化流

ObjectOutputStream 就是把 对象 存储到流中
 ObjectInputStream 就是从流中 读取对象
注意的:无论使用的是序列化流还是反序列化流,都需要当前对象先进行序列化操作

  java.io.NotSerializableException: cn.itcast_06_SerializableStream.Person NotSerializableException - 某个要序列化的对象不能实现 java.io.Serializable 接口
说明当前Person类 没有实现序列化操作, 需要实现序列化
  public interface Serializable
类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
序列化接口没有方法或字段,仅用于标识可序列化的语义
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class SerializableStreamDemo {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//write();
		
		read();
	}

	//通过反序列化流,从流中 读取Person对象
	private static void read() throws IOException, ClassNotFoundException {
		//创建 对象输入流
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("序列化.txt"));
		//从流中读取对象
		Object obj = ois.readObject();
		System.out.println(obj.toString());
		//关闭流对象
		ois.close();
	}

	//通过序列化流,把Person对象写入到流中
	private static void write() throws IOException {
		//创建  对象输出流
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("序列化.txt"));
		//写对象到流中
		Person p = new Person("虎哥", 20);
		oos.writeObject(p);
		//关闭流对象
		oos.close();
	}
}

import java.io.Serializable;
public class Person implements Serializable {
	/**
	 * 序列化版本号
	 */
	private static final long serialVersionUID = 4553866630186546227L;
	private String name;
	//private int age;
	transient int age;//把年龄不参与序列化操作

	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
}

5.多层文件夹的复制

package IOEncodeBufferedTest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//"第一次班会资料"---> Copy
public class BigTestDemo {
public static void main(String[] args) throws IOException {
	File srcPath = new File("第一次班会资料");
	File destPath = new File("Copy");
	
	copyMothed(srcPath,destPath);
}

private static void copyMothed(File srcPath,File destPath) throws IOException {
	destPath.mkdir();
	File[] file = srcPath.listFiles();
	for (File file2 : file) {
		if (file2.isDirectory()){
			File dest = new File(destPath, file2.getName());
			copyMothed(file2, dest);
		}else {
			File dest = new File(destPath,file2.getName());
			copy(file2,dest);
		}
	}
	
}

private static void copy(File file2, File dest) throws IOException {
	FileInputStream fis = new FileInputStream(file2);
	FileOutputStream fos = new FileOutputStream(dest);
	int ch = -1;
	while((ch = fis.read()) != -1){
		fos.write(ch);
	}
	fis.close();
	fos.close();
}
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值