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個,你每次不可能把全部都讀取出來再計算吧,這樣也太耗時了吧。
目前這是我的解決辦法,如果有大佬有更好的幫忙留言一下,謝謝。