Java fundamentals of basic IO
IO is a problem difficult to handle in various of systems because it always becomes a bottleneck in data transfer. in this section, I will introduce some Java classes for two categories of data transfer, Bytes and Characters, those classes were included in java.io package, then I will invoke some methods in java.nio.file.Files class, then the section looks at the mechanism of NIO.2 (non blocking IO), which with advantage feature in multiple concurrent.
let's list out the agenda for this section.
IO Stream (introduce interfaces/classes in interface java.io)
-Bytes IO
-Characters IO
-Buffered IO
NIO (introduce methods in class java.nio.file.Files)
-buffer
-channel
NIO.2
-Asynchronous io (non blocking IO)
IO Stream
Bytes Stream
Java uses 8-bit (Byte) as a basic unit in program, this is Bytes String. all of the class to handle Bytes String are descended fromInputString andOutputtring.
an important thing to use Byte String is always remember to close the Strings when no longer use them.
Bytes String is easy to be understood by machine and it is always used to handle those primitive/raw data (binary), but not appropriate to handle more complicated data - such as character, witch is more easier to be understood by human, thus we have another
type of String - Characters String.
example to demostrate how to use Byte Stream
example
package ioStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class ByteStream {
public static void CopyBytes() throws IOException {
/*
* subclss of java.io.InputStream, use them to manipulate files by
* Bytes.
*/
FileInputStream in = null;
FileOutputStream out = null;
try { out = new FileOutputStream("output.txt");
// byte range : -128 ~ +128
// java array is an object, use new to initialise (malloc)
byte[] buf = new byte [] { 50, 0, -1, 28, -24 };
for (byte b : buf) {
out.write(b);
} in = new FileInputStream("output.txt");
out = new FileOutputStream("copyOutput.txt");
int c;
// in.read will return the next byte of data, or -1 if the end of the file is reached
while ((c = in.read()) != -1) {
out.write(c);
} } finally {
// never forget to close a ByteString instance if no longer need them
if (in != null) {
in.close();
} if (out != null) {
out.close();
}
}
}
}
Characters Stream
Each Characters String uses 16-bit as a basic unit in transfer, Characters String not only more appropriate in human understanding but also resolve the problem of internationalization. Java platform stores data with Unicode conventions, Characters String
will automatically converts its internal characters set to and from locale characters set, so you will no longer need to care the internationalization if using Characters String.
All of Characters String handler are descended from Reader andWriter.
InputStreamReader and OutputStreamWriter can be used to convert Bytes String to Characters String, for example, in sockets communication, we offten create character streams from the byte streams provided by socket classes.
Buffered IO
In general, String will be communicated directly with OS underlying IO, reading or writing will directly handle by OS and it makes a program much less efficient, to reduce this kind of overhead, Java invoke Buffer mechanism to handle IO String. Java platform
implements a buffered IO String, a buffer is a memory region, the native input API is called only when the buffer is empty, and the native output API is called only when the buffer is full.
Both Bytes String and Characters String can be buffed, the following four classes are used to be handle it: BufferedInputStream andBufferedOutputStream create buffered byte streams, whileBufferedReader andBufferedWriter
create buffered character streams.
BufferReader/BufferWriter can convert an un-buffered string to a buffered String,
inputStream = new BufferedReader(new FileReader("xanadu.txt"));
outputStream = new BufferedWriter(new FileWriter("characteroutput.txt"));
a example to demostrate FileReader, BufferedReader, FileWriter, PrintWriter
package ioStream; import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; public class CharStream {
public static void CopyChars() throws IOException {
FileReader inputStream = null;
FileWriter outputStream = null;
try {
char[] buf = new char[] {'a','b','c'};
outputStream = new FileWriter("output.txt");
outputStream.write(buf);
outputStream.close();
inputStream = new FileReader("output.txt");
int c;
while ((c = inputStream.read()) != -1) {
System.out.println(c);
} } finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
} public static void CopyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;
char[] buf = new char[] {'d','e','f'};
try {
outputStream = new PrintWriter(new FileWriter("output.txt"));
outputStream.println(buf);
outputStream.close();
inputStream = new BufferedReader(new FileReader("output.txt"));
String strLine;
while ((strLine = inputStream.readLine()) != null) {
System.out.println(strLine);
}
} finally {
if (outputStream != null) outputStream.close();
if (inputStream != null) inputStream.close();
}
}
}
NIO
above IO String we call them traditional IO, they all transfer with a byte unit in the underlying, this is much less efficient. since JDK 1.4, Java platform invoke the NIO as replace of traditional IO. NIO means new IO or non-blocking IO, the difference
between traditional IO and NIO is the NIO using memory mapping to speed up reading and writing, at the same time, the non-blocking mechanism also resolve the IO string blocking in multiple IO.
There are several packages defined in NIO, in this section we only focus on two of them.
-java.nio.-defines the bufferd
-java.nio.channel.-defines channel and selector
File Class
buffer
buffer looks like an array which stores several same type data. Buffer is just an abstract base class, there are many subclass extend it, the most common subclasses are ByteBuffer and CharBuffer.
There are three essential properties for buffer, there are capacity, limit and position, each subclasses of buffer defines two categories of get and put operations.
-Relative operations;
-Absolute operations;
buffers are not safety for use by multiple concurrent threads.
buffers can be set to read-only by invoking isReadOnly method.
the flip() method sets the limit to the position and set the position to 0, thus the buffer pointer moves to the start place, that means after invocation of flip(), the buffer prepare for outputting.
after outputting, Buffer invoke the clear() method, it is not for clearing up buffer data, while it set limit to capacity and the position to 0 and prepare for inputting data into Buffer.
channel
channel looks like a string object of traditional IO, Channel directly maps the partial or entire of a file to a Buffer, program unable to access data directly via channel, instead, program need to use a buffer to retrieve data from a channel, then the
program read data from the buffer.
Java provides different implemented class by their functionality, such as FileChannel, SockChannel, etc. all types of channel instance should not be built by invoking their constructor method, instead, they should be obtained from getChannel() method byinvoking
its specific class. for example, a FileChannel instance should be obtained by FileInputString.getChannel() as blow
FileChannel inChannel = new FileInputString(new File("xxx.txt")).getChannel()
below example demonstrates how to use File, Buffer and Channel
NIO.2
jdk 1.7 made significant enhancements, the major changes are
- provides comprehensive supports in file IO and file system accessing.
- Asynchronous IO
in the section I will just introduce the fist Item which was introduced in java.nio.file package in JDK1.7, the Asynchronous will be introduced in network communication.
The Path interface was invoked to NIO.2 to resolve the problems that the defect in old File classes, including cannot working in other OS, low performance and ambiguous error message, etc. NIO.2 provides two tool classes, thePath
and Files.
Paths
A Paths object contains the file name and directory list used to construct the path, and is used to examine, locate, and manipulate files.
A Path is not system independent, that means you can manipulate files with the same method in all system, that's cool1
Paths class offers various methods to manipulate a path including creating,removing, comparing, joining and retrieving information, etc. here list out the major methods.
- Paths.get("/tmp/foo"); // create a path. it is the shorthand for FileSystems.getDefault().getPath("/users/sally");
- path.getFileName(),path.subpath(0,2),path.getParent(),path.getRoot(). // retrieving information
- path.equals(otherPath),path.startsWith(beginning),path.endsWith(ending)... //comparing
Files
The Files class is another primary entrypoint of the java.nio.file package. the Files class works on instance of Paths object and offers a rich set of static methods for reading, writing and manipulating files and directories. here list out the out operations
of Files class.
copy(InputStream in, Path target, CopyOption... options)
createDirectories(Path dir, FileAttribute<?>... attrs)
createFile(Path path, FileAttribute<?>... attrs)
delete(Path path)
find(Path start, int maxDepth, BiPredicate<Path,BasicFileAttributes> matcher, FileVisitOption... options)
lines(Path path) //Read all lines from a file as a Stream.
write(Path path, byte[] bytes, OpenOption... options) //Writes bytes to a file.
a example to demonstrate the Files class
package fileIO; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Iterator;
import java.util.List; public class FileOps { public static void readAllBytes() throws IOException {
Path path = Paths.get("output.txt");
byte[] buf = new byte[] { 'a', 'b', 'c' };
Files.write(path, buf, StandardOpenOption.CREATE);
byte[] readByte;
readByte = Files.readAllBytes(path);
for (byte b : readByte) {
System.out.println(b);
}
} public static void readAllLines() throws IOException {
// TODO Auto-generated method stub
Path path = Paths.get("output.txt");
byte[] buf = new byte[] { 'r', 't', 'y' };
Files.write(path, buf, StandardOpenOption.CREATE);
char[] readChar;
Charset cs = Charset.defaultCharset(); List<String> listStr = Files.readAllLines(path, cs);
Iterator it = listStr.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
} public static void readBuffer() throws IOException {
Path path = Paths.get("output.txt");
Charset cs = Charset.defaultCharset();
// no need to close out if put it implicitly in to try () for java7
try (BufferedWriter out = Files.newBufferedWriter(path, cs,
StandardOpenOption.CREATE)) {
char[] buf = new char[] { 'a', 'p', 'l' };
out.write(buf);
out.close();
} String strLine;
try (BufferedReader in = Files.newBufferedReader(path, cs)) {
while ((strLine = in.readLine()) != null) {
System.out.println(strLine);
}
}
} /*
* use the newInputStream(Path, OpenOption...) method. This method returns
* an unbuffered input stream for reading bytes from the file.
*/
public static void readStream() throws IOException{
Path path = Paths.get("output.txt");
byte[] buf = new byte[] { 'b', 's', 'a' }; /*
*here will write file in unbuffered model
*
/*
try (OutputStream out = Files.newOutputStream(path,
StandardOpenOption.CREATE)) {
out.write(buf);
} catch (IOException e) {
throw e;
}
*/ /*
* Files.newOutputStream returns a OutputStream obj which will not
* BufferedOutputStream can handle bytes stream in buffer model
*/
/*
try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(
path, StandardOpenOption.CREATE))) {
out.write(buf);
} catch (IOException e) {
throw e;
}
*/ /*
* BufferedWriter extends from java.io.Writer, returns and buffers chars stream
* OutputStreamWriter also extends from java.io.Writer, is a bridge from byte streams to char streams
* Files.newOutputStream(path) returns an unbuffered OutputStream (bytes stream)
*/
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(Files.newOutputStream(path)))) {
char[] cbuf = new char[] {'h','p','f'};
writer.write(cbuf);
} catch (IOException e) {
throw e;
} /*
* BufferedReader extends from java.io.Reader, returns and buffers chars stream
* InputStreamReader extends from java.io.Reader, is a bridge from byte streams to character streams
* Files.newInputStream(path) returns a unbuffered InputStream (bytes stream)
*/
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Files.newInputStream(path)))) {
String strLine;
while ((strLine = reader.readLine()) != null) {
System.out.println(strLine);
}
} catch (IOException e) {
// TODO Auto-generated catch block
throw e;
}
} public static void readChannel() throws IOException {
Path path = Paths.get("output.txt");
try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.CREATE) ) {
//can not read file by Channel directly, instead, need to read into Buffer first
ByteBuffer buf = ByteBuffer.allocate(10);
String encoding = System.getProperty("file.encoding");
while (sbc.read(buf) > 0) {
buf.rewind();
System.out.print(Charset.forName(encoding).decode(buf));
buf.flip();
}
} catch (IOException e){
throw e;
} }
}
Java fundamentals of basic IO的更多相关文章
- Java socket中关闭IO流后,发生什么事?(以关闭输出流为例)
声明:该博文以socket中,关闭输出流为例进行说明. 为了方便讲解,我们把DataOutputstream dout = new DataOutputStream(new BufferedOutpu ...
- 二十四、JAVA的NIO和IO的区别
一.JAVA的NIO和IO 1.NIO:面向缓冲区(buffer)(分为非阻塞模式IO和阻塞模式IO)组成部分:Channels管道,Buffers缓冲区,Selectors选择器 2.IO:面向流( ...
- java的高并发IO原理,阻塞BIO同步非阻塞NIO,异步非阻塞AIO
原文地址: IO读写的基础原理 大家知道,用户程序进行IO的读写,依赖于底层的IO读写,基本上会用到底层的read&write两大系统调用.在不同的操作系统中,IO读写的系统调用的名称可能不完 ...
- java 提供了哪些IO方式
今天听了杨晓峰老师的java 36讲,感觉IO这块是特别欠缺的,所以讲义摘录如下: 欢迎大家去订阅: 本文章转自:https://time.geekbang.org/column/article/83 ...
- java 发送带Basic Auth认证的http post请求实例代码
构造http header private static final String URL = "url"; private static final String APP_KEY ...
- Java中 NIO与IO的区别
当学习了Java NIO和IO的API后,一个问题马上涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们如何影响您的代 ...
- Java实验1-文件IO
目标:掌握Java类的创建,Java I/O操作,Java集合类的使用等 内容: 王老师非常喜欢读书,为了便于查阅,他每次买书回家后就在笔记本上登记每本书的详细信息(书名.作者.出版社.出版日期.价 ...
- 使用Java的多线程和IO流写一个文件复制功能类
创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...
- Java I/O Basic
/* 记住每个类相应的用法*/流的分类: io包内定义了所有的流 分类: 方向:输入流.输出流 处理数据单位:字节流.字符流 功能不同:节点流.处理流 所有流类型,位于java.io包内,分别继承以下 ...
随机推荐
- 为benchmarksql的PostgreSQL java驱动进行升级
为benchmarksql的PostgreSQL java驱动进行升级[root@minion1 benchmarksql-4.1.0]# wget https://jdbc.postgresql.o ...
- Lintcode: Majority Number III
Given an array of integers and a number k, the majority number is the number that occurs more than 1 ...
- [原创] 关于quartz (spring 中的任务调度器)时间配置
1. CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 ...
- SQL静态
静态 1.普通成员普通成员都是属于对象的用对象调用 2.静态成员静态成员是属于类的用类名调用 class FenBi{public int length;//普通成员public string col ...
- 最新RubyMine2016.2开发Ruby ON Rails(ROR)程序的流程
1.RubyMine新建ROR工程 File->New Project 选择Rails下的"New Application" 点击OK 后生成ROR项目 ...
- linux centos5.7(32bit) oracle 10g oracle11g
cenOS5.5安装oracle10g(傻瓜篇) http://www.cnblogs.com/fnng/archive/2012/06/19/2554159.html (转) 在cenOS5.5上 ...
- (转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates
Setting Display Formats Using Microsoft Excel: Right-click on any desired cell and select Format Cel ...
- 有关dwr推送的笔记
想做一个web推送相关的东东,昨天搞了一天,终于把这些杂乱的配制弄清了,今天写出来方便以后记住,也方便大家看一下吧 1:引入dwr包,我用的是maven <dependency> < ...
- 夺命雷公狗---Thinkphp----12之文章的增删改查(图片上传和关联查询)
我们由于表分析的不够完善,所以我们来加多一个tid的字段,到时候主要目的是为了更好的遍历出文章是属于那个分类下的,表如下所示: 那么下一步我们就开始创建一个ArticleController.clas ...
- vim多行缩进的方法
在visual模式下选中要缩进的行,然后按>