IO中的字节流练习

本文介绍了使用Java进行文件输入输出的基本操作方法,包括直接使用FileInputStream和FileOutputStream进行字节流读写,利用BufferedInputStream和BufferedOutputStream提高读写效率,通过DataInputStream和DataOutputStream实现特定类型的数据读写,以及如何利用PrintStream格式化输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. FileInputStream  FileOutputStream通过字节流来读写文件

	public static void main(String[] args) throws Exception {
		//将文件里写数据
		File f = new File("d:\\dmeo.txt");
		FileOutputStream output = new FileOutputStream(f);
		String s = "I Am Learning Java , 我在学习Java";
		byte[] b = s.getBytes(); //将String变为byte数组
		//output.write(b);write可以接收byte数组
		for (int i = 0; i < b.length; i++) {
			output.write(b[i]);
		}
		output.flush();
		output.close();
		//读取文件里数据
		FileInputStream input = new FileInputStream(f);
		//开辟一个文件大小的数组空间
		byte[] in = new byte[(int) f.length()];
		//将读取来的字节放入数组中
		for (int i = 0; i < in.length; i++) {
			in[i] = (byte) input.read(); 
		}
		System.out.print(new String(in));
		input.close();
	}

2. BufferedInputStream BufferedOutPutStream 带有缓存的读写字节流

 	public static void main(String[] args) throws Exception {
		//将文件里写数据
		File f = new File("d:\\dmeo.txt");
		BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(f));
		String s = "I Am Learning Java , 我在学习Java";
		byte[] b = s.getBytes();
		output.write(b); //默认会有512字节的缓存,当缓存存满时一次性向文件中写入
		output.flush(); 
		output.close();
		
		BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));
		byte[] in = new byte[(int)f.length()];
			for (int i = 0; i < in.length; i++) {
				in[i] = (byte)input.read();
			}
		System.out.println(new String(in));
		input.close();
 	}

2. DataInputStream DataOutputStream 可以读写基本数据类型的字节流

import java.io.*;
//一个简单的人员类,只有姓名和年龄
public class Person {
	private String name;
	private int age;
	
	public Person(String name ,int age){
		this.name = name;
		this.age = age;
	}
	
	public String getName(){
		return this.name;
	}
	public int getAge() {
		return this.age;
	}
	
	
	public static void main(String[] args) {
		//构造对象数组
		Person[] p1 = {new Person("Ryan",20),new Person("Tom",30),new Person("Jerry",15)};
		File f = new File("d:\\demo\\demo.txt");
		try {
			DataOutputStream output =  new DataOutputStream(new FileOutputStream(f));
			//写入数据
			for (int i = 0; i < p1.length; i++) {
				output.writeUTF(p1[i].getName()); //以UTF编码写入姓名
				output.writeInt(p1[i].getAge()); //以Int型写入年龄
			}
			output.flush();
			output.close();
		} catch (Exception e) {
			e.printStackTrace();
		}	
		Person[] p2 = new Person[p1.length];
		try {
			//读出数据
			DataInputStream input = new DataInputStream(new FileInputStream(f));
			for (int i = 0; i < p1.length; i++) {
				String name =  input.readUTF(); //读出姓名
				int age  = input.readInt(); //读出年龄
				//重新构造person对象
				p2[i] = new Person(name,age);			
			}
                input.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		//检查是否还原正确
		for (int i = 0; i < p2.length; i++) {
			System.out.println("姓名:" + p2[i].getName() + " ;年龄:" + p2[i].getAge());
		}
	
	}
}


2. PrintStream  将内存中的数据经过相应的转换后再输出

	public static void main(String[] args){
		File f = new File("d:\\dmeo.txt");
		String studentName = "TOM";
		int age = 20;
		double totalScore = 600.0f;
		PrintStream ps = null;
		try {
			ps = new PrintStream(new FileOutputStream(f));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		//格式化后输出
		ps.printf("姓名:%s , 年龄:%d , 总分:%3.1f",studentName,age,totalScore);
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值