日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java.lang包怎么用_java.lang.io包的使用

發(fā)布時間:2025/4/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java.lang包怎么用_java.lang.io包的使用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1 String source = "ABCDEF123456";2 int mid = source.length() / 2;3

4 ByteArrayInputStream bytesIS = newByteArrayInputStream(source.getBytes());5

6 //1. 順序讀取流

7 int b = 0;8 while( -1 != (b =bytesIS.read())){9 System.out.print(Integer.toHexString(b) + " ");10 }11 System.out.println();12

13 //2. 讀取完畢之后 調(diào)用 read 始終返回 -1

14 b =bytesIS.read();15 System.out.println(b);16

17 //對流進(jìn)行重置

18 int index = 0;19 bytesIS.reset();20 while( -1 != (b =bytesIS.read())){21 if(index++ == mid &&bytesIS.markSupported()){22 bytesIS.reset();23 }24 System.out.print(Integer.toHexString(b) + " ");25 }26 System.out.println();27

28 //標(biāo)記流

29 index = 0;30 bytesIS.reset();31 while( -1 != (b =bytesIS.read())){32 if(index++ == mid &&bytesIS.markSupported()){33 //bytesIS.reset();

34 bytesIS.mark(0);35 }36 System.out.print(Integer.toHexString(b) + " ");37 }38 System.out.println();39 bytesIS.reset();40 while( -1 != (b =bytesIS.read())){41 System.out.print(Integer.toHexString(b) + " ");42 }43 System.out.println();44 bytesIS.reset();45 while( -1 != (b =bytesIS.read())){46 System.out.print(Integer.toHexString(b) + " ");47 }48 System.out.println();49

50 //1. 讀取文件 使用 FileInputStream 只能

51 File file = new File("e:/test.txt");52 System.out.println(file.length());53 FileInputStream FileIS = newFileInputStream(file);54 while( -1 != (b =FileIS.read())){55 System.out.print("0x" + Integer.toHexString(b) + " ");56 }57 System.out.println();58

59 //可以使用seek方法來隨機(jī)的讀取文件,但是對于 RandomAccessFile,并不是流類,

60 String mode = "r";61 RandomAccessFile randomFile = newRandomAccessFile(file, mode);62 while( -1 != (b =randomFile.read())){63 System.out.print("0x" + Integer.toHexString(b) + " ");64 }65 System.out.println();66 System.out.println(randomFile.getFilePointer());67 System.out.println(randomFile.length());68 randomFile.seek(6);69 while( -1 != (b =randomFile.read())){70 System.out.print("0x" + Integer.toHexString(b) + " ");71 }72 System.out.println();73 randomFile.seek(0);74 while( -1 != (b =randomFile.read())){75 System.out.print("0x" + Integer.toHexString(b) + " ");76 }77 System.out.println();78

79 //java.lang包中有一個文本描述的工具類 但是其提供的功能也是順序讀取。80 //java.util.Scanner

81 Scanner scanner = newScanner(file);82 System.out.println(scanner.next());83 scanner =scanner.reset();84

85 //java.io.PushbackInputStream 過濾流86 //這個流提供了 unread 方法, 回退到緩沖區(qū),

87 bytesIS.reset();88 PushbackInputStream pis = newPushbackInputStream(bytesIS);89 pis.unread(0x30);90 System.out.println(Integer.toHexString(pis.read()));91 System.out.println(Integer.toHexString(pis.read()));92

93 //到目前為止,只有流類基本上只能按流的順序來讀寫。

94 ByteArrayOutputStream bytesOutPut = newByteArrayOutputStream();95 bytesOutPut.write(source.getBytes());96 System.out.println(bytesOutPut.toString());97 bytesOutPut.write(0x30);98 System.out.println(bytesOutPut.toString());99

總結(jié)

以上是生活随笔為你收集整理的java.lang包怎么用_java.lang.io包的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。