day_21 IO

2021/10/26 23:09:50

本文主要是介绍day_21 IO,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1 IO

1.1概述

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据传输特性将流抽象为各种类,方便更直观的进行数据操作.

        I :Input 输入

        O:Output输出

输入:就是把数据输入到内存中

输出:就是把内存中的数据写出到外面

1.2分类

·按处理数据类型的不同,分为字节流和字符流

·按数据流向的不同,分为输入流和输出流.(入和出是相对于内存来讲的)

        文件、其他输入设备、内存---输入流--->内存---输出流--->文件、其他输出设备、内存

·按功能不同,分为节点流和处理流

        ·节点流:直接操作数据源

        ·处理流:对其他流进行处理

1.3四大抽象类

InputStream:字节输入

OutputStream:字节输出

Reader:字符输入

Writer:字符输出

1.4InputStream

 

方法描述
void close()关闭此输入流并释放与该溜关联的所有系统资源
abstract int read()从输入流读取下一个数据字节
int read (byte[] b)从输入流中读取一定数量的字节并将其存储在缓冲区数组b中
int read(byte[] b,int off,int len)将输入流中最多冷个数据字节读入字节数组

1.4.1 read使用方式

read:读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾(读完了)返回-1

package day_21text;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * FileInputStream 是字节输入流,就是读取文件中的数据
 * 
 * 操作系统层面 一切皆文件
 *
 * @author 学到头秃的张张张
 *@Date 2021年10月26日下午8:51:21
 */
public class Text01 {
	
	public static void main(String[] args) throws  Exception {
		// 要读取文件,必须要找到它
		// 如何找? 两种方式 : 绝对路径 和 相对路径
		// 绝对路径 : 能够找到这个文件的全路径
		// 相对路径 : 相对当前文件所在位置去找其他文件 , ./ 表示当前目录, ../ 表示上级目录, ../../ 表示上上级目录, ../../../...
		
		// FileInputStream fis = new FileInputStream("D:/test/a.txt");
		// eclipse中 ./ 找到的是当前项目
		FileInputStream fis = new FileInputStream("./src/day_21text/Text01.java");
		// read : 读取文件中的数据,一次读取一个字节,返回值是读取的字节的值(返回int类型),如果读取到文件末尾(读完了)返回-1
		int i1 = fis.read();
		System.out.println((char)i1);
	}
}
package day_21text;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * 循环读取
 *
 * @author 学到头秃的张张张
 *@Date 2021年10月26日下午9:06:31
 */
public class Text02 {
	
	public static void main(String[] args)   {
		try (FileInputStream fis = new FileInputStream("./src/day_21text/Text01.java"); ){
			int temp = 0;
			while( (temp = fis.read()) != -1 ){
				System.out.print((char)temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
}

1.4.2 read重载使用方式

/**
 * read方法的重载 , 可以传递一个字节数组,为了提高读取效率
 * 
 * 默认一次读取一个字节,如果传递了字节数组,则把数组一次性读满,再回来,然后下次去再读满,一直到读完
 * 
 * read(byte[]) : 一次读取一个数组,返回当前次读取到的个数,如果到达文件末尾,返回-1
 *
 * @author 学到头秃的张张张
 *@Date 2021年10月26日下午9:08:44
 */
package day_21text;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Text03 {
	public static void main(String[] args) {
		try (FileInputStream fis = new FileInputStream(
				"./src/day_21text/Text01.java");) {
			// available : 可以获取的字节数
			System.out.println(fis.available());
			// byte[] bytes = new byte[fis.available()];

			byte[] bytes = new byte[1025];
			int count = fis.read(bytes);
			System.out.println(new String(bytes, 0, count));
			System.out.println(count);
			count = fis.read(bytes);

			// new String(bytes) 把自己数组中所有的数据转换为字符串,但是可能有冗余数据
			// 所以推荐使用 new String(bytes,0,count) 只把本次读取的数据转换为字符串
			System.out.println(new String(bytes, 0, count));
			System.out.println(count);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
package day_21text;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class Text04 {
	
	public static void main(String[] args) {
		try (FileInputStream fis = new FileInputStream(
				"./src/day_21text/Text01.java");) {
			byte[] bytes = new byte[1025];
			int count = 0;
			while ((count = fis.read(bytes)) != -1) {
				System.out.println(new String(bytes, 0, count));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

1.5 Reader

方法描述
abstract void close()关闭该流
int read()读取单个字符
int read(char[] cbuf)将字符读入数组
abstract int read(char[] cbuf,int off,int len)将字符读入数组的某一部分
/**
 * FileReader 字符输入流, 字节输入流是一次读取一个字节,而字符输入流一次读取一个字符
 * 
 * 并且java中汉字采用的是unicode编码,而unicode编码占用16位,就是一个字符
 * 
 * 所以使用字符输入流可以解决汉字乱码问题
 * 
 * 字符流一般只用于纯文本文件,比如压缩包,图片,视频等,还是需要使用字节流处理
 * 
 * read() : 读取一个字符,返回该字符的int值,到达文件末尾返回 -1
 * 
 * read(char[]) : 读取一个char数组,返回当前次读取的个数,到达末尾返回-1
 * 
 * @author 学到头秃的张张张
 *@Date 2021年10月26日下午9:22:09
 */

 1.5.1 read使用方式

package day_21text;

import java.io.FileReader;

public class Text05 {
	public static void main(String[] args) {
		try (FileReader fr = new FileReader(
				"./src/day_21text/Text01.java")) {
			int temp = 0;
			while ((temp = fr.read()) != -1) {
				System.out.print((char) temp);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.5.2 read重载使用方式

package day_21text;

import java.io.FileReader;

public class Text05 {
	public static void main(String[] args) {
		 try (FileReader fr = new FileReader(
		 "./src/day_21text/Text01.java")) {
		 char[] chars = new char[512];
		 int temp = 0;
		 while ((temp = fr.read(chars)) != -1) {
		 System.out.println(new String(chars, 0, temp));
		 }
		
		 } catch (Exception e) {
		 e.printStackTrace();
		 }
	}
}

1.6 OutputStream

1.6.1 概述

方法描述
void close()关闭此输出流并释放与此流优芙安的所有系统资源
void flush()刷新此输出流并强制写出所有缓冲的输出字节
void write(byte[] b)将b.length个字节从指定的字节数组写入此输出流
void write(byte[] b,int off,int len)将指定字节数组中从偏移量off开始的len个字节写入此输入流
abstract void write(int b)将指定的字节写入此输出流
/**
 * 输出流 是把内存中的数据写出到硬盘中
 * 
 * 如果该文件不存在,会自动创建,但是不会创建文件夹
 * 
 * 如果对应的文件夹目录不存在,就报错
 * 
 * 构造方法 
 * 
 * 		FileOutputStream(String path) : 向该文件中写出数据,并覆盖原有内容
 * 
 * 		FileOutputStream(String path,boolean append) : 向该文件中写出数据,如果append为true就追加写出,如果为false就覆盖
 * 
 * 		write(int) : 写出一个数据
 * 		write(byte[] b) : 写出该数组中所有内容
 * 		write(byte[] b,int off,int len) : 写出该数组中指定数据
 * 
 * 		flush() : 刷缓存,不调用也可以,关闭流的时候会自动调用
 * 
 * 	辅助方法 : getBytes() : String中的方法,用于获取字符串对应的字节数组
 *
 * @author 学到头秃的张张张
 *@Date 2021年10月26日下午9:34:34
 */

1.6.2使用方式

package day_21text;

import java.io.FileOutputStream;

public class Text06 {

	public static void main(String[] args) {
		try (
//				FileOutputStream fos = new FileOutputStream("./src/com/a.txt");
				FileOutputStream fos = new FileOutputStream("./src/com/a.txt",true);
				){
			fos.write(97);
			fos.write(98);
			fos.write(99);
			fos.write(100);
			fos.write('\n');
			String str = "你好吗";
			byte[] bytes = str.getBytes();
			fos.write(bytes);
			fos.flush();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	 }

}

1.7Writer

1.7.1 概述

 

方法描述
void write(int c)写入单个字符
void write(char[] cbuf)写入字符数组
abstract void write(char[] cbuf,int off,int len)写入字符数组的某一部分
void write(String str)写入字符串
void write(char[] cbuf,int off,int len)写入字符串的某一部分
Writer append(char c)将指定字符添加到此writer
abstract void flush()刷新该流的缓冲
abstract void close()关闭此流,但要先刷新它

1.7.2 使用方式

package day_21text;

import java.io.FileWriter;

public class Text07 {
	public static void main(String[] args) {
		try (
				FileWriter fw = new FileWriter("./src/com/a.txt");
				){
			// 写出字符串
			fw.write("你好吗?\n");
			char[] chars = {'a','b','c','d'};
			// 写出char数组
			fw.write(chars);
			fw.flush();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

1.8缓冲流

1.8.1概述

分类字节流字符流
文件输入流BufferedInputSteamBufferedReader
文件输入流BufferedOutputSteamBufferedWriter

[特点]

·主要是为了提高效率而存在的,减少雾里读取次数

·提供ReadLine()、newLine()这样的便捷方法(针对缓冲字符流)

·在读取和写入时,会有缓存部分、调用flush为刷新缓存,将内存数据写入到磁盘

1.8.2 BufferedInputSteam

package day_21text;

import java.io.BufferedInputStream;
import java.io.FileInputStream;

public class Text08 {
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		try (
				FileInputStream fis = new FileInputStream("./src/com/a.txt");
				BufferedInputStream bis = new BufferedInputStream(fis);
				){
			byte[] bytes = new byte[1024];
			int temp = 0;
			while((temp=bis.read(bytes)) != -1){
				// System.out.println(new String(bytes,0,temp));
			}
			long endTime = System.currentTimeMillis();
			System.out.println("读取完成,耗时 : "+(endTime-startTime));
			// 字节流 : 120
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.8.3 BufferedOutputSteam

package day_21text;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class Text09 {
	
	public static void main(String[] args) {
		try (FileOutputStream fos = new FileOutputStream("./src/com/a.txt");
				BufferedOutputStream bos = new BufferedOutputStream(fos);
		) {
			bos.write(97);
			bos.write(98);
			bos.write(99);
			bos.write(100);
			bos.write('\n');
			String str = "你好吗";
			byte[] bytes = str.getBytes();
			bos.write(bytes);
			bos.flush();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.8.4 BufferedReader

package day_21text;

import java.io.BufferedReader;
import java.io.FileReader;

/**
 * 字符输入缓冲流
 * 
 * 新增方法 String readLine() : 读取一行数据,返回值就是读到的数据,到达文件末尾返回 null
 *
 * @author 学到头秃的张张张
 *@Date 2021年10月26日下午10:53:07
 */
public class Text10 {
	public static void main(String[] args) {
		try (FileReader fr = new FileReader("./src/com/a.txt");
				BufferedReader br = new BufferedReader(fr);) {
			String temp = null;
			// 读取一行
			while ((temp = br.readLine())  != null) {
				System.out.println(temp);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

1.8.5 BufferedWriter

package day_21text;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 字符输出缓冲流
 *
 * @author 学到头秃的张张张
 *@Date 2021年10月26日下午10:54:49
 */
public class Text11 {
	
	public static void main(String[] args) {
		BufferedWriter bw = null ;
		try {
			 bw = new BufferedWriter(new FileWriter("./src/com/a.txt"));

			bw.write("你好吗");
			// 换行
			bw.newLine();
			bw.write("我很好");
			bw.flush();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (bw != null) {
					bw.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}



这篇关于day_21 IO的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程