1. 字符流,PrintWriter,Scanner
File file = new File("text.txt");//只是File本身,不能进行读写
//********写入*********//
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter(file);
printWriter.print("text");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
printWriter.close();
}
//********读取*********//
Scanner scanner = null;
try {
scanner = new Scanner(file);
System.out.println(scanner.nextLine());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
scanner.close();
}
2. 二进制流(InputStream和OutputStream是抽象类,不能实例化)
2.1. FileInputStream和FileOutputStream
//******写入*******//
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("text.dat");
fileOutputStream.write(1);
fileOutputStream.write(2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//*****读取********//
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream("text.dat");
int data = 0;
while ((data = inputStream.read()) != -1) {
System.out.println(data);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
try {
inputStream.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
2.2. DataOutputStream和DataInputStream
//********写入*********//
DataOutputStream dataOutputStream = null;
try {
dataOutputStream = new DataOutputStream(new FileOutputStream("test.dat"));
dataOutputStream.writeInt(2);
dataOutputStream.writeUTF("我");
} catch (Exception e) {
// TODO: handle exception
}finally {
try {
dataOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//********读取*********//
DataInputStream dataInputStream = null;
try {
dataInputStream = new DataInputStream(new FileInputStream("test.dat"));
System.out.println(dataInputStream.readInt());
System.out.println(dataInputStream.readUTF());
System.out.println(dataInputStream.readUTF());//没有输出,只是测试文件是否读取完
} catch(EOFException e) {
//读取到文件尾,继续读取就会发生EOFException,使用该异常来判断文件读取完了
System.out.println("All data were read");
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
dataInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.3. BufferedInputStream和BufferedOutputStream
减少磁盘的读写次数来提高输入输出的速度
//********写入*********//
BufferedOutputStream bufferedOutputStream = null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("test.dat"));
bufferedOutputStream.write(1);
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
bufferedOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//********读取*********//
BufferedInputStream bufferedInputStream = null;
try {
bufferedInputStream = new BufferedInputStream(new FileInputStream("test.dat"));
int data = 0;
while ((data = bufferedInputStream.read()) != -1) {
System.out.println(data);
}
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
bufferedInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.4. ObjectOutputStream和ObjectInputStream
读写序列化(Serializable)对象
public class Main {
public static void main(String[] args) {
//******写入*****//
ObjectOutputStream objectOutputStream = null;
try {
A a = new A(3);
objectOutputStream = new ObjectOutputStream(new FileOutputStream("test.dat"));
objectOutputStream.writeObject(a);
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//******读取*****//
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream("test.dat"));
A a = (A)objectInputStream.readObject();
System.out.println(a.a);
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
objectInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
class A implements Serializable{
public int a;
public A(int a) {
this.a = a;
}
}
3. RandomAccessFile
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile("test.dat", "rw");
randomAccessFile.writeInt(1);
randomAccessFile.writeInt(2);
randomAccessFile.seek(4);//跳过第一数字,读取第二个数字
System.out.println(randomAccessFile.readInt());
} catch (Exception e) {
// TODO: handle exception
} finally {
try {
randomAccessFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}