Results 1 to 11 of 11
- 08-13-2013, 08:30 PM #1
Member
- Join Date
- Aug 2013
- Posts
- 3
- Rep Power
- 0
"Writing to a text file" not compiling
It is showing 'file not found exception' in the line in which the 'output' object is declared.The file 'scores.txt' is in my current working directory also.I cant get what's the error there.Please help.
package file8;
import java.io.*;
public class File8 {
public static void main(String[] args) {
File file = new File("scores.txt");
if (file.exists()) {
System.out.println("File already exists");
System.exit(1);
}
PrintWriter output = new PrintWriter(file);
output.print("John T Smith ");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
output.close();
}
}
- 08-13-2013, 08:33 PM #2
Re: "Writing to a text file" not compiling
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 08-13-2013, 08:40 PM #3
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: "Writing to a text file" not compiling
Before you try to open the file for writing, try doing a System.out.println() of both the file.getAbsolutePath() and file.canWrite(). What do those two output?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-13-2013, 08:57 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: "Writing to a text file" not compiling
I bet your compiler protested in the strongliest way against not handling IOExceptions anywhere ...
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
- 08-14-2013, 10:54 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: "Writing to a text file" not compiling
"If the given file object does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file."
From the API.
So there's a few possibilities there and, since you have an exists() check which exits if the file exists, the relevant ones are in italics.
But, as Jos implies, it would be nice to see the actual code.Please do not ask for code as refusal often offends.
** This space for rent **
- 08-14-2013, 11:00 AM #6
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: "Writing to a text file" not compiling
Well I guess that is the code; the title of the post already indicates "not compiling". I'm guessing that the reference to the file not found exception is not during runtime, but actually during compile time.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-14-2013, 11:33 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 27
Re: "Writing to a text file" not compiling
Ahhhh!
In other words it'd complaining that the OP is not handling the FNF exception!
In which case they need to read the Exceptions Tutorial.Please do not ask for code as refusal often offends.
** This space for rent **
- 08-14-2013, 11:53 AM #8
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: "Writing to a text file" not compiling
... and provide the exact error gotten in stead of "something along these lines" :/ If the OP would have specified "unhandled exception type FileNotFoundException" then there would have been no confusion.
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-17-2013, 03:57 PM #9
Member
- Join Date
- Aug 2013
- Posts
- 3
- Rep Power
- 0
Re: "Writing to a text file" not compiling
Thanks a lot.I removed the if block & it still showing "FileNotFoundException".but that file lies in my current working directory.
- 08-17-2013, 03:57 PM #10
Member
- Join Date
- Aug 2013
- Posts
- 3
- Rep Power
- 0
Re: "Writing to a text file" not compiling
Ya.It's compile time
- 08-17-2013, 04:43 PM #11
Similar Threads
-
access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 09:29 AM -
Convert string operation symbols "+", "-", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 04:06 PM -
Anyone know anything about the "payload" in a PDF file? hidden text?
By Arrowx7 in forum New To JavaReplies: 0Last Post: 06-14-2011, 07:52 AM -
Compiling/Running Project in Command Line: "Could not find main class" Error
By Yasemin Gokce in forum New To JavaReplies: 1Last Post: 06-30-2009, 03:32 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM
Bookmarks