Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file output garbeled!

Status
Not open for further replies.

bobo12

Programmer
Dec 26, 2004
74
US
hi, i am quite puzzled, the following trivial code prints 0 to file. any suggestions!

try {
BufferedWriter out = new BufferedWriter(new FileWriter(outputfile));
out.write("ITRS SYSTEM");
out.newLine();
out.write(" (ITRS) ");
out.newLine();
out.write(" TEST SUBSYSTEM ");
out.newLine();
out.flush();
out.close();
} catch (IOException e) {
System.out.println("excpetion caught" + e);
System.exit(1);
}
 
The code works fine, assuming the File object "outputfile" is valid. I used:

File outputfile = new File("testfile.txt");

I suspect something about your File object is incorrect.
 
ok a few more clues...
when using DataOutputStream out, out.write(...) throws an EOFexception. why is that?
i thought i simply writes to file and moves the EOF accordingly. this is very strange, a write func should move the EOF marker right?

also, after a few out.write's, when i do out.size(), nothing is printed, why is that?
any suggestions?

do u think looking at output file with hex editor helps as far as wierd chars!

why/how are those wierd chars generated?
 
Can you show how the File object is being created? Does it already exist and perhaps read-only? Or maybe on a drive with no room left?

I recommend making a test program like I did and getting it working, so you can at least compare your code to it.

Code:
import java.io.*;
public class TestFileWrite
{

   public static void main(String[] args)
   {
      try {
         File outputfile = new File("testfile.txt");
         BufferedWriter out = new BufferedWriter(new FileWriter(outputfile));
              out.write("ITRS SYSTEM");
              out.newLine();
              out.write("    (ITRS)         ");
              out.newLine();
              out.write("    TEST SUBSYSTEM             ");
              out.newLine();
              out.flush();
              out.close();
          } catch (IOException e) {
              System.out.println("excpetion caught" + e);
              System.exit(1);
          }
   }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top