如何阅读&;在java中从bufferedreader运行多个变量?

如何阅读&;在java中从bufferedreader运行多个变量?,java,arrays,string,bufferedreader,Java,Arrays,String,Bufferedreader,以前,我在两个不同的类中有这两个变量(a),(total),但我无法获得该类的属性 所以 我试图将这两个代码放在一个类中 但这两个变量都不起作用 System.out.println(“(“+file1.getName()+”)-“+”总字数=“+a+”统计的总重复字数:“%+Total”) 两人都没有工作: 目前为止,我的示例输出: (Blog 39.txt)-统计的重复单词总数:4,单词总数:0 都不是 (Blog 39.txt)-统计的重复单词总数:0,单词总数:82 我需要的输出是: (

以前,我在两个不同的类中有这两个变量(a),(total),但我无法获得该类的属性

所以

我试图将这两个代码放在一个类中 但这两个变量都不起作用
System.out.println(“(“+file1.getName()+”)-“+”总字数=“+a+”统计的总重复字数:“%+Total”)

两人都没有工作:

目前为止,我的示例输出:

(Blog 39.txt)-统计的重复单词总数:4,单词总数:0

都不是

(Blog 39.txt)-统计的重复单词总数:0,单词总数:82

我需要的输出是:

(Blog 39.txt)-统计的重复单词总数:4个,单词总数:82个

当我运行时,如果我更改代码(变量)顺序,“a”或“total”都不起作用(反之亦然)

有人告诉我如何获得变量输出吗?? :) 我是java的初学者

下面是我的代码

package ramki;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class newrepeatedcount {
    public static void main(String[] args) {
        FilenameFilter filter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".txt");
            }
        };
        File folder = new File("E:\\testfolder\\");
        File[] listOfFiles = folder.listFiles(filter);
        for (int i = 0; i < listOfFiles.length; i++) {
            File file1 = listOfFiles[i];

            BufferedReader ins = null;
            try {
                ins = new BufferedReader(
                        new InputStreamReader(new FileInputStream(file1)));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String line = "", str = "";
            String st = null;
            try {
                st = IOUtils.toString(ins);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            // split text to array of words
            String[] words = st.split("\\s");
            // frequency array
            int[] fr = new int[words.length];
            // init frequency array
            for (int i1 = 0; i1 < fr.length; i1++)
                fr[i1] = -1;
            // count words frequency
            for (int i1 = 0; i1 < words.length; i1++) {
                for (int j = 0; j < words.length; j++) {
                    if (words[i1].equals(words[j])) {
                        fr[i1]++;
                    }
                }
            }
            // clean duplicates
            for (int i1 = 0; i1 < words.length; i1++) {
                for (int j = 0; j < words.length; j++) {
                    if (words[i1].equals(words[j])) {
                        if (i1 != j)
                            words[i1] = "";
                    }
                }
            }
            int a = 0;
            try {
                while ((line = ins.readLine()) != null) {
                    str += line + " ";
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            StringTokenizer st1 = new StringTokenizer(str);
            while (st1.hasMoreTokens()) {
                String s = st1.nextToken();
                a++;
            }
            int total = 0;
            for (int i1 = 0; i1 < words.length; i1++) {
                if (words[i1] != "") {
                    // System.out.println(words[i1]+"="+fr[i1]);
                    total += fr[i1];
                }
            }
            System.out.println("( " + file1.getName() + " )-"
                    + "Total repeated words counted:" + total + ","
                    + "total no of words:" + a);
            // System.out.println("total no of words:"+a);
        }
    }
}
package-ramki;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FilenameFilter;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.StringTokenizer;
导入org.apache.commons.io.FileUtils;
导入org.apache.commons.io.IOUtils;
公共类newrepeatedcount{
公共静态void main(字符串[]args){
FilenameFilter筛选器=新建FilenameFilter(){
公共布尔接受(文件目录,字符串名称){
返回名称.endsWith(“.txt”);
}
};
文件夹=新文件(“E:\\testfolder\\”;
File[]listOfFiles=folder.listFiles(过滤器);
for(int i=0;i
如果您将一个流读到底,您将无法再进一步读取。 由于您的代码在许多方面没有得到优化,我可以建议一种快速而肮脏的方法来让您的代码工作。在重新计算“a”的值之前,只需初始化分配给变量“ins”的BufferedReader


如果你把一个流读到它的末尾,你就不能再读下去了。 由于您的代码在许多方面没有得到优化,我可以建议一种快速而肮脏的方法来让您的代码工作。在重新计算“a”的值之前,只需初始化分配给变量“ins”的BufferedReader


每当您进行这样的数据处理时,Java8的流API可能是最佳选择

// get all the files under this folder
Files.walk(Paths.get("E:\\testfolder\\"))
        // keep all the files ending in .txt
        .filter(p -> p.toString().toLowerCase().endsWith(".txt"))
        .forEach(p -> {
            try {
                // process all the lines of the file.
                Map<String, Long> wordCount = Files.lines(p)
                        // break the lines into words
                        .flatMap(l -> Stream.of(l.split("\\s")))
                        // collect the words and count them
                        .collect(Collectors.groupingBy(w -> w, Collectors.counting()));
                // find how many values are more than 1
                long wordDuplicates = wordCount.values().stream().filter(l -> l > 1).count();
                // get the sum of all the values.
                long totalWords = wordCount.values().stream().mapToLong(l -> l).sum();
                System.out.println(p + " has " + wordDuplicates + " duplicates and " + totalWords + " words");
            // catch the IOException at the end because you can't do anything more with the file if this happens.
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
//获取此文件夹下的所有文件
Files.walk(path.get(“E:\\testfolder\\”)
//保留所有以.txt结尾的文件
.filter(p->p.toString().toLowerCase().endsWith(“.txt”))
.forEach(p->{
试一试{
//处理文件的所有行。
Map wordCount=Files.lines(p)
//把台词分成几行
.flatMap(l->Stream.of(l.split(\\s)))
//收集单词并数一数
.collect(Collectors.groupingBy(w->w,Collectors.counting());
//查找有多少个值大于1
long-wordDuplicates=wordCount.values().stream().filter(l->l>1.count();
//获取所有值的总和。
long totalWords=wordCount.values().stream().mapToLong(l->l.sum();
System.out.println(p+“有”+字副本+“副本”和“+字总数+”字);
//
// get all the files under this folder
Files.walk(Paths.get("E:\\testfolder\\"))
        // keep all the files ending in .txt
        .filter(p -> p.toString().toLowerCase().endsWith(".txt"))
        .forEach(p -> {
            try {
                // process all the lines of the file.
                Map<String, Long> wordCount = Files.lines(p)
                        // break the lines into words
                        .flatMap(l -> Stream.of(l.split("\\s")))
                        // collect the words and count them
                        .collect(Collectors.groupingBy(w -> w, Collectors.counting()));
                // find how many values are more than 1
                long wordDuplicates = wordCount.values().stream().filter(l -> l > 1).count();
                // get the sum of all the values.
                long totalWords = wordCount.values().stream().mapToLong(l -> l).sum();
                System.out.println(p + " has " + wordDuplicates + " duplicates and " + totalWords + " words");
            // catch the IOException at the end because you can't do anything more with the file if this happens.
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
 import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

 public class NewRepeatedCount  {

    public static void main(String... arg0)
    {
     BufferedReader br = null;
     Map<String, Integer> counterMap = new HashMap<String, Integer>();

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:\\testing.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                String[] words = sCurrentLine.split("\\s");
                for(String word : words)
                {
                    int count = 1;
                    if(counterMap.get(word) != null)
                    {
                        count = counterMap.get(word);
                        count++;
                        counterMap.put(word, count);
                    }else{
                        counterMap.put(word, count);
                    }

                }
            }
            System.out.println(counterMap.entrySet());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
 }