的FileInputStream,FileOutputStream中 - 检查文件的特定值/线路/串 [英] FileInputStream, FileOutputStream - check file for specific value/line/string

查看:193
本文介绍了的FileInputStream,FileOutputStream中 - 检查文件的特定值/线路/串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我咨询这个社会已经几次,但尽管如此,它似乎我仍然雏解决我自己我的问题。

I consulted this community already a few times, but nevertheless it seems I'm still to unexperienced to solve my problems on my own.

我最近的问题如下:

我有一个文件,在应用程序(无根设备)的专用存储创建。 I'm能够写入它,并从中读取。但是:我不知道,我怎么在我的文件中添加更多的信息(我认为另一个FOS会很容易地在第一的末尾添加阵列,但我不知道),我不知道,我怎么可以去看看为特定的字符串如:

I have a file, created in the private storage of the app (unrooted device). I´m able to write to it, and to read from it. BUT: I do not know, how i add additional information in my file ( I think another FOS will easily add the array at the end of the first, but am not sure), and I have no idea, how i can have a look for a specific e.g String.

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;

}

这就是我所做的到现在的code。我想添加一个时间字符串值12:00和阅读它们形成不同的方法。该lanecount字符串已经拿到了价值10101010我只想这个值,说搜索字符串 lanecount:并说搜索字符串时间:

This is the code of what I have done till now. I want to add a time String with the value 12:00 and read them form different methods. The lanecount String has got the values 10101010 I just want this values, by saying search for String lanecount: and get the 12:00 by saying search for String time:

编辑:

首先应此行被添加到我的文件。这是它应该是什么样子:

First of all should this lines be added to my file. This is what it should look like:

行1:lanecount:10001000

line1: lanecount: 10001000

2号线:时间:12:00

line2: time: 12:00

3号线:天美时:05:00

line3: timex: 05:00

4号线:anyword:fhfbjklös

line4: anyword: fhfbjklös

LINE5:Stopbyte:0x28

line5: Stopbyte: 0x28



. . .

现在我想的方法,即能拿出关键字的Timex,或者另外一个值,也可以读取lanecount。也许一个能得到Stopbyte的价值。

and now I want a method, that is able to take out the value of Keyword timex, or another one, that can read the lanecount. And maybe one that can get the value of Stopbyte.

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

Then I need a method, which is able to EDIT the value of Keyword time.

推荐答案

根据您所描述的文件内容,这听起来像一个天然的属性文件:

Based on the file content you described, it sounds like a natural properties file:

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

使用 java.util.Properties ,它所有脏活累活为您提供:

Use java.util.Properties, it does all dirty work for you:

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

我不知道您的要求,作为一种替代方法,可以加载/从的共享preferences 的应用程序的专用存储,而不是:

I don't know your requirements, as an alternative, you can load/store your configurations from SharedPreferences instead of app's private storage:

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

希望这有助于。

这篇关于的FileInputStream,FileOutputStream中 - 检查文件的特定值/线路/串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆