【发布时间】:2015-10-12 02:01:37
【问题描述】:
我有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]);
}
}
}
我想从CallMethod 类中调用ExecuteStem 方法。所以CallMethod.java如下。
public class CallMethod {
public static void main(String[] args) throws IOException {
String[] t={"Singing and Dancing"};
Stemmer.ExecuteStem(t);
}
运行此代码时,会打印“错误读取唱歌和跳舞”。为什么它不会读取“FileInputStream”?
【问题讨论】:
-
你有一个名为《唱歌跳舞》的文件吗?
-
另外,为什么不在 catch 块中添加
e.printStackTrace()以便看到原始错误消息? -
而Singing and Dancing是当前工作目录下的一个文件?
-
不,我将它作为字符串数组传递。实际上,我想通过提供文件的 url 来传递文件中的内容。
-
new FileInputStream()的参数是文件的名称,不是要读取的内容。
标签: java eclipse fileinputstream