0
我找不到相關的(即使有語法分析問題的hundrets)什麼:解析TXT文件信息以整數
到很老的網站計數器一樣,我喜歡存放在一個txt一些文件在SD卡上。每次在應用程序中激活一個方法時,它應該讀取一個txt文件的編號,然後加1並再次保存。
如果我嘗試下面的代碼,它創建一個奇怪的字符串,沒有INT。如何解決這個問題呢? :-(
FileWriter writer;
File file;
FileInputStream fileIS;
public void implementCounter()
{
File sdcard = Environment.getExternalStorageDirectory();
int counter = 0;
try{
File f = new File(sdcard +"/Experiment/Counter.txt");
fileIS = new FileInputStream(f);
BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
//counter = Integer.parseInt(buf.readLine());
String readString = new String();
//just reading each line and pass it on the debugger
/*while((readString = buf.readLine())!= null){
Log.d("line: ", readString);
}*/
readString = buf.readLine();
try {
counter = Integer.parseInt(readString.toString());
} catch(NumberFormatException nfe) {
Log.d("line: ", "XXX: " + readString + "E");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
//Create/Write LogFile
file = new File(sdcard + "/Experiment/Counter.txt");
try
{
counter++;
writer = new FileWriter(file ,false);
writer.write(counter);
writer.flush();
writer.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
的想法是,新的FileWriter簡單的覆蓋舊文件與一個數字更高。
爲什麼你要將它保存在一個文件,你還好嗎只保存它以這樣一種方式即使應用程序更新,它也會持續存在!! – 2012-04-23 12:09:53
是的,當然:-) – user1351229 2012-04-23 12:34:13