Files. What is wrong with this??
Here is my code:
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);
}
}
It asks me to enter the names of file 1 and 2, then when I hit enter, I get these errors:
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.
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.
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?
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.
Re: Files. What is wrong with this??
Quote:
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.
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?
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....
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.
Re: Files. What is wrong with this??
Code:
public static void main(String[] args)throws IOException {
....
System.exit(0);
}
<nitpick>
This line is totally pointless as the program will exit with or without it. Personal irritation.
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.
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.
Re: Files. What is wrong with this??
Ok...sorry, but I have absolutely no idea what that means.
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.
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:
Code:
System.out.println("absPath= " + readFile.absolutePath()); // Show file's full path
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.
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.
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.
Re: Files. What is wrong with this??
Code:
System.out.println(System.getProperty("user.dir"));
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.
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.
Re: Files. What is wrong with this??
Where exactly would I enter that code? After the variable declaration?
Re: Files. What is wrong with this??
I'm sorry I didn't realise my reply 17 was invisible.