今天我要写的是流的套接技术及用流异常用异常来控制文件结束 以后有的没有available,只能用异常来读取所有内容。以及用ObjectOutputStream和ObjectInputStream来读写文件,这个处理能力比之前的强大一次可以读写一个对象,而之前的只能读写一个字节。很明显这个对象读写的速率快。
第一个知识点:流的套接技术。因为OutputStream out= new FileOutputStream("e:/abc/a/data.txt",true); 而DataOutputStream dout=new DataOutputStream(out);这里DataOutputStream要一个OutputStream 对象但是OutputStream 又是抽象的 所有子类当父类,用FileOutputStream来new。三层套接。这里有个小细节,在new FileOutputStream("e:/abc/a/data.txt",true);里面的true 是在文件中进行追加读写,如果不写的话默认的是false,就是覆盖读写。
package cn.hncu.io1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MyDateInOutputStreamDemo {
public static void main(String[] args) {
//流的套接技术
//写入文件
try {
OutputStream out= new FileOutputStream("e:/abc/a/data.txt",true);//ture的话就是在后面追加 没有加ture的话默认为false 覆盖掉原来的
DataOutputStream dout=new DataOutputStream(out);
for (int i = 0; i <100; i+=2) {
dout.writeInt(i);
}
} catch (IOException e) {
e.printStackTrace();
}
// 文件写入的时候用记事本开的话不对,因为记事本是用它自己的读取方式,我们如果要读取的话要自己写读取方法。
// 文件读取
try {
InputStream in= new FileInputStream("e:/abc/a/data.txt");
DataInputStream din=new DataInputStream(in);
while(din.available()>0){
int n=din.readInt();
System.out.print(n+" ");
}
System.out.println();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第二个知识点:用流异常的方式来用异常来控制文件结束以后有的没有available,只能用异常来读取所有内容。这个和之前的while(din.available()>0大概是相同的,只是变成while(true)里面用catch异常用break来控制文件读写的结束
</pre><pre name="code" class="java">
</pre><pre name="code" class="java">package cn.hncu.io1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class IntFile {
private String fileName;
public IntFile(String fileName) {
this.fileName = fileName;
}
//把所有short范围内的费布拉切序列的数输入到文件当中
private void writeToFile() throws IOException {
FileOutputStream fout=new FileOutputStream(fileName);
DataOutputStream dout=new DataOutputStream(fout);
short i=0,j=1;
do{
dout.writeInt(i);
dout.writeInt(j);
i=(short)(i+j);
j=(short)(i+j);
}while(i>0);//只要i<0即代表溢出
dout.close();
fout.close();
}
private void readFormFile() throws IOException {
FileInputStream fin=new FileInputStream(fileName);
DataInputStream din=new DataInputStream(fin);
do{
int n=din.readInt();
System.out.print(n+" ");
}while(din.available()>0);
System.out.println();
}
//用异常来控制文件结束 以后有的没有available,只能用异常来读取所有内容。
private void readFormFile2() throws IOException {
FileInputStream fin=new FileInputStream(fileName);
DataInputStream din=new DataInputStream(fin);
while(true){
try {
int n=din.readInt();
System.out.print(n+" ");
} catch (EOFException e) {
break;
}
}
System.out.println();
}
public static void main(String[] args) {
IntFile afile=null;
try {
afile=new IntFile("FibIntFile.dat");
afile.writeToFile();
// afile.readFormFile();
afile.readFormFile2();
} catch (IOException e) {
e.printStackTrace();
}
}
}
第三个知识点:用ObjectOutputStream和ObjectInputStream对象读写。这里也要用到流的套接技术,我在这里也用到第二个知识点来读写文件,这个obj读写比较绕,用到了多态等知识点。但细细品味还是感觉挺简单的。因为用到了几个其他的类所以我就只贴出我读写核心代码,而作为对象读写的类如student和person 以及mydata我就省了。
<pre name="code" class="java">package cn.hncu.io1;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class ObjectFileInOutputStreamDemo {//用这个一次可以写一个对象,而之前的一次只能写一个字节
private String fileName;
public ObjectFileInOutputStreamDemo(String fileName, Person[] persons) throws IOException {
this.fileName=fileName;
writeToFile(persons);
}
private void writeToFile(Person[] persons)throws IOException {
FileOutputStream fout=new FileOutputStream(fileName);
ObjectOutputStream objOut=new ObjectOutputStream(fout);
for (int i = 0; i < persons.length; i++) {
objOut.writeObject(persons[i]);
}
objOut.close();
fout.close();
}
private String readFormFile() throws IOException{
FileInputStream fin=new FileInputStream(fileName);
ObjectInputStream objIn=new ObjectInputStream(fin);
String str="";
//对象用流异常处理的方式去读到文件结束
while(true){
try {
str+=objIn.readObject().toString()+"\n";
} catch (Exception e) {
break;
}
}
return str;
}
public static void main(String[] args) {
Person[] persons={
new Student("楊柳",new MyDate(1989,1,2),"計算機"),
new Student("張明",new MyDate(1990,2,3),"計算機"),
new Person("小玲",new MyDate(1988,6,2)),
};
try {
System.out.println(new ObjectFileInOutputStreamDemo("PersonFile.dat",persons).readFormFile());
} catch (IOException e) {
e.printStackTrace();
}
}
}