I wrote a piece of code if DataOutputStream.
But the Console didn't show what I expected.
The Console show No file given
, and I wish it prints id and name writing in the second try{}
.
It seems like I trapped in File's output stream.
Please help me find out what's wrong...
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);
}
}
}
Here I found the solution. Thanks to everybody who answers my question.
I should run it with arguments. Because the code fos = new FileOutputStream(args[0])
requires a argument.
I write Java codes by Eclipse, and I can use Run - Run Configurations.