Java的一些高级特性(七)——Java7中的I/O

本文通过示例介绍了Java中字节流、字符流及带缓冲区的流操作方法,并展示了如何使用Scanner类处理带分隔符的文本文件。

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

首先我们来看看一个简单的Byte Stream操作。程序在循环中每次读取一个字节和写入一个字节,最后完成文件的拷贝操作。注意try...with段和in.read()/out.write()方法:

package com.freesoft.java7newfeature;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class TestByteStream {
	public static final int BUF_SIZE = 1024;
	public static void main(String[] args) {
		try (
				FileInputStream in = new FileInputStream("files/tbs/source.png");
				FileOutputStream out = new FileOutputStream("files/tbs/target.png");){
			int c = 0;
			while((c = in.read()) != -1) {
				out.write(c);
			}
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

下面我们看看字符流的操作。我们这里使用文本文件来作为源和目标,可以看到reader.read()和writer.write()每次读取和写入都是一个字符:

package com.freesoft.java7newfeature;

import java.io.FileReader;
import java.io.FileWriter;

public class TestCharacterStream {
	public static final int BUF_SIZE = 1024;
	public static void main(String[] args) {
		try (
				FileReader reader = new FileReader("files/tbs/source.txt");
				FileWriter writer = new FileWriter("files/tbs/target.txt");){
			int c = 0;
			while((c = reader.read()) != -1) {
				writer.write(c);
			}
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

如果我们需要使用缓冲区,我们就需要使用BufferedReader和BufferedWriter(如果是二进制读写则使用BufferedInputStream和BufferedOutputStream,使用了wrap pattern):

package com.freesoft.java7newfeature;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class TestBufferedCharStream {
	public static final int BUF_SIZE = 1024;
	public static void main(String[] args) {
		try (
				BufferedReader reader = new BufferedReader(new FileReader("files/tbs/source.txt"));
				BufferedWriter writer = new BufferedWriter(new FileWriter("files/tbs/target.txt"));){
			int c = 0;
			while((c = reader.read()) != -1) {
				writer.write(c);
			}
			writer.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

最后我们看一下在Java7中如何操作有分隔符的文件,这里使用了一个sanner类:

package com.freesoft.java7newfeature;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Scanner;

public class TestTokenizedText {
	public static void main(String[] args) {
		try (
				Scanner s = new Scanner(new BufferedReader(new FileReader("files/tbs/source.txt")));
				){
			// 此处可以使用各种参数,看文件是使用什么形式分割
			s.useDelimiter("\r\n");
			while(s.hasNext()) {
				System.out.println(s.next());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}










Input/output (I/O) is not a sexy subject, but it’s an important part of non-trivial applications. This book introduces you to most of Java’s I/O capabilities as of Java 8 update 51. Chapter 1 presents a broad overview of I/O in terms of Java’s classic I/O, New I/O (NIO), and NIO.2 categories. You learn what each category offers in terms of its capabilities, and you also learn about concepts such as paths and Direct Memory Access. Chapters 2 through 5 cover classic I/O APIs. You learn about the File and RandomAccessFile classes along with streams (including object serialization and externalization) and writers/readers. Chapters 6 through 11 focus on NIO. You explore buffers, channels, selectors, regular expressions, charsets, and formatters. (Formatters were not introduced with the other NIO types in Java 1.4 because they depend on the variable arguments capability that was introduced in Java 5.) NIO is missing several features, which were subsequently provided by NIO.2. Chapters 12 through 14 cover NIO.2’s improved file system interface, asynchronous I/O, and the completion of socket channel functionality. Each chapter ends with assorted exercises that are designed to help you master its content. Along with long answers and true/false questions, you are often confronted with programming exercises. Appendix A provides the answers and solutions. Appendix B provides a tutorial on sockets and network interfaces. Although not directly related to classic I/O, NIO, and NIO.2, they leverage I/O capabilities and are mentioned elsewhere in this book.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值