4

I decided to do a test for how many different characters I could write. I tried this code:

for (int i = 0;i < 255;i++) {
    myBufferedWriter.write(i);
}

But in the file, using my hex editor, I saw it counted normally at first 01, 02, 03..., but then it got to 3F, and wrote that around 20 times, and then continued writing normally. Can I not write certain characters? I want to be able to write all characters 0-255.

Full code:

public class Main {
    public static void main(String[] args) {
        try{
            BufferedWriter bw = new BufferedWriter(new FileWriter(new File("abc.txt")));
            for (int i = 0;i < 255;i++) {
                bw.write(i);
            }
            bw.close();
          }catch(Exception e){}
    }
}
5
  • @jlordo It does that in any editor. I tried two others. If it was showing the wrong things, then it wouldn't be an hex editor. Commented Jul 4, 2013 at 23:33
  • oh-oh. I didn't look close enough. I have the same output as you. I'll do some research and post an answer. Commented Jul 4, 2013 at 23:35
  • @jlordo Yup. A ton of question marks. :) Commented Jul 4, 2013 at 23:36
  • No need to put the answer into the question ... especially since it is not necessarily the best answer. I edited it out. Commented Jul 5, 2013 at 1:35
  • Ok.. you didnt have to but thanks. Commented Jul 5, 2013 at 2:31

2 Answers 2

4

I think your problem is that you are using the wrong API. What is happening with your current code is that your "characters" are being treated as text and encoded using the default character encoding for your platform.

If you want to write data in binary form (e.g. the bytes zero through 255) then you should be using the OutputStream APIs. Specifically BufferedOutputStream.

For example:

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(new File("abc.dat")));
        for (int i = 0; i < 255; i++) {
            bos.write(i);
        }
        bos.close();
    }
}

If you want to be able to write other data types too (in binary form), then DataOutputStream is more appropriate ... because it has a bunch of other methods for writing primitive types and so on. However, if you want to write efficiently you need a BufferedOutputStream in between the FileOutputStream and the DataOutputStream.

Sign up to request clarification or add additional context in comments.

2 Comments

How do I write to a file using an output stream?
@SusanYanders OutputStream.write(). +1 for this answer.
0

I figured it out. My new code:

public class Main {
    public static void main(String[] args) {
        try{
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(new File("abc.txt")));
            for (int i = 0;i < 255;i++)
                dos.writeByte(i);
            dos.close();
        }catch(Exception e){}
    }
}

3 Comments

There are two problems with this code: 1) You aren't doing any buffering so each byte written will do a system call. 2) You are squashing all exceptions - BAD IDEA.
@StephenC I know about that. right now it isnt important. And in c++ you can hardly use exceptions anyways
It is only unimportant if you don't have any bugs that might throw exceptions. If there are bugs, then your code will silently erase any evidence. I don't think this should ever be dismissed as unimportant ... unless you are one of those rare people who actually enjoys tracking down obscure bugs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.