1
我正在android中创建一个基于模式锁定的项目。 我有一个名为category.txt
文件的文件的内容如下在java前面追加一个字符串?
Sports:Race:Arcade:
没有我想要的是,每当用户绘制的图案为一个特定的游戏类的模式应该在前面得到追加该类别。
例如: Sports:Race:"string/pattern string to be appended here for race"Arcade:
我已经使用下面的代码,但它不工作。
private void writefile(String getpattern,String category)
{
String str1;
try {
file = new RandomAccessFile(filewrite, "rw");
while((str1 = file.readLine()) != null)
{
String line[] = str1.split(":");
if(line[0].toLowerCase().equals(category.toLowerCase()))
{
String colon=":";
file.write(category.getBytes());
file.write(colon.getBytes());
file.write(getpattern.getBytes());
file.close();
Toast.makeText(getActivity(),"In Writefile",Toast.LENGTH_LONG).show();
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException io)
{
io.printStackTrace();
}
}
请大家帮忙!
究竟什么是行不通的?我试了一下,发现问题是虽然文件关闭(s。'file.close()'调用),循环继续。这会导致IOException。 – Ria 2015-03-25 09:40:38
我不知道为什么它不适合我。 你能否给我提供一些可以读取文件的附加字符串,例如在Race的前面加上“any string”:“要附加的字符串”。 – 2015-03-26 04:14:48
为了确保我正确理解了这一点:你的文件只包含一行,如'Sports:Race:Arcade:'。如果给定的类别与字符串中的元素相匹配(比如'Race'),您想要将提供的模式追加到类别前并将其写回到同一个文件中? – Ria 2015-03-26 07:00:51