Java 查找、统计文档中的单词(按第一个字母)

Java 查找、统计文档中的单词(按第一个字母),java,Java,我有txt文件( )我想在其中查找,计算以相同字符开头的所有单词(所有单词以qq=qqwwwww,qqswddfed,qqwwedfvv….)并以qq=5的形式输出并保存在new.txt中 public static void main(String[] args) throws FileNotFoundException, IOException { String searchWord = "aaayyy"; FileInputStream fis = new FileInpu

我有txt文件(

)我想在其中查找,计算以相同字符开头的所有单词(所有单词以
qq=qqwwwww,qqswddfed,qqwwedfvv….
)并以qq=5的形式输出并保存在new.txt中

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

            }

        }
    }
}
在这里,代码里面只找到一个单词在文档中,我知道这个代码不好。如果您有完全不同的解决方案,请写信。
谢谢您的时间。

我不知道您的
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();
    }
}

如果您想检查所有单词,而不仅仅是
搜索词
,请告诉我,如果我能找到解决方案,我将尝试编辑我的答案。

据我所知,您有一个.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());
}

试试我的解决办法

    Scanner sc = new Scanner(new FileInputStream("1.txt"));
    int n = 0;
    while(sc.findWithinHorizon("\\bqq", 0) != null) {
        n++;
    }
    System.out.println(n);
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.IOException;
导入java.util.ArrayList;
/**
*
*@作者哈利姆
*/
公共类CountWords{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException{
字符串searchWord=“aa”;
FileInputStream fis=新的FileInputStream(新文件(“./words.txt”);
字节[]内容=新字节[fis.available()];
阅读(内容);
fis.close();
字符串[]行=新字符串(内容为“UTF-8”)。拆分(“,”);
ArrayList wordRes=新的ArrayList();
用于(字符串行:行){
String[]words=line.split(“\n”);
for(字符串字:字){
if(word.startsWith(searchWord)){
添加(word);
}
}
}
System.out.println(“以“+searchWord+”开头的单词总数:“+wordRes.size()”);
System.out.println(“单词是”+wordRes);
}   
}

这将写入
aaayy=2
例如到
new.txt
注意:它每次都会覆盖文件您所说的
第一个字母的确切含义是什么?
qq
是第一个字母吗?你用什么编码?不清楚你在问什么。你的问题是什么?它只找到一个词?您正在查找“Aayyy”,而这只是文件中的一次。78k信誉用户难道不知道“尝试我的解决方案”不是正确的答案描述吗?请加上适当的解释。
            if(word.startsWith(searchWord))
            if(word.contains(searchWord))
    Scanner sc = new Scanner(new FileInputStream("1.txt"));
    int n = 0;
    while(sc.findWithinHorizon("\\bqq", 0) != null) {
        n++;
    }
    System.out.println(n);
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);
}   
}