0

I'm not particularly proficient with Java.

I'm converting an array of bytes to a String (each byte with a decimal representation) and then writing to a file. Here is a minimal example that reproduces the problem that I'm having (I've left the file naming stuff in, just in case it's related):

public class PacketWriter {
    public static void writeBytes(byte[] in) {
        Calendar cal = Calendar.getInstance();
        cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("ddmmyyHHmmss");
        PrintStream out = null;
        File outFile = null;
        try {
            outFile = new File("recpacket"+sdf.format(cal.getTime())+".txt");
            outFile.createNewFile();   // also checks for existence of file alre    ady
            out = new PrintStream(new FileOutputStream(outFile,false));
            System.out.println(Arrays.toString(in));
            out.print(Arrays.toString(in));
            out.flush();
            System.out.println("Did the writing!");
        } catch (FileNotFoundException e) {
                System.err.println("Packet output file not found.");
        } catch (IOException e) {
            System.err.println("Could not write packets (I/O error).");
        }
        finally {                   
            System.out.println("Closing...");
            if (out != null) out.close();
            System.out.println("Closed.");
        }
    }
}

When I call PacketWriter.writeBytes(/* some nonempty byte array */) I get the following output:

... array ...
Did the writing!
Closing...
JVM_Close returned -1
Closed.

written to stdout.

The file is empty upon return and does not contain the string that I want it to.

What's going wrong?

Froskoy
  • 2,967
  • 5
  • 20
  • 21
  • 1
    Check you input passed to writeBytes function because i tried it with your code it working fine and data was also written in file – Vijay Aug 01 '13 at 13:33
  • Thanks. The input is fine - it prints the array successfully to stdout. I wonder if it's a file permissions error of some sort? I'm running the JVM as root (for other reasons) so could this be part of the problem? – Froskoy Aug 01 '13 at 13:35

1 Answers1

2

The PrintStream class has very poor error reporting: it looks like writing to the file is failing, but it's impossible to know why; it could be because there's no space left in the file system for example. Try using a FileWriter instead:

    Writer out = null;

        out = new FileWriter(outFile,false);
        System.out.println(Arrays.toString(in));
        out.write(Arrays.toString(in));
        out.flush();
Joni
  • 108,737
  • 14
  • 143
  • 193
  • Thanks, now I actually get a useful error. I get java.io.IOException: Disk quota exceeded, but this is strange since I have plenty of disk space left. However, it is consistent with the fact that this works for smaller files, but not for large files, so I'll investigate further. Thanks! – Froskoy Aug 01 '13 at 13:53
  • You can use the `quota` command to find how much disk space you are allowed to use, and if you're the sysadmin, disable disk quotas with `quotaoff`. The exact details depend on the kind of Unix system you have, though. – Joni Aug 01 '13 at 13:59