写字符串时通过线
我用下面的代码来写串到我的简单的文本文件,文本文件中的行避免重复文本文件,但是,很奇怪,某些字符串被覆盖,如:写字符串时通过线
apple orange
grapes
grapes
grapes
apple
kiwi
我的问题是:
- 如何停止多次写入字符串?
- 如何在文件中已经存在字符串(一行)的情况下停止写入文件?
我咨询了this post,但未能将其应用于我的案例。你可以请一点帮助吗?提前致谢。
回答:
这是一个关于如何解决你的问题,一个完整的例子:
import java.io.BufferedReader; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
public class HisSaver {
private HashSet<String> uniqueTester = new HashSet<String>();
private String fileLocation="/mnt/sdcard/out.txt";
private static HisSaver instance = null;
private HisSaver() {
readWordsFromFile();
}
public static HisSaver getInstance() {
if(instance == null)
instance = new HisSaver();
return instance;
}
public void saveWord(String word) {
if(!uniqueTester.contains(word)) {
uniqueTester.add(word);
writeWordToFile(word);
}
}
private void writeWordToFile(String word) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
fileLocation), true));
bw.write(word);
bw.newLine();
bw.close();
} catch (IOException e) {
}
}
private void readWordsFromFile() {
try {
BufferedReader br = new BufferedReader(new FileReader(new File(
fileLocation)));
String line;
while((line = br.readLine()) != null) {
if(!uniqueTester.contains(line)) {
uniqueTester.add(line);
}
}
} catch (IOException e) {
}
}
}
我们利用这一点,你只需做你的代码如下:
HisSaver hs = HisSaver.getInstance(); hs.saveWord("newWord");
这将插入假如没有其他函数在你的代码中访问这个文件,那么“newWord”当且仅当它不在你的文件中。请注意:此解决方案不是线程安全的!
编辑:代码做什么的说明: 我们创建一个单独的类HisSaver。这是通过将构造函数设置为私有的并提供一个返回初始化HisSaver的静态方法getInstance()来实现的。这个HisSaver将已经包含您文件中的所有预先存在的单词,因此只会添加新单词。从另一个类调用getInstance()方法将为您提供一个单例的句柄,并允许您调用saveWord,而无需担心手中是否拥有正确的对象,因为它只能有一个实例被实例化。
回答:
我猜mWordHis
是List
,它可以包含重复的条目。 您可以先将其转换为Set
(不允许重复)并仅打印Set
中的文字。
Set<String> wordSet= new HashSet<>(mWordHis); for (String item : wordSet)
{
sbHis.append(item);
sbHis.append("\n");
}
如@fge评论,LinkedHashSet
也可以使用,如果插入顺序很重要。
如果您需要使用同一个文件多次运行相同的代码,您必须将所有已写入文件的记录保存在内存中,或者在写入文件之前先读取文件并获取所有数据。
编辑:
我只能想想修剪的话因为有些人可能包含不必要的空格:
Set<String> wordSet= new HashSet<>(); for (String item : mWordHis){
wordSet.add(item.trim());
}
回答:
试试这个:
public void saveHisToFile(Set<String> existingWords) {
if (prefs.getBoolean("saveHis", true) && mWordHis != null && mWordHis.size() >= 1)
{
StringBuilder sbHis = new StringBuilder();
for (String item : mWordHis)
{
if (!existingWords.contains(item)) {
sbHis.append(item);
sbHis.append("\n");
}
}
String strHis = sbHis.substring(0, sbHis.length()-1);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
fileLocation), true));
bw.write(strHis);
bw.newLine();
bw.close();
} catch (IOException e) {
}
}
}
回答:
您可以将所有的字符串添加到HashMap并检查每个新的String是否已经在那里。
实施例:
HashMap<String,String> test = new HashMap<String,String>(); if(!test.containsKey(item)) {
test.put(item,"");
// your processing: example
System.out.println(item);
} else {
// Your processing of duplicates, example:
System.out.println("Found duplicate of: " + item);
}
编辑:或使用一个HashSet如由其他解决方案...
HashSet<String> test = new HashSet<String>(); if(!test.contains(item)) {
test.add(item);
// your processing: example
System.out.println(item);
} else {
// Your processing of duplicates, example:
System.out.println("Found duplicate of: " + item);
}
EDIT2:
private String fileLocation="/mnt/sdcard/out.txt"; public void saveHisToFile()
{
if (prefs.getBoolean("saveHis", true) && mWordHis != null && mWordHis.size() >= 1)
{
StringBuilder sbHis = new StringBuilder();
HashSet<String> test = new HashSet<String>();
Set<String> wordSet= new HashSet<String>(mWordHis);
for (String item : wordSet)
{
if(!test.contains(item)) {
test.add(item);
// your processing: example
sbHis.append(item+System.lineSeparator());
} else {
// Your processing of duplicates, example:
//System.out.println("Found duplicate of: " + item);
}
}
String strHis = sbHis.toString();
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(new File(
fileLocation), true));
bw.write(strHis);
bw.newLine();
bw.close();
} catch (IOException e) {
}
}
}
以上是 写字符串时通过线 的全部内容, 来源链接: utcz.com/qa/259111.html