-4

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.

Xinyi Zhang
  • 11
  • 1
  • 4
  • 2
    Well what did the console show and what did you expect? – user253751 Oct 14 '16 at 04:23
  • 1
    You say the console shows "no file given". Surely you must know what that means, because *you're* the one who made the program print that! – user253751 Oct 14 '16 at 04:36
  • @immibis I still didn' t get it...I wrote if args' length is short than 0, it means there is no file, so print `no file given` .But my expect is that it shows `wrote a id: 100, wrote a name: Tanaka`... – Xinyi Zhang Oct 14 '16 at 05:18
  • Then args' length is shorter than 0. Why is args' length shorter than 0? Did you expect args' length to be shorter than 0? – user253751 Oct 14 '16 at 05:22
  • @immibis Thank you. I just started to learn java...how can I correct it? to add args? I just want it to show `wrote a id: 100, wrote a name: Tanaka` – Xinyi Zhang Oct 14 '16 at 05:31
  • args contains the command-line arguments to your program. If you want it to not be empty, then run it with some command-line arguments. – user253751 Oct 14 '16 at 05:32
  • thank you, i ran it with args. it helped – Xinyi Zhang Oct 17 '16 at 04:45

1 Answers1

0

You did not provide arguments to your Java program.

java -?
Usage: java [-options] class [args...]
           (to execute a class)
   or  java [-options] -jar jarfile [args...]
           (to execute a jar file)
e.g.
java DOutPut C:\MyFolder\MyFile.txt
KC Wong
  • 2,410
  • 1
  • 18
  • 26