2013-03-20 46 views
2

我諮詢過這個社區已經幾次了,但似乎我仍然沒有經驗來解決我自己的問題。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的價值。

然後我需要一個方法,它能夠編輯關鍵字時間的值。

+1

如果你在文件中存儲字符的,我建議你換你流中的InputStreamReader和OutputStreamWriter。你也應該把你的流包裝在BufferedReader/BufferedWriter或者BufferedInputStream/BufferedOutputStream中,這樣你就不必自己做緩衝了。 充分利用[java.io包(包含所有這些類)](http://developer.android.com/reference/java/io/package-summary.html)。 – 2013-03-20 08:49:56

+1

thx給你我的邏輯我會試試,但是這不會解決問題 – Ekonion 2013-03-20 08:52:22

+1

'byte input [] = new byte [fis.available()];'在Javadoc中針對'InputStream.available ()'。 – EJP 2013-03-20 08:53:33

回答

4

根據您所描述的文件內容,這聽起來像一個天然的屬性文件:

# This is a comment: 
lanecount=10001000 
time=12:00 
timex=05:00 
anyword=fhfbjklös 
Stopbyte=0x28 
... ... 

使用java.util.Properties,它爲你所有髒活累活:

# Write properties to private storage: 
FileOutputStream fos = context.openFileOutput(cfg, Context.MODE_PRIVATE); 
Properties props = new Properties(); 
props.setProperty("lanecount", "10001000"); 
props.setProperty("time", "12:00"); 
prop.store(fos, "This is a comment"); // store() is threadsafe 
fos.close(); 

... ... 

# Read properties from private storage: 
FileInputStream fis = context.openFileInput(cfg); 
Properties props = new Properties(); 
props.load(fis); // load() is threadsafe 
fis.close(); 
String lanecount = props.getProperty("lanecount"); 
String time = props.getProperty("time"); 

我不知道您的要求,作爲替代,您可以從SharedPreferences加載/存儲您的配置,而不是應用的私有存儲空間:

# Write properties to shared preferences: 
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 
SharedPreferences.Editor editor = prefs.edit(); 
editor.putString("lanecount", "10001000"); 
editor.putString("time", "12:00"); 
editor.commit(); 

... ... 

# Read properties from shared preferences: 
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
String lanecount = prefs.getString("lanecount", "defauleValue"); 
String time = prefs.getString("time", "defauleValue"); 

希望這會有所幫助。

+0

omg是真正日期... thx很多,將嘗試它..似乎正是我一直在尋找 – Ekonion 2013-03-27 09:12:44

+0

完美答案,thx賞金獎勵 – Ekonion 2013-04-02 08:13:34

+0

omnomnom ...賞金... – Ekonion 2013-04-02 11:08:51

0
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.Properties; 

import android.content.Context; 
import android.os.Environment; 

import com.bowltec.own.OwnClass; 

public class Config_cbt extends OwnClass{ 
    static String cfg = "config.cbt"; 
    static int coll; 
    static Properties props; 

    public static void createcfg() throws FileNotFoundException, IOException { 
     String packageName = acti.getPackageName(); 
     String path = Environment.getExternalStorageDirectory() 
       .getAbsolutePath() + "/Android/data/" + packageName + "/files/"; 
     new File(path).mkdirs(); 
     props = new Properties(); 
    } 

    public static void cfgset(String key, String val) 
      throws IOException { 
     FileOutputStream fos = acti.openFileOutput(cfg, Context.MODE_PRIVATE); 
      props.setProperty(key, val); 
      props.store(fos, "..."); 
    } 

    public static int cfgget(String tocollect) throws IOException { 

     FileInputStream fis = context.openFileInput(cfg); 
     props.load(fis); 
     fis.close(); 
     String collected = props.getProperty(tocollect, "0"); 
     coll = Integer.valueOf(collected); 
     return coll; 
    } 
} 

這就是我現在的樣子。這工作完美。這對所有在同一問題上有問題的人來說都是如此。請隨意使用。

acti在聲明我OwnClass使用protected static Activity acti;acti = this;