一. 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)其它方法也都是類似的。。。