一. DataInput接口
DataInput接口提供了一系列的方法从二进制流中读取字节,并将读取出来的字节转换成任意的java基本类型,包括转换成UTF-8类型的字符串。
该接口中主要方法介绍如下:
(1)void readFully(byte b[]) throws IOException;
从input stream中读取 b.length 长度的字节,并将其存储到b中。
@Test public void testReadFully() throws IOException { // test.txt文件中的内容就是abcdefghijklmn RandomAccessFile dataInput = new RandomAccessFile("E:\\360downloads\\wpcache\\srvsetwp\\test.txt", "r"); byte[] buf = new byte[5]; dataInput.readFully(buf); System.out.println(new String(buf)); // abcde
以上代码,运行正常,将流中前5个字节读取到了buf中。下面看一个异常情形
(2) int skipBytes(int n) throws IOException;
跳过n个字节,与InputStream接口中的skip(n)一样
@Test public void testSkipBytes() throws IOException { // test.txt文件中的内容就是abcdefghijklmn RandomAccessFile dataInput = new RandomAccessFile("E:\\360downloads\\wpcache\\srvsetwp\\test.txt", "r"); dataInput.skipBytes(5); byte[] buf = new byte[5]; dataInput.readFully(buf); System.out.println(new String(buf)); // fghij }
(3)boolean readBoolean() throws IOException;
从流中读取一个字节,如果这个字节是非零,返回true,否则返回false。 该方法适用读取DataOutput#writeBoolean()写出的字节。
@Test public void testReadBoolean() throws IOException { // test.txt文件中的内容就是abcdefghijklmn RandomAccessFile dataInput = new RandomAccessFile("E:\\360downloads\\wpcache\\srvsetwp\\test.txt", "r"); while (true) { System.out.println(dataInput.readBoolean()); } }
打印结果如下:
true true true true true true true true true true true true true true java.io.EOFException at java.io.RandomAccessFile.readBoolean(RandomAccessFile.java:644) at io.TestDataInput.testReadBoolean(TestDataInput.java:33) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
前面14个打印true,input中的数据读完后,就抛EOF
下面再看一个示例
@Test public void testReadBoolean2() throws IOException { RandomAccessFile data = new RandomAccessFile("E:\\360downloads\\wpcache\\srvsetwp\\test1.txt", "rw"); // 使用writeBoolean()方法将false写到test1.txt文件 data.writeBoolean(false); // 再写个int data.writeInt(5); data.close(); data = new RandomAccessFile("E:\\360downloads\\wpcache\\srvsetwp\\test1.txt", "r"); // 使用readBoolean()读取test1.txt文件中的内容 System.out.println(data.readBoolean());// true // 再读int System.out.println(data.readInt()); // 5 }
(4) byte readByte() throws IOException;
读一个byte, 值范围是-128~127, 适合于读取DataOutput#writeByte
@Test public void readByte() throws Exception{ // test.txt文件中的内容就是abcdefghijklmn RandomAccessFile input = new RandomAccessFile("E:\\360downloads\\wpcache\\srvsetwp\\test.txt", "r"); // 从input中读取一个字节 byte b = input.readByte(); System.out.println(b);//97 (a的unicode就是97) }
(5) int readUnsignedByte() throws IOException;
读取一个字节,并将其转换成int类型返回,范围0~255 , 也适合于读取DataOutput#writeByte
(6)其它方法也都是类似的。。。
日拱一卒无有尽,功不唐捐终入海