Page 1 of 2 12 LastLast
Results 1 to 20 of 35
  1. #1
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Angry Files. What is wrong with this??

    Here is my code:

    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);
    	}
    }
    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.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.

  3. #3
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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?

  4. #4
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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.

  5. #5
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.
    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?

  6. #6
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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....

  7. #7
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default 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.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Files. What is wrong with this??

    Java 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.

  9. #9
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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.

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default 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. #11
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Files. What is wrong with this??

    Ok...sorry, but I have absolutely no idea what that means.

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default 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.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default 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 path
    Last edited by Norm; 11-17-2011 at 12:27 AM.

  14. #14
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default 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.

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default 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.

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default 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.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Files. What is wrong with this??

    Java 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.

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default 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.

  19. #19
    Tullamore is offline Member
    Join Date
    Nov 2011
    Posts
    24
    Rep Power
    0

    Default Re: Files. What is wrong with this??

    Where exactly would I enter that code? After the variable declaration?

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Files. What is wrong with this??

    I'm sorry I didn't realise my reply 17 was invisible.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Replies: 2
    Last Post: 06-11-2011, 01:54 PM
  3. Creating Jar Files with functioning input files
    By appleLove in forum NetBeans
    Replies: 1
    Last Post: 04-10-2011, 10:37 PM
  4. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  5. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •