Java IO常用对象

 

IO中常用对象


1.字节流
1.1
FileOutputStream fos = new FileOutputStream(getFile());
FileInputStream fis = new FileInputStream(getFile());
1.2
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath + "b.gif"), 1024);
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filePath + "a.gif"));


2.字符流
2.1
FileWriter fw = new FileWriter(getFile(), true);
FileReader fr = new FileReader(getFile());
2.2
BufferedWriter bw = new BufferedWriter(new FileWriter(getFile(), false), 1024);
BufferedReader br = new BufferedReader(new FileReader(getFile()), 1024);

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getFile())), 1024);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(getFile())));

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getFile())));


3.转换流(字节转字符)
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(getFile()));
InputStreamReader reader = new InputStreamReader(new FileInputStream(getFile()));

4.Properties
File file = new File(getFile());
Properties p = new Properties();
p.load(new FileReader(file));
Object obj = p.get("count");
p.setProperty("count", String.valueOf(count));
p.store(new FileWriter(file), "说明");


5.File


6.打印流
PrintStream
PrintWriter


7.管道流
PipedInputStream
PipedOutputStream

 

8.RandomAccessFile

// 生成一个文件,文件第一行保存文件路径名及文件记录的行数,逗号分隔,第一行不包括在内
	// 随机生成指定个数,如果是偶数就保存到文件中并记录个数,第行保存一个数
	// 最后更新文件中记录的行数
	public void createFile() {
		String filePath = "e:/doc/data/abc.txt";
		BufferedWriter writer = null;
		int max = 10000;
		int count = 0;
		try {
			writer = new BufferedWriter(new FileWriter(filePath));
			writer.write(filePath);
			writer.write(",00000000");
			writer.newLine();
			Random random = new Random();
			int value = 0;
			for (int i = 0; i < max; i++) {
				value = random.nextInt(1000000);
				if (value % 2 == 0) {
					writer.write(String.valueOf(value));
					writer.newLine();
					count++;
				}
				if (count % 100 == 0) {
					writer.flush();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (writer != null) {
				try {
					writer.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		if (count > 0) {
			updateFile(filePath, count);
		}
	}

	// 最后更新文件中记录的行数
	private void updateFile(String filePath, int count) {
		RandomAccessFile accessFile = null;
		try {
			accessFile = new RandomAccessFile(filePath, "rw");
			accessFile.writeBytes(filePath);
			accessFile.writeBytes(",");
			accessFile.writeBytes(String.format("%08d", count));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (accessFile != null) {
				try {
					accessFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	//直接用RandomAccessFile生成文件并修改
	public void createFile2() {
		String filePath = "e:/doc/data/abc2.txt";
		RandomAccessFile accessFile = null;
		int max = 10000;
		int count = 0;
		try {
			accessFile = new RandomAccessFile(filePath, "rw");
			accessFile.writeBytes(filePath);
			accessFile.writeBytes(",00000000\r\n");
			Random random = new Random();
			int value = 0;
			for (int i = 0; i < max; i++) {
				value = random.nextInt(1000000);
				if (value % 2 == 0) {
					accessFile.writeBytes(String.valueOf(value));
					accessFile.writeBytes("\r\n");
					count++;
				}
			}
			accessFile.seek(0);
			accessFile.writeBytes(filePath);
			accessFile.writeBytes(",");
			accessFile.writeBytes(String.format("%08d", count));
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (accessFile != null) {
				try {
					accessFile.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

 

其中

accessFile.seek(0);
accessFile.writeBytes(filePath);
accessFile.writeBytes(",");
accessFile.writeBytes(String.format("%08d", count));

 可修改为

accessFile.seek(filePath.length() + 1);
accessFile.writeBytes(String.format("%08d", count));

 或

accessFile.seek(0);
accessFile.skipBytes(filePath.length() + 1);
accessFile.writeBytes(String.format("%08d", count));

 

9.序列流
可以将多个读取流合并成一个流
SequenceInputStream


10.对象的序列化
ObjectInputStream
ObjectOutputStream


11.操作基本数据类型的流对象
DataInputStream
DataOutputStream


12.操作数组的流对象
12.1操作字节数组
ByteArrayInputStream
ByteArrayOutputStream
12.2操作字符数组
CharArrayReader
CharArrayWriter


13.编码转换
本机编码
System.getProperty("file.encoding");
常见码表:
ASCII
ISO8859-1
GB2312
GBK
unicode
UTF-8

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值