简体   繁体   English

FileInputStream,FileOutputStream-检查文件中的特定值/行/字符串

[英]FileInputStream, FileOutputStream - check file for specific value/line/string

I consulted this community already a few times, but nevertheless it seems I'm still to unexperienced to solve my problems on my own. 我已经向该社区咨询过几次,但是似乎我仍然没有经验来自行解决问题。

My latest problem is the following: 我的最新问题如下:

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 eg String. 但是:我不知道如何在文件中添加其他信息(我认为另一个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;

}

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. 我想添加一个值为12:00的time字符串,并以不同的方法读取它们。 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: lanecount字符串的值是10101010,我只想要此值,方法是说搜索String lanecount:并说12:00来搜索String time:

EDIT: 编辑:

First of all should this lines be added to my file. 首先,应将此行添加到我的文件中。 This is what it should look like: 它应该是这样的:

line1: lanecount: 10001000 第1行:车道数:10001000

line2: time: 12:00 第2行:时间:12:00

line3: timex: 05:00 第3行:timex:05:00

line4: anyword: fhfbjklös 第4行:anyword:fhfbjklös

line5: Stopbyte: 0x28 第5行:停止字节:0x28

. . .

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. 现在我想要一个方法,该方法可以取出关键字timex或另一个可以读取通道计数的值。 And maybe one that can get the value of Stopbyte. 也许可以得到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
... ...

Use java.util.Properties , it does all dirty work for you: 使用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");

I don't know your requirements, as an alternative, you can load/store your configurations from SharedPreferences instead of app's private storage: 我不知道您的要求,或者,您可以从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");

Hope this helps. 希望这可以帮助。

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;
    }
}

This is how i have it now. 这就是我现在的方式。 This works perfect. 这完美。 It`s for all those having problems on the same issue. 适用于所有在同一问题上有疑问的人。 Pls feel free to use. 请随意使用。

acti is declared in my OwnClass using protected static Activity acti; acti在我宣布OwnClass使用protected static Activity acti; and acti = this; acti = this;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM