Java - IO流

IO流

1 概述

IO流的作用是实现两个设备之间数据的传递,设备包括磁盘(硬盘)、内存、键盘、文件、网络和控制台,其中网络表示当前主机之外的网上资源

2 分类

IO流根据操作的方式可以分为输入流和输出流,根据数据的类型可以分为字节流和字符流

2.1 字节流

概述
字节流传输的是字节,可以操作任意类型的数据,如音频、视频、文件、图片等
InputStream、OutputStream
FileInputStream、FileOutputStream
(1)将数据写入磁盘
public static void write() {
		try {
			//1. 创建字节输出流并关联文件
			FileOutputStream fileOutputStream = new FileOutputStream("test1.txt", true);//true:追加而不是清空覆盖
			//2. 写--这里只能写字节
			fileOutputStream.write("Hello world, I Love You!\n".getBytes());
			//3. 关闭资源
			fileOutputStream.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
(2)读1:一次读一个字节
public static void read1() {
		try {
			//1. 创建字节输入流并关联文件
			FileInputStream fileInputStream = new FileInputStream("test1.txt");
			//2. 读
			int num = 0;
			while((num = fileInputStream.read()) != -1) {
				System.out.print((char)num);
			}
			//3. 关闭资源
			fileInputStream.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
(3)读2:一次读多个字节
public static void read2() {
		try {
			FileInputStream fileInputStream = new FileInputStream("test1.txt");
			
			int num = 0;
			byte byt[] = new byte[10];
			while((num = fileInputStream.read(byt)) != -1) {
				System.out.print(new String(byt, 0, num));
			}
			
			fileInputStream.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
(4)读3:一次全部读完
public static void read3() {
		try {
			FileInputStream fileInputStream = new FileInputStream("test1.txt");
			
			//获取文件的字节个数
			//备注:这种方式适合文件的字节数比较小的时候,大概是几kb之内
			int num1 = fileInputStream.available();
			
			byte[] byt = new byte[num1];
			fileInputStream.read(byt);
			System.out.print(new String(byt));
			
			fileInputStream.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
(5)使用字节流实现图片的复制
//使用字节流i实现图片的复制
	public static void test01() {
		try {
			//1. 创建字节输入流
			FileInputStream fileInputStream = new FileInputStream("1.jpg");
			//2. 创建字节输出流
			FileOutputStream fileOutputStream = new FileOutputStream("2.jpg");
			//3. 进行读写
			int num = 0;
			while((num = fileInputStream.read()) != -1) {
				fileOutputStream.write(num);
			}
			
			//4. 关闭资源
			fileInputStream.close();
			fileOutputStream.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}

2.2 字符流

1 概述
字符流传输的是字节,不同点是在传输过程中加入了编码的操作,可以更方便的操作文本
2 Reader、Writer
3 FileWriter
3.1 FileWriter练习
package hope;

import java.io.*;

public class _FileWriter {
	public static void main(String args[]) throws IOException {
		//1. 创建FileWriter对象并关联相应文件
		FileWriter fileWriter = new FileWriter("test1.txt");
		
		//2. 写
		fileWriter.write("Hello World! Hello Hope!");
		
		//3. 刷新
		fileWriter.flush();
		
		//4. 关闭流(关闭&刷新)
		fileWriter.close();
	}
}

注意点:
(1)创建对象时,如果只写了文件的名字,没有写具体路径,则默认路径时当前的工程
(2)创建对象时,如果关联的文件已经存在,则覆盖掉源文件,如果不存在,则程序会自动创建要给新的
(3)创建对象时,如果指定的路径并不是真实存在的,则会报异常(FileNotFoundException),表示系统找不到指定路径
(4)执行write方法时,数据临时存放到流对象的内部字节数组中,会默认去查编码表
(5)刷新时,会将临时字节数组中的数据放入磁盘
(6)关闭流时,不仅关闭流,并且会进行刷新操作
(7)如果关闭流之后,在进行写操作,会报异常(Stream closed)

3.2 FileWriter-文件续写
package hope;

import java.io.*;

/**
 * 
 * 文件的读写:FiileWriter(String file, boolean value)
 *当value位是true时,表示不会将原来的内容覆盖掉,会接着写
 */

public class _FileWriter_02 {
	public static void main(String args[]) throws IOException{
		//1. 创建
		FileWriter fileWriter = new FileWriter("test1.txt", true);
		
		//2. 写
		fileWriter.write("Hello World!\n");
		
		//3. 刷新
		fileWriter.flush();
		
		//4. 关闭
		fileWriter.close();
	}
}

4 FileReader
4.1 FileReader练习—从文件中读取数据,打印到控制台
package _FileReader;

import java.io.*;

/**
 * 
 * 读内容-FileReader
 * 实例:从文件中读取内容并显示在控制台
 *
 */

public class _FileReader_01 {
	public static void main(String args[]) {
		test01("test1.txt");
		test02("test1.txt");
	}
	//read();//一个字符一个字符的读,每次输出一个字符
	public static void test01(String path) {
		try {
			FileReader fileReader = new FileReader(path);
			
			int num = 0;
			//num = fileReader.read();//返回的是ASCII码值
			while((num = fileReader.read()) != -1) {
				System.out.print((char)num);
			}
			
			fileReader.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
	//read(数组);//每次读出多个字符(剩余数组长度 == 数组长度? “数组长度”:“剩余数组长度”)
	//数组负责临时存放数据,一般这个数组的大小<=!kB
	//返回值表示本次读到的真实字符个数,如果返回值是-1,则表示已读完
	public static void test02(String path) {
		try {
			FileReader fileReader = new FileReader(path);
			
			char[] arr = new char[4];
			int num = 0;
			while((num = fileReader.read(arr)) != -1) {
				//System.out.println(new String(arr, 0, num) + " " + num);
				System.out.print(new String(arr, 0, num));
			}
			
			fileReader.close();
		}
		catch(IOException e) {
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值