Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating and Writing a file  RSS feed

 
Sam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
In my Java program, I am creating a file followed by datetime (ie, TowerDetail09-10-2003-03:28:04.doc) and writing into it.
I am getting this error:-
Exception in thread "main" java.io.FileNotFoundException: TowerDetail09-10-2003-03:28:04.doc (The system cannot find the file specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:176)
at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
at TowerDetail.main(TowerDetail.java:30)
***************
The snippet of the code is:
java.util.TimeZone tz = java.util.TimeZone.getTimeZone("EST");
java.util.Date now = new java.util.Date();
DateFormat df = new SimpleDateFormat("MM-dd-yyyy-hh:mm:ss");
df.setTimeZone(tz);
String result = df.format(now);
String fileName = "TowerDetail"+result+".doc";
File inputFile = new File(fileName);
FileOutputStream fos = new FileOutputStream(inputFile);
PrintWriter pw = new PrintWriter(fos);

Help will be appreciated.
thakns...
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paraankusam,
Welcome to JavaRanch!
We don't have many rules here at JavaRanch, but we do have one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as you code goes, try this:
PrintWriter writer = new PrintWriter( new FileWriter( filename ) );
If the file doesn't exist, it will be created. FileOutputStream is expecting a file to already be there.
 
Sam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jason,
Sorry, I couldn't understand. My name is Paraankusam and why do I need to change my display name, please.
Thanks
Paraaankusam
Originally posted by jason adam:
Paraankusam,
Welcome to JavaRanch!
We don't have many rules here at JavaRanch, but we do have one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Sam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paraankusam:
Hello Jason,
Sorry, I couldn't understand. My name is Paraankusam and why do I need to change my display name, please.
Thanks
Paraaankusam

Hello Jason,
I am getting the same error, please.
C:\TowerUsage\JavaFiles>java Detail 'abc'
Exception in thread "main" java.io.FileNotFoundException: Detail09-10-2003-04:34:44.doc (The system cannot find the file specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:176)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at java.io.FileWriter.<init>(FileWriter.java:46)
at TowerDetail.main(TowerDetail.java:30)
thanks...paraankusam
 
Sam
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paraankusam:
Hello Jason,
Sorry, I couldn't understand. My name is Paraankusam and why do I need to change my display name, please.
Thanks
Paraaankusam

Hello Jason,
I am getting the same error, please.
C:\TowerUsage\JavaFiles>java Detail 'abc'
Exception in thread "main" java.io.FileNotFoundException: Detail09-10-2003-04:34:44.doc (The system cannot find the file specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:176)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at java.io.FileWriter.<init>(FileWriter.java:46)
at TowerDetail.main(TowerDetail.java:30)
thanks...paraankusam
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi --
First, regarding the naming policy -- the rule is, you need to use a first and last name. Surely you've got both a given name and a family name, right? Thanks for following our rules!
Second, about what Jason sez about FileOutputStream: I think he was up real early tending them chickens, and ain't thinkin' straight. FileOutputStream will normally create a file that doesn't exist, just like FileWriter.
Third, as to yer problem: I see from your output that you're running on one of them funny Winders OS's. I believe you're having this problem because you're trying to include a colon ":" in a file name. Windows thinks a colon is the end of a drive specifier or somethin', so it normally won't let you put colons into file names. Java's just passing along the error that Winders supplies; it's not a very good message, but I'm purty sure that's what it means.
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, it was in the middle of the day and I had been staring at UML models in Rose for about 4 hours straight. For some reason I was thinking one of the stream classes would give you an error if the file didn't exist... maybe FileInputStream??? That would make more sense...
As always (at least on the forums!), Ernest is correct. The colons throw off Windows. You can use dots (.) instead, that seems to work fine.
 
Kenneth Rowe
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
Your suggestion to use PrintWriter writer = new PrintWriter( new FileWriter( filename ) );
worked perfectly and solved my problem! Many thanks.

Ken
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!