我有txt文件(
aaayyy,qqqqwwwww
sswwww,qqswddfed
swssaa,deedddddd
dsssss,deeffcccc
sssddd,deessaaaa
ddddss,qqwwedfvv
qwwwsd,qqddfffff
ddffff,wwedddddd
sddsss,qqqdddddd
)我想在其中查找,计算所有以相同字符开头的单词(所有单词以qq = qqqqwwwww,qqswddfed,qqwwedfvv .... .
开头),并输出保存在new.txt中,qq = 5。
public static void main(String[] args) throws FileNotFoundException, IOException {
String searchWord = "aaayyy";
FileInputStream fis = new FileInputStream(new File("C:/test.txt"));
byte[] content = new byte[fis.available()];
fis.read(content);
fis.close();
String[] lines = new String(content, "UTF-8").split(",");
for (String line : lines) {
String[] words = line.split(",//n");
int j=1;
for (String word : words) {
if (word.equalsIgnoreCase(searchWord)) {
System.out.println("find word = " +j);
}
}
}
}
这里,只在文档中找到一个单词的代码,我知道这段代码并不好。如果您有完全不同的解决方案,请写信。 谢谢你的时间。
答案 0 :(得分:2)
我不知道你的searchWord
变量是什么,但是如果这是你要搜索所有单词的前缀那么就是这样的。
public static void main(String[] args) throws FileNotFoundException, IOException {
String searchWord = "aaayyy";
FileInputStream fis = new FileInputStream(new File("C:/test.txt"));
byte[] content = new byte[fis.available()];
fis.read(content);
fis.close();
String[] lines = new String(content, "UTF-8").split(",");
PrintWriter outputWriter = new PrintWriter("new.txt", "UTF-8"); //create writer for your new.txt
int amountFound = 0; //amount of times the word started with searchWord
for (String line : lines) {
String[] words = line.split(",//n");
for (String word : words) {
if (word.startsWith(searchWord)) { //check if word starts with searchWord
amountFound++;
}
}
writer.println(searchWord + "="+amountFound);
writer.close();
}
}
如果您想检查所有字词而不仅仅是searchWord
,请告诉我,如果我能找到解决方案,我会尝试编辑我的答案。
答案 1 :(得分:2)
根据我的理解,你有一个带有逗号分隔的单词和换行符的.txt文件,你想要计算以给定搜索词开头的单词数量,是吗?
这段代码可以解决问题:
public static void main(String[] args) throws FileNotFoundException, IOException {
String searchWord = "qq";
FileInputStream fis = new FileInputStream(new File("./words.txt"));
byte[] content = new byte[fis.available()];
fis.read(content);
fis.close();
String[] lines = new String(content, "UTF-8").split(",");
ArrayList<String> wordRes = new ArrayList<String>();
for (String line : lines) {
String[] words = line.split("\n");
for (String word : words) {
if(word.startsWith(searchWord)) {
wordRes.add(word);
}
}
}
System.out.println("Total words beginnig with '" + searchWord + "': " + wordRes.size());
}
如果您想查找.txt文件中包含searchWord的所有单词,只需更改此内容:
if(word.startsWith(searchWord))
到
if(word.contains(searchWord))
答案 2 :(得分:2)
尝试我的解决方案
Scanner sc = new Scanner(new FileInputStream("1.txt"));
int n = 0;
while(sc.findWithinHorizon("\\bqq", 0) != null) {
n++;
}
System.out.println(n);
答案 3 :(得分:2)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
/**
*
* @author Halim
*/
public class CountWords {
public static void main(String[] args) throws FileNotFoundException, IOException {
String searchWord = "aa";
FileInputStream fis = new FileInputStream(new File("./words.txt"));
byte[] content = new byte[fis.available()];
fis.read(content);
fis.close();
String[] lines = new String(content, "UTF-8").split(",");
ArrayList<String> wordRes = new ArrayList<String>();
for (String line : lines) {
String[] words = line.split("\n");
for (String word : words) {
if(word.startsWith(searchWord)) {
wordRes.add(word);
}
}
}
System.out.println("Total words beginnig with '" + searchWord + "': " + wordRes.size());
System.out.println("word are "+wordRes);
}
}