简体   繁体   English

Java:读取FileInputStream时出错

[英]Java: Error reading FileInputStream

I have Stemmer class which has the method ExecuteStem as follows. 我有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]);
  }
}
}

I want to call ExecuteStem method from the class CallMethod . 我想从类CallMethod调用ExecuteStem方法。 So CallMethod.java is as follows. 所以CallMethod.java如下。

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

When this code is run, "error reading singing and dancing" gets printed. 运行此代码后,将显示“阅读唱歌和跳舞时出错”。 Why won't it read "FileInputStream"? 为什么不显示“ FileInputStream”?

I replaced InputStream with FileInputStream and changed the code as follows. 我用FileInputStream替换了InputStream ,并如下更改了代码。

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

And I changed the CallMethod class as follows: 然后我将CallMethod类更改如下:

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

This worked! 这工作了!

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

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