关于IO流的思考

今天是我接触IO流的第8天。

这几天很懒散,一直在和懒惰做斗争,好在生活已经从兼职模式渐渐调整过来,话不多说,上干货。

IO流的作用,可以说是革命性的,它突破了数据随着程序结束而消失的局限,可以将经过程序处理的数据以文件的形式保留下来。如果将IO流和之前学习的java基础结合起来,对你以后的Java学习可谓说是加了一个助推器。

IO流是程序和用文件存储的数据的交互。

I就是输入流,是指数据从文件或者键盘内输入到程序里,O就是输出流,是指数据从程序出来,并打印在屏幕上,或者文件上。接下来我说的,是输入从键盘或者文件,输出到文件。中间的媒介,就是程序。

文件可以分为文本文件和二进制文件。故此,文件I/O分为文本文件和二进制文件。在这里我们先讲一讲他们的共性。

不论是文本文件,还是二进制文件,都是可以用IO的方法操作的,程序可以从文件内读取数据,也可以把数据写入到文件内。像其他程序一样,这个过程伴随着很多不确定的因素,比如程序需要读取一个文件,可是该文件的设置为不可读。或者程序在运行时某个地方出错。这样都会抛出异常,因此,在操作IO流的时候,需要捕获异常。在程序该做的都做了,不需要文件什么事了,就要切断IO流,切断它总是好的,不然会出现很不好的事情。接下来,讲特性。

下面,我们从文本文件来讲起。

文本文件,你可以把它理解字符串组成的文件,比如记事本,word文档都属于文本文件。该类型的文件可以直接查看编辑(用户没有设置权限的情况下)。比较好理解,没有什么好讲的。上代码。

文本文件输出流

import java.io.PrintWriter;

import java.io.FileNotFoundException;
import java.util.Scanner;
//该程序创建文本文件,并将键入内容打印到文本文件上。
public class TextFileOutputDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
String fileName="out.txt";//用户名也可键入
PrintWriter outputStream=null;//PrintWriter流类会抛出FileNotFoundException异常,需要在try语句内捕获异常。
try {
	outputStream=new PrintWriter(fileName);//outputStream对象赋值,并捕获可能发生的异常
}catch(FileNotFoundException e) {
	System.out.println("Error opening the file "+fileName);
	System.exit(0);
	
}
System.out.println("Enter three lines of text.");
Scanner in=new Scanner(System.in);
for(int i=1;i<=3;i++) {
	String line =in.next();
	outputStream.println(i +" "+line);//输出流,将内容打印在文件和屏幕上。
}
outputStream.close();//关闭流。断开连接
System.out.println("Those lines were written to "+fileName);
	}

}

文本文件输入流

//该程序读入文本文件的内容,并打印输出。

public class TextFileInputDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
String fileName="out.txt";
Scanner inputStream=null;//Scanner 的对象可能会抛出异常。
try {
	inputStream =new Scanner(new File (fileName));//Scanner类的构造器不能识别出字符串表示的文件名,所以需要新建File类,
												//将文件名作为包装成File类的对象传入进去。					
	
}catch(FileNotFoundException e) {
	System.out.println("Error opening the file "+fileName);
	System.exit(0);
}
while(inputStream.hasNextLine()) {//该句检查是否有下一句,如果有为true;
	String line =inputStream.nextLine();//读取一行。
	System.out.println(line);//输出一行。
}
inputStream.close();
	}

}

下面讲讲二进制文件。

二进制文件I/O提供了对于数据处理更加细致的方法,不仅可以将常见数据形式按照基本类型读入,也可以读取对象,是的,你没有听错,读取对象,最简单的讲,就是保存该对象现在的数据结构,在需要它的时候,再调用。做到这点,和输入流,输出流的写法没有什么太大的差别,需要注意的是,在类的首部,需要加上implements Serializable.

二进制输出流

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;//该类用于存储二进制文件。
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;


public class BinaryOutputDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
String fileName="numbers.bat";
try {
	ObjectOutputStream outputStream =new ObjectOutputStream(new FileOutputStream(fileName));
	//ObjectOutputStream类的构造器不能接受字符串为文件名的参量。因此,采用FileOutputStream类的对象来使构造器能接受。
	Scanner in=new Scanner(System.in);
	System.out.println("Enter nonnegative number at the end.");
	System.out.println("Place a negative number at the end.");
	int anInteger;
	do {
		anInteger=in.nextInt();
		outputStream.writeInt(anInteger);//把int值anInteger写入输出流。
		
	}while(anInteger>=0);
	System.out.println("Numbers and sentinel value.");
	System.out.println("written to the file "+fileName);
	outputStream.close();//流结束输出后必须关闭
	in.close();
}catch(FileNotFoundException e) {//FileNotFound 可能抛出FileNotFoundException异常
	System.out.println("Problem opening the file "+fileName);
}catch(IOException e) {//ObjectOutputStream可能抛出IOException 异常。
	System.out.println("Problem with output to file "+fileName);
}

	}

}

二进制文件输入流

//该类为二进制输入流类,但是结合了EOFException类的特点。
import java.io.FileInputStream;

import java.io.ObjectInputStream;//二进制输入流
import java.io.EOFException;//该类属于读到文本文件末尾后抛出异常类
import java.io.FileNotFoundException;
import java.io.IOException;

public class EOFExceptionDemo {
public static void main(String args[]) {
	String fileName="numbers.dat";
	try {
	ObjectInputStream inputStream =new ObjectInputStream (new FileInputStream(fileName));
	//创建二进制流类ObjectInputStream的对象,同样,和输出流一样,他的构造器不能直接接受以字符串形式标识的文件名作为参量。
	System.out.println("Reading ALL the integers");
	System.out.println("in the file"+fileName);
	//注意,接下来是个很有意思的方法,准确来说,是一种思想。
	try {
		
		while(true) {
			int anInteger =inputStream.readInt();
			System.out.println(anInteger);
			//while算法看起来像个死循环,但事实并不是这样,它位于try方法内,利用inputStream读取到文章末尾后,抛出IOFException异常,结束自己
		}
		}catch(EOFException e) {
			System.out.println("End of reading from file");
		
	}
	//
	inputStream.close();
	}
	catch(FileNotFoundException e) {
		System.out.println("Cannot find file "+fileName);
	}catch(IOException e) {
		System.out.println("Problem with input from file "+fileName);
	}

}
}

对象的处理方法,我今天还没有运行好,下次再写。

猜你喜欢

转载自blog.csdn.net/qq_41960942/article/details/81668043