我諮詢過這個社區已經幾次了,但似乎我仍然沒有經驗來解決我自己的問題。FileInputStream,FileOutputStream - 檢查文件的具體值/線/字符串
我最新的問題如下:
我有一個文件,在應用程序(無根設備)的私有存儲設備創建的。我可以寫信給它,並從中讀取。但是:我不知道,我如何在我的文件中添加額外的信息(我認爲另一個FOS會輕鬆地在第一個末尾添加數組,但我不確定),我不知道,我該如何看看爲特定的例如字符串。
public static Runnable cfgsetlanecount(int l1, int l2, int l3, int l4,
int l5, int l6, int l7, int l8, Context context)
throws IOException {
System.out.println("" + l1 + l2+ l3+l4+l5+l6+l7+l8);
FileOutputStream fos = context
.openFileOutput(cfg, Context.MODE_PRIVATE);
String lanecount = "lanecount: " + (Integer.valueOf(l1).toString())
+ "" + (Integer.valueOf(l2).toString()) + ""
+ (Integer.valueOf(l3).toString()) + ""
+ (Integer.valueOf(l4).toString()) + "" + l5 + "" + l6 + ""
+ l7 + "" + l8;
byte buf[] = lanecount.getBytes();
fos.write(buf);
fos.close();
cfggetlanecount();
return null;
}
public static Runnable cfggetlanecount() throws IOException {
String collected = null;
FileInputStream fis = context.openFileInput(cfg);
byte input[] = new byte [fis.available()];
while (fis.read(input)!=-1){
collected = new String (input);
System.out.println(collected);
}fis.close();
return null;
}
這是我迄今爲止所做的代碼。我想添加一個time
字符串值爲12:00並從不同的方法讀取它們。該lanecount字符串已經拿到了價值10101010我只想這個值,說搜索字符串lanecount:
和說搜索字符串獲取12:00 time:
編輯:這應該添加
首先行到我的文件。這是它應該是什麼樣子:
線路1:lanecount:10001000
線路2:時間:12:00
3號線:天美:05:00
4號線:anyword:fhfbjklös
line5:Stopbyte:0x28
。 。 。
現在我想要一個方法,它可以取出關鍵字timex的值,或者另一個可以讀取lanecount的值。也許可以得到Stopbyte的價值。
然後我需要一個方法,它能夠編輯關鍵字時間的值。
如果你在文件中存儲字符的,我建議你換你流中的InputStreamReader和OutputStreamWriter。你也應該把你的流包裝在BufferedReader/BufferedWriter或者BufferedInputStream/BufferedOutputStream中,這樣你就不必自己做緩衝了。 充分利用[java.io包(包含所有這些類)](http://developer.android.com/reference/java/io/package-summary.html)。 – 2013-03-20 08:49:56
thx給你我的邏輯我會試試,但是這不會解決問題 – Ekonion 2013-03-20 08:52:22
'byte input [] = new byte [fis.available()];'在Javadoc中針對'InputStream.available ()'。 – EJP 2013-03-20 08:53:33