Java I/O流总结

一、I/O:

I指的是input即输入,O指的是output即输出。对于I/O我们可做以下分类:

按数据流向分类:输入流和输出流;

按处理的数据类型分类:字节流和字符流;

二、“流”的理解:

我们可以通过“水流”的概念来理解“I/O流”,有方向、有传输介质;只不过在计算机中流指的是数据。记住一句话“流的本质就是数据传输”。

三、输入输出流:

简单理解:从文本文件中读取数据到程序中,即为输入流;通过程序向文本文件中写入数据即为输出流。

四、I/O流中相关类及相关操作:

1、File类:

该类主要用于文件和目录的创建、文件的查找和文件的删除等。示例如下:

File file=new File("F:/demo.txt");
		if(!file.exists()) { //判断文件是否存在
			try {
				file.createNewFile(); //新建一个文件
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("文件已存在!");
		}

2、FileInputStream和FileOutputStream类:

这两个类是操作字节流较常用和重要的,使用前提是文本内容为数字或字母;若文本内容为中文需采用字符流来操作,下面会讲到。1字节=8bit;1汉字=2字节=16bit。以下示例为通过字节流读取文本内容:

File file=new File("F:/demo.txt");
		if(!file.exists()) { //判断文件是否存在
			try {
				file.createNewFile(); //新建一个文件
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("文件已存在!");
			byte[] bt = new byte[1024]; //以1kb的大小来读取
			try {
				FileInputStream fis = new FileInputStream(file);
				int a = fis.read(bt); //若文本内容不超过1kb将会被一次性读取,将读到的内容放在byte数组中并返回文本内容的字节数
				String s = new String(bt);
				System.out.println(s);
				System.out.println(a);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

通过字节流写入文本:基本上的操作在上面的输入流已经完成,重点为下面两行代码,括号内的true如果不写上默认为false,那么当写入文本的时候会覆盖之前的内容,如果我们加上true的话可以在原有内容上继续添加。

FileOutputStream fos=new FileOutputStream("F:/demo1.txt",true);
fos.write(bt);

2、InputStreamReader和BufferedReader类:

这两个类常用来操作字符流;以下示例为通过InputStreamReader读取文本中的中文:"gbk"为编码格式,若不写则可能导致文本内容为乱码。

File file=new File("F:/demo.txt");
		if(!file.exists()) { //判断文件是否存在
			try {
				file.createNewFile(); //新建一个文件
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("文件已存在!");
			try {
				FileInputStream fis = new FileInputStream(file);
                                InputStreamReader isr=new InputStreamReader(fis,"gbk"); //获取字符输入流
                                char[] ca = new char[64]; //以字符型读取文本
				int a = fis.read(ca); 
				String s = new String(ca);
				System.out.println(s);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

以下示例为BufferedReader的使用:缓冲字符流,能保证文本内容的格式:

File file=new File("F:/demo.txt");
		if(!file.exists()) { //判断文件是否存在
			try {
				file.createNewFile(); //新建一个文件
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("文件已存在!");
			try {
				FileInputStream fis = new FileInputStream(file);
                                InputStreamReader isr=new InputStreamReader(fis,"gbk");
                                BufferedReader br=new BufferedReader(isr);
                                String line=null;
				while((line=br.readLine())!=null){ //通过readLine方法整行读取
					System.out.println(line);
				}
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

BufferedWriter的使用:

扫描二维码关注公众号,回复: 5401138 查看本文章
File file=new File("F:/demo.txt");
		if(!file.exists()) { //判断文件是否存在
			try {
				file.createNewFile(); //新建一个文件
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else {
			System.out.println("文件已存在!");
			try {
				FileInputStream fis = new FileInputStream(file);
                                InputStreamReader isr=new InputStreamReader(fis);
                                BufferedReader br=new BufferedReader(isr);
                                OutputStreamWriter osw=new OutputStreamWriter(fos);
			       	BufferedWriter bw=new BufferedWriter(osw);
                                String line=null;
				while((line=br.readLine())!=null){ //通过readLine方法整行读取
					bw.write(line); //写入文本
					bw.newLine();
				}
				br.close();
				bw.close(); //记得关闭流
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

以上就是我对于I/O流的理解,但是除了我讲的几个常用类之外还有许多的,大家可以在实际应用中慢慢摸索,并且每个类中的方法也有很多的,都是需要大家慢慢学习的。

猜你喜欢

转载自blog.csdn.net/qq_40181063/article/details/82354145