This discussion is archived
12 Replies Latest reply: May 2, 2008 8:18 AM by 807601 RSS

Writing integer to file error

800637 Newbie
Currently Being Moderated
Hey

Im trying to write an integer to a file, but it writes an whole ohter number than variable contains. I know this because i do i System.out.println on the variable and it prints the correct number. Why does the write function write anohter number? For example the print shows 50 but the write functions write " 2" to file.


Here i call the write:
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        String file = "C:\\Users\\Jesper\\Documents\\NetBeansProjects\\Katrinelund_v0.16\\secure\\checkNewOrder.txt";
        control.deleteFile(file);
        control.createFile(file);
        int currentOrder = control.getCurrentOrderNumber();
        System.out.println(currentOrder);
        control.setWriteToFile(file, currentOrder);
        quit();        
    } 
Here is the write function:
public void writeToFile(String fileDestination, int WhatToWrite){        
        FileOutputStream fos; 
        DataOutputStream dos;

        try {
            File file= new File(fileDestination);
            fos = new FileOutputStream(file);
            dos = new DataOutputStream(fos);
            dos.writeInt(WhatToWrite);
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
  • 1. Re: Writing integer to file error
    807601 Newbie
    Currently Being Moderated
    DataOutputStream.writeInt() doesn't write the int as text. It writes it as a datastream.

    From the API
    Writes an int to the underlying output stream as four bytes, high byte first.
    Notepad in Windows uses one byte per character, so you'd see 4 characters if you wrote an int. In a unicode environment (I think Wordpad in Windows uses unicode), you have 2 bytes per character, so you'd have 2 characters after writing with dataoutputstream.

    So in short...don't use a dataoutputstream if your goal is plaintext, which yours clearly is.
  • 2. Re: Writing integer to file error
    3004 Newbie
    Currently Being Moderated
    System.out.println(int) takes the int and converts it to a string in the default character encoding ant writes that out. Most likely your encoding will overlap with ASCII, so if the int is 2, you'll get the decimal value 50, which is the hex value 0x32, which is the ASCII (and UTF-8, ISO-8859, etc) code for the character '2'.

    When you use DataOuptutStream.writeInt, however, it does not write text. It simply writes the bytes that make up that int, i.e., 0x00 00 00 02.

    If you want a Java int, use DataOutputStream. If you want human readable text, use a print or println method in an PrintStream or PrintWriter.
  • 3. Re: Writing integer to file error
    3004 Newbie
    Currently Being Moderated
    File file= new File(fileDestination);
    PrintWriter pw = new PrintWriter(file);
    pw.println(whatToWrite);
  • 4. Re: Writing integer to file error
    800637 Newbie
    Currently Being Moderated
    thanks you for the answers and sorry for the late reply.

    I tried to do this, but now it aint writing/printing anything to the file:
    public void writeToFile(String fileDestination, int WhatToWrite){
            try {
                File file = new File(fileDestination);
                PrintWriter pw = new PrintWriter(file);
                pw.print(WhatToWrite);
            } catch (IOException e) {
                e.printStackTrace();
            }       
        }
    am i doing anything wrong?
  • 5. Re: Writing integer to file error
    807601 Newbie
    Currently Being Moderated
    Add pw.flush() to your code

    Edited by: Illu on May 2, 2008 12:40 PM

    Edited by: Illu on May 2, 2008 12:43 PM
  • 6. Re: Writing integer to file error
    807601 Newbie
    Currently Being Moderated
    PrintWriter is set up by default to not flush it's buffer unless specifically told to, or when it is closed. This is for performance reasons, as performing a single write to the file with all the text is faster than performing lots of little writes.

    So your options are:

    1) When all of your prints are complete, call close() on the PrintWriter
    2) Call flush() on the PrintWriter after every print
    3) Use the constructor for PrintWriter that sets autoflush to true, which simulates #2
    PrintWriter(Writer out, boolean autoFlush)
    PrintWriter pw = new PrintWriter(new BufferedWriter(file), true);
    Edited by: newark on May 2, 2008 8:44 AM
  • 7. Re: Writing integer to file error
    807601 Newbie
    Currently Being Moderated
    Damn, you beat me to it, Newark! :)

    Illu
  • 8. Re: Writing integer to file error
    807601 Newbie
    Currently Being Moderated
    Can't edit my post anymore, but there's a mistake in it.
    PrintWriter pw = new PrintWriter(new BufferedWriter(file), true);
    won't work, as BufferedWriter doesn't have a constructor that takes a File.

    Try
    PrintWriter pw = new PrintWriter(new PrintWriter(file), true);
  • 9. Re: Writing integer to file error
    3004 Newbie
    Currently Being Moderated
    newark wrote:
    Try
    PrintWriter pw = new PrintWriter(new PrintWriter(file), true);
    You wanna rethink that maybe? :-)
  • 10. Re: Writing integer to file error
    807601 Newbie
    Currently Being Moderated
    jverd wrote:
    newark wrote:
    Try
    PrintWriter pw = new PrintWriter(new PrintWriter(file), true);
    You wanna rethink that maybe? :-)
    Probably. First I just put the file in, then realized that the autoflush constructor needs a writer, not a file. So I put a BufferedWriter in, but then realized BufferedWriter doesn't take a file either. Then I was all confused, and meant to use a FileWriter but put PrintWriter instead.

    So how about
    PrintWriter pw = new PrintWriter(new FileWriter(file), true);
    In short, I hate nested readers/writers and think that if you ever have to use them, there's a flaw in the IO system.
  • 11. Re: Writing integer to file error
    3004 Newbie
    Currently Being Moderated
    newark wrote:
    In short, I hate nested readers/writers and think that if you ever have to use them, there's a flaw in the IO system.
    I disagree. It's just the decorator pattern. Different classes for different purposes.
  • 12. Re: Writing integer to file error
    807601 Newbie
    Currently Being Moderated
    jverd wrote:
    Different classes for different purposes.
    Yes, that's exactly what I'd like. A different class for each purpose. Want to write plaintext? One class for that. Data stream? One class for that. None of this file writer inside buffered writer inside printstream inside xxx inside yyy crap.

    Edit: seriously? Charlie Romeo Alpha Papa is on the language filter?