Results 1 to 20 of 35
Thread: Files. What is wrong with this??
- 11-16-2011, 08:40 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Files. What is wrong with this??
Here is my code:
It asks me to enter the names of file 1 and 2, then when I hit enter, I get these errors:Java Code:import java.util.Scanner; import java.io.*; public class UpperCaseFile { String file1; String file2; public UpperCaseFile(String firstfile, String secondfile)throws IOException { File readfile= new File(firstfile); Scanner inputFile= new Scanner(readfile); PrintWriter outputFile= new PrintWriter(secondfile); while(inputFile.hasNext()) { String line= inputFile.nextLine(); outputFile.println(line.toUpperCase()); } inputFile.close(); outputFile.close(); } } public class FilesTest { public static void main(String[] args)throws IOException { String firstfile; String secondfile; Scanner keyboard= new Scanner(System.in); System.out.println("Enter the name of file 1"); firstfile= keyboard.nextLine(); System.out.println("Enter the name of file 2"); secondfile= keyboard.nextLine(); UpperCaseFile object= new UpperCaseFile(firstfile, secondfile); System.exit(0); } }
Exception in thread "main" java.io.FileNotFoundException: UpperCaseFile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
at java.util.Scanner.<init>(Scanner.java:656)
at UpperCaseFile.<init>(UpperCaseFile.java:10)
at FilesTest.main(FilesTest.java:17)
I'm not really sure what's going wrong here.? Any help is appreciated. Thanks in advance.
- 11-16-2011, 09:25 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Files. What is wrong with this??
That exception is fairly explanatory - the file you entered cannot be found. Make sure the file exists and name that you enter of the file you wish to read is the location of the existing file.
- 11-16-2011, 10:09 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Files. What is wrong with this??
Well yeah. But I create the file, say with the name file1.txt, but when I search for it, it is nowhere to be found. Total beginner here so I guess I'm asking how i would go about fixing this?
- 11-16-2011, 10:10 PM #4
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Files. What is wrong with this??
Also, my intention is to be able to view the file in Notepad.... don't know if thats makes a difference or not.
- 11-16-2011, 10:30 PM #5
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Files. What is wrong with this??
I don't understand what you mean. Your program takes an input file and copies it to a new file, yes? Where is your first file, and what do you input into your program as this file name?Well yeah. But I create the file, say with the name file1.txt, but when I search for it, it is nowhere to be found.
- 11-16-2011, 10:36 PM #6
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Files. What is wrong with this??
Alright, like I said, total beginner here. I guess what I'm saying is, I don't know where the first file is. And I've named it several things on different run-throughs. But say I name it file1.txt. How would i go about viewing that file in notepad.?? Thanks for you help....
- 11-16-2011, 10:42 PM #7
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Files. What is wrong with this??
The file must exist prior to running your application - so you must know where it is. Create one, and pass its path as the first argument. For example, make this file in the same directory as your application, the pass that file name to your application when it requests file 1.
- 11-16-2011, 10:46 PM #8
Re: Files. What is wrong with this??
<nitpick>Java Code:public static void main(String[] args)throws IOException { .... System.exit(0); }
This line is totally pointless as the program will exit with or without it. Personal irritation.
- 11-16-2011, 10:52 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Files. What is wrong with this??
Ok...well, when I look in the directory where that app is, there is also a JCU file (?) But when I open that in notepad, it is in XML format. Would that be the file I created? This is my first app involving files so as you can tell, I'm just kind of lost.
- 11-16-2011, 11:13 PM #10
Re: Files. What is wrong with this??
If the print the File class's absolutePath method's value, it will show you where the system is looking for the file.
After you create an instance of a File object, print out its absolutePath.
- 11-16-2011, 11:19 PM #11
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Files. What is wrong with this??
Ok...sorry, but I have absolutely no idea what that means.
- 11-16-2011, 11:20 PM #12
Re: Files. What is wrong with this??
???
Is the object of your program is to convert the contents one file 1 to uppercase and store it in file 2? Then what you need to do is open notepad, type is some garbage in lowercase, save it. Then when you run your Java program you enter in the name of the file you just created for file 1. Enter in a different name for file 2 and once you Java program finishes file 2 should have been created. NOTE your Java program is not responsible for creating file 1 which you seem to be misunderstanding.
- 11-16-2011, 11:22 PM #13
Re: Files. What is wrong with this??
The File class has a method named absolutePath(). Read the API doc to get the correct spelling and usage.
On line 9 of your program you create an instance of a File object: readFile.
Use that instance to call the absolutePath method to display where the JVM is looking for your file:
Java Code:System.out.println("absPath= " + readFile.absolutePath()); // Show file's full pathLast edited by Norm; 11-17-2011 at 12:27 AM.
- 11-17-2011, 12:06 AM #14
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Files. What is wrong with this??
Ok...I went into notepad.. typed in some crap (abcdefghijk). Saved it as abc.txt Then went back into JCreator and ran my code and entered file 1 as abc.txt. I'm still getting the file not found exception though. Do I have to specify where that file is saved in my code?? Also...Thanks.
- 11-17-2011, 12:08 AM #15
Re: Files. What is wrong with this??
No your code should not be changed. Where did you save abc.txt? If it is not located in the same directory as your Java program it has no idea where to find it. Unless you provide the full path to the file.
- 11-17-2011, 12:13 AM #16
Re: Files. What is wrong with this??
Your code must provide the correct path to where the file is that it is trying to read.
- 11-17-2011, 12:16 AM #17
Re: Files. What is wrong with this??
Add this line as the first line of your program. The ouput will tell you where the JVM is looking for your abc.txt file. Unless you provide a full path when you enter the file name.Java Code:System.out.println(System.getProperty("user.dir"));
- 11-17-2011, 12:27 AM #18
Re: Files. What is wrong with this??
Its hard to understand why the OP can't type in the line of code provided in post#13 to see where the JVM is looking for the file.
- 11-17-2011, 01:19 AM #19
Member
- Join Date
- Nov 2011
- Posts
- 24
- Rep Power
- 0
Re: Files. What is wrong with this??
Where exactly would I enter that code? After the variable declaration?
- 11-17-2011, 01:27 AM #20
Similar Threads
-
Wrong output (well.. the one who's wrong is probably me ;) )
By shacht1 in forum New To JavaReplies: 4Last Post: 06-11-2013, 01:37 AM -
Java webstart having problem with multiple jar files and resource files
By rjalori in forum AWT / SwingReplies: 2Last Post: 06-11-2011, 01:54 PM -
Creating Jar Files with functioning input files
By appleLove in forum NetBeansReplies: 1Last Post: 04-10-2011, 10:37 PM -
Convert avi, mpeg, wmv media files to .flv files in java code
By vinay1497 in forum New To JavaReplies: 8Last Post: 07-30-2010, 05:47 PM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks