JAVA IO流RandomAccessFile随机读取
来源:互联网 发布:不是淘宝卖家能贷款吗 编辑:程序博客网 时间:2024/05/18 00:59
<pre name="code" class="java">public class BufferreadDemo {static long n;//定义静态变量n为了记录每次读取的文件指针//本文以从文件中读取字符串并转化为整形为例说明public int[] transform(int size) {String s =null;//将文件读取操作全部放到try catch中以防止每次调用都要抛出异常try {//实现文件的随机读取RandomAccessFile in = new RandomAccessFile("./data/realdata.txt","r");//从第n个位置开始读取文件(n是上次读取后文件指针的位置) in.seek(n); //从文件中读出字符串 s = in.readLine(); //将此次读取后文件的指针位置赋给n为了便于下次从此处开始读取 n=in.getFilePointer(); } catch(IOException e){ e.printStackTrace(); } //System.out.println(n);if(s!=null){System.out.println("读出的字符串是:"+s);//每行字符串的长度int le=s.length();//System.out.println("读出的字符串长度是:"+le);byte[] byteBuffer = new byte[le];int[] a = new int[size];int count = 0;//将String转换为byte数组byteBuffer = s.getBytes();for(int i=0;i<le;i++){int x = byteBuffer[i];}System.out.println();for(int i=0;i<le;i++){if (byteBuffer[i]>0&&byteBuffer[i]<256){a[count] = byteBuffer[i];count++;}else{System.out.println("數據異常!");}}return a;}else{System.out.println("已經到文件末尾");n=0;return null;}}}//定义测试主函数public class TestDemo {public static void main(String[] args) throws IOException {int [] a = new int[20];//实现两次读取看是否是从本次读取后的位置开始读取a= new BufferreadDemo().transform();for(int n:a){System.out.print(n+" ");}a= new BufferreadDemo().transform();for(int n:a){System.out.print(n+" ");}System.out.println();String s = new BufferreadDemo().transString(a);System.out.println(s);}}
0 0
- JAVA IO流RandomAccessFile随机读取
- 24-IO流-60-IO流(RandomAccessFile-读取&随机读取)
- java IO RandomAccessFile的写入和随机读取
- Java RandomAccessFile随机读取文件
- IO操作-随机读取文件RandomAccessFile
- IO流--随机访问(RandomAccessFile)
- 随机文件读取流RandomAccessFile
- (原创)RandomAccessFile随机读取流
- Java IO流->处理流->“随机访问” 的方式:RandomAccessFile
- Java IO流之随机读写流RandomAccessFile
- [Java核心API]IO流--随机访问文件RandomAccessFile
- Java IO RandomAccessFile随机读写文件用法
- java IO流:RandomAccessFile
- IO流(RandomAccessFile随机访问流)
- IO流--RandomAccessFile--随机访问文件
- IO之随机读写流RandomAccessFile
- java: 随机访问流RandomAccessFile
- java RandomAccessFile随机读写流
- hdu 3591 The trouble of Xiaoqian(多重背包)
- 【项目3 - 体验复杂度之二 复杂度为O(nlogn) 的快速排序quicksort】
- 关于Go
- Android connect函数hook
- A Word Can Make You Miss Your Deadline
- JAVA IO流RandomAccessFile随机读取
- Xamarin.Android使用教程之简介和安装
- 第二周 项目1 c/c++语言中函数参数传递的三种方式
- C# 执行存储过程
- Androidz之clickable
- 2014 07 29 写在七月的尾巴
- java web开发 高并发处理
- 第二周项目三多文件程序
- C++Primer第五版 3.2.3节练习