流Java数据输出流
我写了一段代码,如果DataOutputStream
但控制台没有显示出我的预期
控制台show No file given
,我希望它在第二个try{}
中打印id和名称
似乎我被困在了文件的输出流中
请帮我找出问题所在
import java.io.*;
public class DOutPut {
public static void main(String args[]) {
DataOutputStream dos;
FileOutputStream fos;
int id = 100;
String name = "Tanaka";
if (args.length <= 0) {
System.out.println("No file given");
System.exit(1);
}
try {
fos = new FileOutputStream(args[0]);
dos = new DataOutputStream(fos);
try {
dos.writeInt(id);
System.out.println("wrote a id: " + id);
dos.writeUTF(name);
System.out.println("wrote a name: " + name);
} catch (IOException e) {
// TODO: handle exception
System.err.println("IO error");
System.exit(1);
} finally {
fos.close();
dos.close();
}
} catch (IOException e) {
// TODO: handle exception
System.err.println("Opening/Closing error");
System.exit(1);
}
}
}
我在这里找到了解决办法。感谢所有回答我问题的人
我应该用参数来运行它。因为代码fos = new FileOutputStream(args[0])
需要一个参数
我通过Eclipse编写Java代码,并且可以使用运行配置
# 1 楼答案
您没有向Java程序提供参数