简体   繁体   English

如何从私有静态类访问方法

[英]how to access a method from private static class

im trying to put all stopwords on a hashset, i dont want to add it one by one so im trying to put in a txt file and have my scanner scan it. 即时通讯试图将所有停用词放在一个哈希集上,我不想一个接一个地添加它,所以即时通讯试图放入一个txt文件并让我的扫描仪对其进行扫描。 the problem is i think my code does not reach my scanner here is my code: 问题是我认为我的代码未到达扫描仪,这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class StopWords  {

    public static final Set<String> stopWords = new HashSet<String>();   

    private static class scan {

        public scan()throws IOException {
            Scanner s = null;

            try{
                s = new Scanner(new BufferedReader(new FileReader("stopwords.txt")));

                while (s.hasNext()) {
                    //System.out.println(s.next());
                    stopWords.add(s.next());
                }
            }finally{
                if (s != null) {
                    s.close();
                }
            }        
        }
    }
}

im running my main on other class and im just calling this class. 我在其他课程上运行我的主语言,而我只是在调用该课程。 thanks in advance 提前致谢

Make a wrapper for it in enclosing class. 在封闭类中为其包装。 Something like: 就像是:

public void doScan() {
    try {
         scan.scan();
    catch (IOException e) {};
}

in StopWords class. 在StopWords类中。

This way you could call doScan() on instance of StopWords. 这样,您可以在StopWords实例上调用doScan()。 You could also make it static. 您也可以使其静态。

And I agree that you should follow naming convections of Java language ( wikipedia.org ). 我同意您应该遵循Java语言的命名对流( wikipedia.org )。

Just want to add a couple tricks you might consider: 只想添加一些您可能会考虑的技巧:

  • First - you could store your stopwords in a properties file, then use java.util.Properties.load to pull the data in. 首先-您可以将停用词存储在属性文件中,然后使用java.util.Properties.load提取数据。
  • Second - you can put your stopwords file on your classpath, and bundle up the stopwords file with the rest of your code in a jar for delivery. 其次-您可以将停用词文件放在类路径中,然后将停用词文件与其余代码捆绑在一个jar中以进行交付。

You wind up with something like this: 您结束时会出现以下内容:

final Properties stopProps = new java.util.Properties();
stopProps.load( new InputStreamReader( this.class.getClassLoader().getResourceAsStream( "mycode/stopWords.properties", "UTF-8" ) );

... ...

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM