import java.io.*;
public class Tt {
public static void main(String args[]){
try{
DataInputStream file = new DataInputStream(args[0]);
String read_file_char = file.readUTF();
System.out.print(read_file_char);
read_file_char = file.readUTF();
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("需要提供一个文件名作为命令行参数!");
}catch(FileNotFoundException e){
System.out.println("file not find !");
}catch(IOException e){}
}
}
为什么会出错?!
java 中的 dataInputStream 流问题 !? 求高手解答!
答案:2 悬赏:0 手机版
解决时间 2021-02-01 00:20
- 提问者网友:wodetian
- 2021-01-31 05:33
最佳答案
- 五星知识达人网友:归鹤鸣
- 2021-01-31 06:03
你好,很小的错误。
把DataInputStream file = new DataInputStream(args[0]);
换成DataInputStream file = new DataInputStream(new FileInputStream(args[0]));
new DataInputStream()括号里的参数必须是个InputStream。
把DataInputStream file = new DataInputStream(args[0]);
换成DataInputStream file = new DataInputStream(new FileInputStream(args[0]));
new DataInputStream()括号里的参数必须是个InputStream。
全部回答
- 1楼网友:何以畏孤独
- 2021-01-31 06:58
dis.close();
s.close();
这样就可以了, dis.close()会主动去调用:inputstream.close();
也就说只要关闭最上层的流就可以了,其内部流会自动关掉。
看看源码就明白了。
但是如果
datainputstream dis=new datainputstream(inputstream);
有异常的话,就有问题。 所以最好是在:
try{
}catch{
dis.close(); //必须先关
inputstream.close();
s.close();
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯