FileInputStream,FileOutputStream - 检查文件的具体值/线/字符串
我咨询过这个社区已经几次了,但似乎我仍然没有经验来解决我自己的问题。FileInputStream,FileOutputStream - 检查文件的具体值/线/字符串
我最新的问题如下:
我有一个文件,在应用程序(无根设备)的私有存储设备创建的。我可以写信给它,并从中读取。但是:我不知道,我如何在我的文件中添加额外的信息(我认为另一个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:00 time:
编辑:这应该添加
首先行到我的文件。这是它应该是什么样子:
线路1:lanecount:10001000
线路2:时间:12:00
3号线:天美:05:00
4号线:anyword:fhfbjklös
line5:Stopbyte:0x28
。 。 。
现在我想要一个方法,它可以取出关键字timex的值,或者另一个可以读取lanecount的值。也许可以得到Stopbyte的价值。
然后我需要一个方法,它能够编辑关键字时间的值。
根据您所描述的文件内容,这听起来像一个天然的属性文件:
# 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");
希望这会有所帮助。
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;
如果你在文件中存储字符的,我建议你换你流中的InputStreamReader和OutputStreamWriter。你也应该把你的流包装在BufferedReader/BufferedWriter或者BufferedInputStream/BufferedOutputStream中,这样你就不必自己做缓冲了。 充分利用[java.io包(包含所有这些类)](http://developer.android.com/reference/java/io/package-summary.html)。 – 2013-03-20 08:49:56
thx给你我的逻辑我会试试,但是这不会解决问题 – Ekonion 2013-03-20 08:52:22
'byte input [] = new byte [fis.available()];'在Javadoc中针对'InputStream.available ()'。 – EJP 2013-03-20 08:53:33