Java:读取FileInputStream时出错

时间:2015-10-12 02:01:37

标签: java eclipse fileinputstream

我有Stemmer类,其方法ExecuteStem如下所示。

public class Stemmer implements Stemmer2 {       
public static void ExecuteStem(String[] args) throws IOException{
Stemmer s = new Stemmer();

for (int i = 0; i < args.length; i++) {
  try {
    InputStream in = new FileInputStream(args[i]);
    byte[] buffer = new byte[1024];
    int bufferLen, offset, ch;

    bufferLen = in.read(buffer);
    offset = 0;
    s.reset();

    while(true) {
      if (offset < bufferLen)
        ch = buffer[offset++];
      else {
        bufferLen = in.read(buffer);
        offset = 0;
        if (bufferLen < 0)
          ch = -1;
        else
          ch = buffer[offset++];
      }

      if (Character.isLetter((char) ch)) {
        s.add(Character.toLowerCase((char) ch));
      }
      else {
         s.stem();
         System.out.print(s.toString());
         s.reset();
         if (ch < 0)
           break;
         else {
           System.out.print((char) ch);
         }
       }
    }

    in.close();
  }
  catch (IOException e) {
    System.out.println("error reading " + args[i]);
  }
}
}

我想从课程ExecuteStem中调用CallMethod方法。所以CallMethod.java如下。

public class CallMethod {
public static void main(String[] args) throws IOException {
    String[] t={"Singing and Dancing"};
    Stemmer.ExecuteStem(t);
}

当这段代码运行时,&#34;错误阅读唱歌和跳舞&#34;得到印刷。为什么它不会读取&#34; FileInputStream&#34;?

1 个答案:

答案 0 :(得分:0)

我将InputStream替换为FileInputStream并更改了以下代码。

public static void ExecuteStem() throws IOException{
Stemmer s = new Stemmer();

FileInputStream fin= new FileInputStream("C:\\Users\\dell\\Desktop\\input.txt");
DataInputStream in = new DataInputStream(fin);

我按如下方式更改了CallMethod课程:

public class CallMethod {
public static void main(String[] args) throws IOException {
    Stemmer.ExecuteStem();
}
}

这很有用!