1

I'm trying to dump a large integer array (10 000 elements) into a text file but am encountering some problems. I've tried two different approaches and neither seems to be working. Below is the function I've written:

private static void writeToFile(String name, int[] a){ try { FileWriter fw = new FileWriter(name); BufferedWriter bw = new BufferedWriter(fw); bw.write("working"); for (int n: a){ bw.write(n + " "); } bw.close(); } catch (IOException e) { System.err.print("Unable to write to file " + name+ "."); e.printStackTrace(); } } 

The first thing I tried was creating a string in the for loop and then writing the whole string to the file. Neither method works and gives me the same results as follows:

  1. File is created but left blank
  2. Works fine for shorter arrays (~10 elements)
  3. Works fine if the space is a letter eg: bw.write(n + "a")

Any idea what I'm doing wrong? Or is there an even easier way that I'm not seeing?

Thanks,
Civatrix

6
  • What if you use: bw.write(String.valueOf(n) + " ");? Commented Oct 23, 2011 at 0:52
  • Do you close the BufferedWriter and FileWriter objects when the loop is finished? Did you try to call the flush method? Commented Oct 23, 2011 at 0:56
  • 2
    There's nothing wrong. You're writing a file with a single really long line and your editor is not displaying it properly. Commented Oct 23, 2011 at 2:27
  • Seconding @Gabe 's comment. Generated file looks good in vim and notepad, but very odd in Eclipse (It knows that there are 48k columns but doesn't display them) Commented Oct 23, 2011 at 3:36
  • Trying a PrintWriter and print the numbers of separate lines. Commented Oct 23, 2011 at 6:44

2 Answers 2

1

Can you explain how you're viewing/reading in the file afterwards? Your code is basically fine as far as I can see (bar moving the close() to a finally block), and it really should make no difference whatsoever whether a space or other letter is added. But that might make a difference e.g. to a text editor, I suppose...?

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

4 Comments

Opening the generated text file in notepad.exe shows there's content while eclipse text editor shows only some of the last parts of it. I've been debugging this piece of code for half an hour now and it was as simple as this (or atleast it worked for me, but I'm not the one with the real problem).
Remember it will be one HUGELY long line - how that will look will vary depending on what you open it with...
OK, what ultimately do you want to do with that file? Do you particularly care that some particular text editor has a bug that means it doesn't display properly? (If you DO care, then the solution as John Peat has hinted at may be to put a newline after each number.)
It seems to work in everything except the eclipse built in editor, which is probably the only thing that will never open it so I think I'm good. Thanks guys.
0

That code will write all the elements of a[] to the file followed by a space, unless it gets an exception. However it won't write any lines. Is that your problem? If so, you need bw.newLine() after each write().

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.