java:IO流(缓冲流、对象流、控制台IO、转换流、java.io.File 类 )

目录

一、IO 流的结构体系

二、缓冲流:BufferedInputStream & BufferedOutputStream

三、对象流:ObjectInputStream & ObjectOutputStream

四、控制台IO

五、转换流 InputStreamReader & OutputStreamWriter

六、java.io.File 类 :


 

一、IO 流的结构体系

  • 字符流:用于操作文本文件 .txt .java
  • 字节流:用于操作非文本文件 .avi .mp3 .jpg

 

二、缓冲流:BufferedInputStream & BufferedOutputStream

处理流的一种,包装现有节点流,用于提高效率,内置缓冲区

//非文本文件的复制
@Test
public void test1(){
	//3. 创建 BufferedInputStream 的实例,包装对应的节点流,用于提高效率
	BufferedInputStream bis = null;
	//4. 创建 BufferedOutputStream 的实例,包装对应的节点流,用于提高效率
	BufferedOutputStream bos = null;
	try {
		//1. 创建 FileInputStream 的实例,同时打开指定文件
		FileInputStream fis = new FileInputStream("C:\\Users\\LI/Desktop/1.jpg");
		
		//2. 创建 FileOutputStream 的实例,同时打开指定文件
		FileOutputStream fos = new FileOutputStream("C:\\Users\\LI\\Desktop\\2.jpg");
		
		bis = new BufferedInputStream(fis);
		
		bos = new BufferedOutputStream(fos);
		
		//5. 通过 read(byte[] b)读取指定文件的内容
		byte[] b = new byte[1024];
		int len = 0; //接收实际读取的字节总数
		while((len = bis.read(b)) != -1){
			//6. 通过 write(byte[] b, int off, int len) 将读取的内容写到目标地点去
			bos.write(b, 0, len);
            //bos.flush();//强制清空换缓冲区,但是会影响效率
		}
	} catch (IOException e) {
		e.printStackTrace();
	} finally {

		//7. 关闭流
		if(bos != null){
			try {
				bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		if(bis != null){
			try {
				bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

 

 

三、对象流:ObjectInputStream & ObjectOutputStream

  1. 对象的序列化:将内存中的对象永久的以二进制形式保存到磁盘中。

    ①创建节点流的对象

    ②(可选)使用缓冲流包装现有节点流,用于提高效率

    ③创建对象流包装现有缓冲流,用于序列化

    ④通过 writeXxx() 方法 Xxx: 对应的数据类型,完成对象的序列化操作

    ⑤关闭流

    ⑥需要序列化对象的类必须实现 java.io.Serializable 接口(该类其他属性也必须实现该接口)

    ⑦需要提供一个序列号,保证如何序列化就如何反序列化

    public static final long serialVersionUID = 9239893899894L;

  2. 对象的反序列化:将磁盘中的数据进行读取

public class ObjectStreamTest {

	// 反序列化
	@Test
	public void test1() {
		ObjectInputStream ois = null;

		try {
			FileInputStream fis = new FileInputStream("./person.dat");
			BufferedInputStream bis = new BufferedInputStream(fis);
			ois = new ObjectInputStream(bis);

			List<Person> list = (List<Person>) ois.readObject();
			
			Iterator<Person> it = list.iterator();
			
			while(it.hasNext()){
				Person p = it.next();
				System.out.println(p);
			}
			
		} catch (ClassNotFoundException | IOException e1) {
			e1.printStackTrace();
		} finally {
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	// 序列化
	public static void main(String[] args) {
		Person p1 = new Person("张三", 18, 98.88);
		Person p2 = new Person("李四", 20, 100.22);
		Person p3 = new Person("王五", 38, 300.33);
		
		List<Person> list = new ArrayList<>();
		list.add(p1);
		list.add(p2);
		list.add(p3);

		ObjectOutputStream oos = null;
		try {
			FileOutputStream fos = new FileOutputStream("./person.dat");
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			oos = new ObjectOutputStream(bos);
			oos.writeObject(list);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (oos != null) {
				try {
					oos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

}

 

四、控制台IO

【了解】

  1. System.in : “标准”的输入流

  2. System.out : "标准"的输出流(System.setOut() 可以改变 println 方法的默认输出位置)

  3. System.err :"标准"的错误输出流

 

打印流:PrintStream & PrintWriter

 

五、转换流 InputStreamReader & OutputStreamWriter

编码:字符串 -> 字节数组   OutputStreamWriter

//编码
	@Test
	public void test2(){
		String str = "好好学习天天向上!";
		
		BufferedWriter bw = null;
		try {
			//注意:应该使用字符流操作文本文件,暂时为了演示转换流的用法,使用字节流操作文本文件
			FileOutputStream fos = new FileOutputStream("hello.txt");
			OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
			bw = new BufferedWriter(osw);
			
			bw.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(bw != null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

 

解码:字节数组 -> 字符串

	//解码
	@Test
	public void test3(){
		BufferedReader br = null;
		try {
			FileInputStream fis = new FileInputStream("hello.txt");
			InputStreamReader isr = new InputStreamReader(fis, "ISO8859-1");
			br = new BufferedReader(isr);
			
			String str = br.readLine();
			
			System.out.println(str);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 

六、java.io.File 类 :

描述文件或目录,用于操作文件或目录新建、删除、重命名等基本操作

若需要操作文件的内容,File 对象将无能为力,需要使用 IO 流

通常 File 对象与 IO 配合使用,将 File对象作为参数传递给 IO 流的构造器。

 

访问文件名:

  • getName() :获取文件名
  • getPath():获取相对路径
  • File getAbsoluteFile() :获取绝对路径的 File 对象
  • String getAbsolutePath() :获取绝对路径对应的字符串
  • getParent():获取父路径
  • renameTo(File newName) :重命名
  • file1.renameTo(file2) : file1 必须存在,file2 必须不存在

 

文件检测

  • exists() : 判断文件或目录是否存在
  • canWrite() : 判断是否可写
  • canRead() :是否可读
  • isFile() :判断是不是一个文件
  • isDirectory() :判断是不是一个目录

 

获取常规文件信息

  • lastModified() :获取最后修改时间对应的毫秒值
  • length():获取文件的大小,但是注意目录没有大小

 

文件操作相关

  • createNewFile() :创建一个“文件”
  • delete() :删除文件,若需要删除目录必须将目录中所有的内容全部删掉再删当前目录

 

目录操作相关

  • mkDir() :新建一个目录,如:e:/io/abc 若io包不存在则abc不能创建,若io存在则abc可以创建成功
  • mkDirs() :新建目录,如:e:/io/abc 若io包不存在则自动创建io包
  • String[] list():获取指定目录下所有的文件列表字符串名称
  • File[] listFiles():获取指定目录下所有文件和目录的 File 对象

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值