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

Java FileInputStream,FileOutputStream-检查文件中的特定值/行/字符串,java,android,search,text-files,key-value,Java,Android,Search,Text Files,Key Value,我已经咨询过这个社区好几次了,但似乎我还没有自己解决问题的经验 我最近遇到的问题如下: 我有一个文件,在应用程序(无根设备)的私有存储中创建。我能给它写信,也能从中阅读。但是:我不知道如何在我的文件中添加附加信息(我认为另一个FOS将很容易在第一个文件末尾添加阵列,但我不确定),我不知道如何查找特定的字符串,例如字符串 public static Runnable cfgsetlanecount(int l1, int l2, int l3, int l4, int l5, in

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

我最近遇到的问题如下:

我有一个文件,在应用程序(无根设备)的私有存储中创建。我能给它写信,也能从中阅读。但是:我不知道如何在我的文件中添加附加信息(我认为另一个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我只想要这个值,通过说search for String
lanecount:
得到12:00,通过说search for String
time:

编辑:

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

第1行:lanecount:10001000

第二行:时间:12:00

第3行:时间X:05:00

第4行:anyword:fhfbjklös

第5行:停止字节:0x28

。 .

现在我想要一个方法,可以取出关键字timex的值,或者另一个方法,可以读取lanecount。也许有一个可以得到Stopbyte的值


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

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

# This is a comment:
lanecount=10001000
time=12:00
timex=05:00
anyword=fhfbjklös
Stopbyte=0x28
... ...
使用,它可以为您完成所有脏活:

# 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");
我不知道您的要求,作为替代方案,您可以从应用程序的专用存储加载/存储您的配置:

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

希望这有帮助。

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

# This is a comment:
lanecount=10001000
time=12:00
timex=05:00
anyword=fhfbjklös
Stopbyte=0x28
... ...
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;
    }
}
使用,它可以为您完成所有脏活:

# 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");
我不知道您的要求,作为替代方案,您可以从应用程序的专用存储加载/存储您的配置:

# 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");
希望这有帮助

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
在my
OwnClass
中使用
受保护的静态活动acti声明
acti=this

这就是我现在拥有它的方式。这很好用。这是为所有在同一问题上有问题的人准备的。请随意使用


acti
在my
OwnClass
中使用
受保护的静态活动acti声明
acti=this

如果您在文件中存储字符,我建议您将流包装在InputStreamReader和OutputStreamWriter中。您还应该将流包装在BufferedReader/BufferedWriter或BufferedInputStream/BufferedOutputStream中,这样您就不必自己进行缓冲。对你来说,逻辑学家我会尝试这样做,但这无法解决问题
字节输入[]=新字节[fis.available()]
InputStream.available()
的Javadoc中有一个针对这种用法的特定警告。我刚刚添加了
available
标记,因为它是在我看到的教程中完成的。目前它可以工作,但将来我希望有一个特定数量的字符(在lanecount案例8中),因此我将定义它们,但是thx EJP文件的内容在一段时间后会是什么样子?如果您在文件中存储字符,我建议您将流包装在InputStreamReader和OutputStreamWriter中。您还应该将流包装在BufferedReader/BufferedWriter或BufferedInputStream/BufferedOutputStream中,这样您就不必自己进行缓冲。对你来说,逻辑学家我会尝试这样做,但这无法解决问题
字节输入[]=新字节[fis.available()]
InputStream.available()
的Javadoc中有一个针对这种用法的特定警告。我刚刚添加了
available
标记,因为它是在我看到的教程中完成的。现在它可以工作了,但将来我想有一个特定数量的字符(在lanecount案例8中),所以我会定义它们,但是thx EJP文件的内容在一段时间后会是什么样子?天哪,真的是过时了。。。thx很多,我会试试的。。似乎正是我一直在寻找的,真的是过时了。。。thx很多,我会试试的。。似乎正是我一直在寻找的