在java中读取配置文件时出错

时间:2015-07-09 06:44:50

标签: java file configuration

我正在阅读java中的文本文件,但是我收到以下错误消息

“读取配置文件时出错。确保指定了正确的路径。”

我提供了正确的文件路径,但不知道错误来自哪里。

包wordsensefinder;

import in.ac.iitb.cfilt.jhwnl.JHWNL;
import in.ac.iitb.cfilt.jhwnl.JHWNLException;
import in.ac.iitb.cfilt.jhwnl.data.IndexWord;
import in.ac.iitb.cfilt.jhwnl.data.IndexWordSet;
import in.ac.iitb.cfilt.jhwnl.data.Pointer;
import in.ac.iitb.cfilt.jhwnl.data.PointerType;
import in.ac.iitb.cfilt.jhwnl.data.Synset;
import in.ac.iitb.cfilt.jhwnl.data.POS;
import in.ac.iitb.cfilt.jhwnl.dictionary.Dictionary;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;


public class Examples {

static void demonstration() {

    BufferedReader inputWordsFile = null;
    try {
        inputWordsFile = new BufferedReader(new InputStreamReader (new FileInputStream ("inputwords.txt"), "UTF8"));
    } catch( FileNotFoundException e){
        System.err.println("Error opening input words file.");
        System.exit(-1);
    } catch (UnsupportedEncodingException e) {
        System.err.println("UTF-8 encoding is not supported.");
        System.exit(-1);
    }
    JHWNL.initialize();

    String inputLine;
    long[] synsetOffsets;

    try {
        while((inputLine = inputWordsFile.readLine()) != null){
            System.out.println("\n" + inputLine);
            //   Look up the word for all POS tags
            IndexWordSet demoIWSet = Dictionary.getInstance().lookupAllIndexWords(inputLine.trim());                
            //   Note: Use lookupAllMorphedIndexWords() to look up morphed form of the input word for all POS tags              
            IndexWord[] demoIndexWord = new IndexWord[demoIWSet.size()];
            demoIndexWord  = demoIWSet.getIndexWordArray();
            for ( int i = 0;i < demoIndexWord.length;i++ ) {
                int size = demoIndexWord[i].getSenseCount();
                System.out.println("Sense Count is " + size);   
                synsetOffsets = demoIndexWord[i].getSynsetOffsets();
                for ( int k = 0 ;k < size; k++ ) {
                    System.out.println("Offsets[" + k +"] " + synsetOffsets[k]);    
                }

                Synset[] synsetArray = demoIndexWord[i].getSenses(); 
                for ( int k = 0;k < size;k++ ) {
                    System.out.println("Synset [" + k +"] "+ synsetArray[k]);
                    System.out.println("Synset POS: " + synsetArray[k].getPOS());
                    Pointer[] pointers = synsetArray[k].getPointers();
                    System.out.println("Synset Num Pointers:" + pointers.length);
                    for (int j = 0; j < pointers.length; j++) {                         
                        if(pointers[j].getType().equals(PointerType.ONTO_NODES)) {  // For ontology relation
                            System.out.println(pointers[j].getType() + " : "  + Dictionary.getInstance().getOntoSynset(pointers[j].getOntoPointer()).getOntoNodes());
                        } else {
                            System.out.println(pointers[j].getType() + " : "  + pointers[j].getTargetSynset());
                        }                           
                    }

                }
            }

        }
    } catch (IOException e) {
        System.err.println("Error in input/output.");           
        e.printStackTrace();
    } catch (JHWNLException e) {
        System.err.println("Internal Error raised from API.");
        e.printStackTrace();
    } 
}

public static void main(String args[]) throws Exception {
    demonstration();
    }
}

1 个答案:

答案 0 :(得分:0)

如果文件与java代码在同一个包中,那么您需要做的就是以这种方式读取文件

的getClass()。的getResourceAsStream( “inputwords.txt”)