Java学习笔记——学习IO流

输入 input :输入到java程序
输出 output :java程序输出

分类:
按照传输内容分:字节流、字符流
按照传输方向分:输入流、输出流

字节流

主要是传输字节(byte)数据,例如:音视频、图片等二进制文件

输入字节流

InputStream 是 字节输入流的所有类的超类。是抽象类

FIleInputStream 是 字节输入流的常用类

构造方法:

  1. FileInputStream(String name) :通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
  2. FileInputStream(File file) :通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。

演示:

public void testConstructor() throws FileNotFoundException{
	//构造方法1 : 传入String类型的文件路径
	FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
	//构造方法2 :传入File类型的文件对象参数
	FileInputStream fis2 = new FileInputStream(new File("E:/A/B2/a.txt"));
}

常用方法:
public int read([byte[] b[, int off, int len]]) throws IOException :

  1. 无参数时,从此输入流中读取一个数据字节。
  2. 有参数b时,从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
  3. 有全部参数时,从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。b - 存储读取数据的缓冲区;off - 目标数组 b 中的起始偏移量;len - 读取的最大字节数。

读取字节:

演示:

//读取一个字节
public void testInput() throws IOException {
	FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
	//读取一个字节
	int b1 = fis1.read();
	System.out.println((char)b1);
	//读取一个字节
	int b2 = fis1.read();
	System.out.println((char)b2);
	//读取一个字节
	int b3 = fis1.read();
	System.out.println((char)b3);
}
public void testInputPlus() throws IOException {
	FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
	try{
		while((int b = fis1.read()) != -1){
			System.out.print((char)b);
		}
	}finally{
		fis1.close();//关流
	}
}
//读取字节数组
public void testInputArray() throws IOException {
	FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
	//定义一个数组,长度是文件字节大小,如果长度小于字节大小时,每次读入数组长度
	byte[] arr = new byte[10];
	try{
		//int num = fis1.read(byte[] b);num是字节的个数
		while((int b = fis1.read(arr)) != -1){
			System.out.print("读入的字节个数为:" + b);
		}
		System.out.println(Array.toString(arr));
	}finally{
		fis1.close();//关流
	}
}
public void testInputArrayPlus() throws IOException {
	FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
	byte[] arr = new byte[3];
	try{
		while((int b = fis1.read(arr,0,arr.length)) != -1){
			System.out.print("读入的字节个数为:" + b);
		}
		System.out.println(Array.toString(arr));
	}finally{
		fis1.close();//关流
	}
}
public void testAvailable() throws IOException {
	FileInputStream fis1 = new FileInputStream("E:/A/B2/a.txt");
	int available = fis1 .available();//返回文件字节数
	System.out.println(available);
}

输出字节流

OutputStream 输出字节流,抽象类.
常用实现类 FileOutputStream 字节输出流.

构造方法:

  1. FileOutputStream(File file) :创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
  2. FileOutputStream(File file, boolean append) :创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
  3. FileOutputStream(String name) :创建一个向具有指定名称的文件中写入数据的输出文件流。
  4. FileOutputStream(String name, boolean append) :创建一个向具有指定 name 的文件中写入数据的输出文件流。

注意:

  1. OutputStream 创建对象时,如果没有该文件,会创建一个新的,然后再写入
  2. 如果构造方法不传第二个参数时,默认就是false,即不和该文件原来的内容拼接,会将原来的内容覆盖
  3. 如果构造方法指定append为true,会和该文件原来的内容拼接
public void testConstructor() throws FileNotFoundException{
	
	//构造方法1
	FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt",true);
	//构造方法2
	FileOutputStream fos2 = new FileOutputStream((new File("E:/A/B2/a.txt"));
}

常用方法:
public void write(int b || [byte[] b[, int off, int len]]) throws IOException :

  1. 参数是“int b”时,将指定字节写入此文件输出流。
  2. 参数是“byte[] b”时,将 b.length 个字节从指定 byte 数组写入此文件输出流中。
  3. 参数是“byte[] b, int off, int len”时,将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
public void addDate() throws Exception{
	FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt");
	//输出一个字节
	fos1.write(97);
	fos1.close();
}
public void addArray() throws Exception{
	FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt",true);
	byte[] bytes = {66,67,68,69,70};
	//输出一个数组内容的数据
	fos1.write(bytes);
	fos1.close();
}
public void addArrayPlus() throws Exception{
	FileOutputStream fos1 = new FileOutputStream("E:/A/B2/a.txt",true);
	byte[] bytes = {71,72,73,74,75};
	//输出一个数组内容的数据
	/**
	 *参数1 bytes   存储数据的数组
	 *参数2 偏移量  从数组的那个下标开始
	 *参数3 长度    读取数据的个数
	 */
	fos1.write(bytes,1,3);
	fos1.close();
}

缓冲字节输入流(BufferedInputStream)

在创建 BufferedInputStream 时,会创建一个内部缓冲区数组

public BufferedInputStream(InputStream in, int size) :创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。创建一个长度为 size 的内部缓冲区数组并将其存储在 buf 中。 可以不指定size,默认为8K。

public void test1() throws Exception{
	FileInputStream fis = new FileInputStream("E:/A/B2/a.txt");
	BufferedInputStream bis = new BufferedInputStream(fis);
	while((int b = bis.read())!=-1){
		System.out.println(b);
	}
	bis.close();
}

缓冲字节输出流(BufferedOutputStream)

public BufferedOutputStream(OutputStream out, int size) :创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。

public void flush() throws IOException :刷新此缓冲的输出流。这迫使所有缓冲的输出字节被写出到底层输出流中。可以不指定size,默认为8K。

public void test1() throws Exception{
	FileOutputStream fos = new FileOutputStream("E:/A/B2/a.txt");
	BufferedOutputStream bos = new BufferedOutputStream(fis);
	while((int b = bos.read())!=-1){
		System.out.println(b);
	}
	fos.close();
	bos.close();
}

字符流

输入字符流

Reader抽象类,FileReader实现类

构造方法:

  1. public FileReader(File file) throws FileNotFoundException :在给定从中读取数据的 File 的情况下创建一个新 FileReader。
  2. public FileReader(String fileName) throws FileNotFoundException :在给定从中读取数据的文件名的情况下创建一个新 FileReader。

常用方法:

public abstract int read(char[] cbuf, int off, int len) throws IOException :

  1. 无参数时,读取单个字符。。
  2. 有参数char[] cbuf时,将字符读入数组。
  3. 有全部参数时,将字符读入数组的某一部分。在某个输入可用、发生 I/O 错误或者到达流的末尾前,此方法一直阻塞。cbuf - 目标缓冲区;off - 开始存储字符处的偏移量;len - 要读取的最多字符数。
public void testRead() throws Exception{
	//输入字符流
	FileReader fr = new FileReader("E:/A/B2/a.txt");
	//读一个字符,返回值是int
	int read = fr.read();
	System.out.println(read);
	//连续多次,一次读取一个字符
	int b = 0;
	while((b = fr.read())!=-1){
		System.out.println((char)b);
	}
	
	fr.close();
}
public void testReadPlus() throws Exception{
	//输入字符流
	FileReader fr = new FileReader("E:/A/B2/a.txt");
	
	//一次读取字符数组长度的字符
	char[] chars = new char[];
	int b = 0;
	while((b = fr.read(chars))!=-1){
		System.out.println(chars);
	}
	
	fr.close();
}

输出字符流

Writer抽象类,FileWriter实现类

构造方法:

  1. public FileWriter(File file[, boolean append]) throws IOException :根据给定的 File 对象构造一个 FileWriter 对象。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。第二个参数可以不写,默认为false。
  2. public FileWriter(String fileName, boolean append) throws IOException :根据给定的文件名以及指示是否附加写入数据的 boolean 值来构造 FileWriter 对象。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。第二个参数可以不写,默认为false。
public void testWrite() throws Exception{
	//创建输出字符流
	FileWriter fw = new FileWriter("E:/A/B2/aa.txt");
	//输出单个字符
	fw.write('我');
	char[] chars = new char[]{'你','好','j','a','v','a'};
	//输出字符数组
	fw.write(chars);
	//输出字符串
	fw.write("你好,java!");
	//输出字符数组中指定内容
	fw.write(chars,2,4);
	//输出字符串中指定内容
	fw.write("你好,java!",2,6);
}

缓冲字符输入流(BufferedReader)

public BufferedReader(Reader in, int sz) :创建一个使用指定大小输入缓冲区的缓冲字符输入流。可以不指定sz,默认为8K。

public String readLine() throws IOException :读取一个文本行。通过下列字符之一即可认为某行已终止:换行 (’\n’)、回车 (’\r’) 或回车后直接跟着换行。返回:包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null

public static void main(String[] args) throws IOException {
		FileReader fr = new FileReader("C:\\A\\B\\d.txt");
		BufferedReader br = new BufferedReader(fr);
		
		int b = 0;
		// 从缓冲区中一次读取一个 输出
//		while((b = br.read()) != -1) {
//			System.out.println((char)b);
//		}
//		fr.close();
		// 从缓冲区中一次读一行 输出,不会读取换行符
		String s = null;
		while((s = br.readLine()) != null) {
			System.out.println(s);
		}
		fr.close();
		
	}

缓冲字符输出流(BufferedWreader)

public BufferedReader(Reader in, int sz) :创建一个使用指定大小输入缓冲区的缓冲字符输入流。可以不指定sz,默认为8K。

    public static void main(String[] args) throws IOException {
		/**
		 * 使用缓冲字符流 进行 文本复制
		 */
		// 创建 缓冲输入字符流
		FileReader fr = new FileReader("C:\\A\\B\\d.txt");
		BufferedReader br = new BufferedReader(fr);
		// 创建 缓冲输出字符流
		FileWriter fw = new FileWriter("C:\\A\\B\\d_copy.txt");
		BufferedWriter bw = new BufferedWriter(fw);
		
//		int b = 0;
//		// 从缓冲区读一个字符,会读取 换行符
//		while((b = br.read()) != -1) {
//			bw.write(b);
//		}
		
		String s = null;
		// 从缓冲区读一行.没有读取换行符
		while((s = br.readLine()) != null) {
			// 写一行
			bw.write(s);
			// 换行
			bw.newLine();
		}
		
		bw.close();
		br.close();
		
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值