【问题标题】:FileInputStream, FileOutputStream - check file for specific value/line/stringFileInputStream, FileOutputStream - 检查文件的特定值/行/字符串
【发布时间】:2013-03-20 08:19:43
【问题描述】:

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

我最近遇到的问题如下:

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

}

这是我到目前为止所做的代码。我想添加一个值为 12:00 的 time 字符串,并从不同的方法中读取它们。 Lanecount 字符串的值是 10101010 我只想要这个值,通过搜索字符串 lanecount: 并通过搜索字符串 time: 来获取 12:00

编辑:

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

line1:lanecount:10001000

line2:时间:12:00

line3: timex: 05:00

line4: 任意词:fhfbjklös

第 5 行:停止字节:0x28

。 . .

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

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

【问题讨论】:

  • 如果您在文件中存储字符,我建议您将流包装在 InputStreamReader 和 OutputStreamWriter 中。您还应该将流包装在 BufferedReader/BufferedWriter 或 BufferedInputStream/BufferedOutputStream 中,这样您就不必自己进行缓冲。充分利用java.io package (which contains all these classes)
  • 感谢逻辑学家,我会尝试这样做,但这不会解决问题
  • byte input[] = new byte [fis.available()];InputStream.available() 的Javadoc 中有一个针对这种用法的特定警告。
  • 我刚刚添加了available 标签,因为它在我看到的教程中完成。现在它可以工作,但将来我想要特定数量的字符(在lanecount案例8中)所以我会定义它们,但是谢谢EJP
  • 一段时间后文件的内容会是什么样子?

标签: java android search text-files key-value


【解决方案1】:

根据您描述的文件内容,它听起来像是一个自然属性文件:

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

希望这会有所帮助。

【讨论】:

  • 天哪,真是过时了...谢谢很多,会尝试一下..似乎正是我一直在寻找的东西
【解决方案2】:
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; 声明

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    相关资源
    最近更新 更多