/**
* 二进制读写文件
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainClass
{
/**
* java.io包中的OutputStream及其子类专门用于写二进制数据。
* FileOutputStream是其子类,可用于将二进制数据写入文件。
* DataOutputStream是OutputStream的另一个子类,它可以
* 连接到一个FileOutputStream上,便于写各种基本数据类型的数据。
*/
public void writeMethod1()
{
String fileName="c:/kuka1.dat";
int value0=255;
int value1=0;
int value2=-1;
try
{
//将DataOutputStream与FileOutputStream连接可输出不同类型的数据
//FileOutputStream类的构造函数负责打开文件kuka.dat,如果文件不存在,
//则创建一个新的文件,如果文件已存在则用新创建的文件代替。然后FileOutputStream
//类的对象与一个DataOutputStream对象连接,DataOutputStream类具有写
//各种数据类型的方法。
DataOutputStream out=new DataOutputStream(new FileOutputStream(fileName));
out.writeInt(value0);
out.writeInt(value1);
out.writeInt(value2);
out.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
//对于大量数据的写入,使用缓冲流BufferedOutputStream类可以提高效率
public void writeMethod2()
{
String fileName="c:/kuka2.txt";
try
{
DataOutputStream out=new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(fileName)));
out.writeInt(10);
System.out.println(out.size()+" bytes have been written.");
out.writeDouble(31.2);
System.out.println(out.size()+" bytes have been written.");
out.writeBytes("JAVA");
System.out.println(out.size()+" bytes have been written.");
out.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 对二进制文件比较常见的类有FileInputStream,DataInputStream
* BufferedInputStream等。类似于DataOutputStream,DataInputStream
* 也提供了很多方法用于读入布尔型、字节、字符、整形、长整形、短整形、
* 单精度、双精度等数据。
*/
public void readMethod1()
{
String fileName="c:/kuka1.dat";
int sum=0;
try
{
DataInputStream in=new DataInputStream(
new BufferedInputStream(
new FileInputStream(fileName)));
sum+=in.readInt();
sum+=in.readInt();
sum+=in.readInt();
System.out.println("The sum is:"+sum);
in.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
public void readMethod2()
{
try
{
FileInputStream stream=new FileInputStream("c:/kuka.dat");
int c;
while((c=stream.read())!=-1)
{
System.out.println(c);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
==============================================例子========================================
DataInputStream与DataOutputStream类
//DataStreamTest.java
import java.io.*;
public class DataStreamTest{
public static void main(String[] args) throws Exception {
FileOutputStream fos = new FileOutputStream("count.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
DataOutputStream dos = new DataOutputStream(bos);
dos.writeUTF("ab中国");
dos.writeBytes("ab中国");
dos.writeChars("ab中国");
dos.close();
//只要关闭最上层流对象,下层的流对象也会关闭
FileInputStream fis = new FileInputStream("count.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream dis = new DataInputStream(bis);
System.out.pritnln(dos.readUTF());
dis.close();
}
}
PrintStream类
ObjectInputStream与ObjectOutputStream类
编程举例:创建了一个可序列化学生对象,并用ObjectOutputStream类把它存储到一个文件(mytext.txt)中,然后再用ObjectInputStream类把存储的数据读了到一个学生对象中,即恢复保存的学生对象。
//Student.java
import java.io.Serializable;
class Student implements Serializable{
int id;
String name;
int age;
String department;
public Student(int id,String name,int age,String department){
this.id = id;
this.name = name;
this.age = age;
this.department = department;
}
}
//Serialization.java 启动类
import java.io.*;
public class Serialization{
public static void main(String[] args) throws Exception {
Student stu1 = new Student(19,"zhangsan",25,"hauxue");
Student stu2 = new Student(20,"lisi",23,"wuli");
FileOutputStream fos = new FileOutputStream("student.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(stu1);
os.writeObject(stu2);
os.close();
FileInputStream fis = new FileInputStream("student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
stu1 = (Student)ois.readObject();
stu2 = (Student)ois.readObject();
ois.close();
System.out.println("id:" + stu1.id);
System.out.println("name:" + stu1.name);
System.out.println("age:" + stu1.age);
System.out.println("age:" + stu1.department);
System.out.println("id:" + stu2.id);
System.out.println("name:" + stu2.name);
System.out.println("age:" + stu2.age);
System.out.println("age:" + stu2.department);
}
}
字节流与字符流的转换
|