2

我已经咨询了这个社区几次,但似乎我仍然没有经验自己解决我的问题。

我的最新问题如下:

我有一个文件,在应用程序的私有存储(无根设备)中创建。我能够写入它,并从中读取。但是:我不知道,我如何在我的文件中添加其他信息(我认为另一个 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:00time:

编辑:

首先应该将此行添加到我的文件中。这应该是这样的:

第1行:车道数:10001000

line2:时间:12:00

第 3 行:timex:05:00

第 4 行:任何字词:fhfbjklös

第 5 行:停止字节:0x28

. . .

现在我想要一种方法,它能够取出关键字 timex 的值,或者另一个可以读取车道计数的方法。也许一个可以获得 Stopbyte 的价值。

然后我需要一个方法,它能够编辑关键字时间的值。

4

2 回答 2

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");

希望这可以帮助。

于 2013-03-26T22:01:01.140 回答
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;

于 2013-04-02T10:18:16.273 回答