• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

trouble writing file

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, i'm using visual j++ (unfortunately). i am simply looking to write some data collected in the program i have written to a .txt file. i have never encountered problems with this usually simple task, however, i have also never encountered visual j++ in all its ignominy. anyway, the following code is placed in a class that extends JPanel.
PrintWriter out;
...some lines later:
try{
out = new PrintWriter(new FileWriter("output.txt"));
}
catch (Exception IOException){
System.out.println("exception");
}
and later...
out.println("test.");
simple enough...it would seem. anyone know of any reasons why output.txt wouldn't show "test." when i open it after closing out the program? it is console based, but driven within classes inheriting JFrame/JPanel. thanks in advance.
n
 
Ranch Hand
Posts: 1179
Mac OS X Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
did you flush the outputstream before closing?
e.g. out.flush();
/Rene
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im only a beginner myself so if this is wrong Please dont shout at me people
This was just working with writing integers to a file mine and not strings but the method i would presume is pretty similar. The code i used to write my list of numbers to an external file was :
public static void saveFile (int[]a,int count)throws IOException

PrintWriter outFile;
outFile = Text.create("Numbers.dat");
// Change Numbers.dat for your file name
for(int i=0; i < count;i++)
{
outFile.println(a[i]);
}
outFile.println(-1);
outFile.close();

}//End Save File
Sorry if this is off the point but i thought it might help , :roll:
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nothing seems to look wrong in your code. Are you sure you are checking output.txt at the right place?
The following code worked for me.
 
fred baby
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
got it. it was a problem with accessing from a key event handler. thanks for the help.
 
reply
    Bookmark Topic Watch Topic
  • New Topic