public static String readString(String filePath,String fileName,int pos) {
try {
File file = new File(filePath+fileName);
if(!file.exists()){
Log.e(TAG,"文件不存在");
}
FileInputStream stream = null;
stream = new FileInputStream(file);
int len = 1;//读几个字节
stream.skip(pos); //跳过pos个字节数
byte[] b = new byte[len];
stream.read(b);
String s = new String(b);
Log.e(TAG, " 读取了 " + new String(b));
stream.close();
return s;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
这里说明一下 第一个参数是文件路径比如(abc/cba/),第二个是文件的名字比如(test.txt) 第三个参数是跳过的位数比如(你想读取第5位 那 pos=4)读取一位 就是第五位。
**
举个栗子
**
一个文件是由十万个0和十万个1组成的,你只想要第10个,你每次不可能把全部都读取出来再计算吧,这样也太耗时了吧。
目前这是我的解决办法,如果有大佬有更好的帮忙留言一下,谢谢。