Java文件读写

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();
			}
		}

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值