Results 1 to 9 of 9
  1. #1
    TannerLD is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default java.io.File - Files do not exist

    Hi all,

    I'm trying to make a program copy files, but I'm having more trouble getting Java to recognize the files via the java.io.File object. Whenever I call whateverFile.exists() it returns false, even though the path of the file is correct.

    Java Code:
    import java.io.File;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    public class TestFiles {
    	public static void main(String args[]) throws URISyntaxException
    	{
                    //These two lines do not work, but the paths are correct and the files exist.
    		//File source = new File(new URI("file:///Volumes/public/buzzer.wmv"));
    		//File source = new File("/Users/tanner/Desktop/buzzer.wmv");
    
                    //This line works.
    		File source = new File("TestFiles.java");
    		
    		System.out.println("Exists: "+source.exists());
    	}
    }
    Am I missing something, because this seems like it should work?

    Thanks
    -Tanner
    Last edited by TannerLD; 09-01-2009 at 02:29 AM.

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Try this:

    Java Code:
    import java.io.File;
    import java.net.URI;
    import java.net.URISyntaxException;
    
    public class TestFiles {
       public static void main(String args[]) throws URISyntaxException
       {
          //These two lines do not work, but the paths are correct and the files exist.
          //File source = new File(new URI("file:///Volumes/public/buzzer.wmv"));
    
          //This line works.
          //File source = new File("/Users/tanner/Desktop/buzzer.wmv");
    
          TestFiles base = new TestFiles();
    
          String path = base.getClass().getResource("").toString();
          System.out.println(path+"TestFiles.java");
    
          File source = new File(path+"TestFiles.java");
    
          System.out.println("Exists: "+source.exists());
       }
    }
    My Hobby Project: LegacyClone

  3. #3
    TannerLD is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Try this:

    [code removed for length's sake]
    That does not work.

    I think I made this confusing in my first post (I'll edit it to reflect this):

    Does Not Work (both - what I need/want):
    File source = new File(new URI("file:///Volumes/public/buzzer.wmv"));
    File source = new File("/Users/tanner/Desktop/buzzer.wmv");

    Works (but not what I want):
    File source = new File("TestFiles.java");

    Thanks
    -Tanner

  4. #4
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    Oops, sorry, paths are such a pain in java, hehe.

    Java Code:
    TestFiles base = new TestFiles();
    
    String path = base.getClass().getResource("").toString();
    System.out.println(path);
    This gives you a relative path to where things will be at run time if you don't happen to know the actual path on the machine you run it on. You could put your files there. Otherwise you will have to give an exact path to the file.
    Last edited by mrmatt1111; 09-01-2009 at 03:22 AM.
    My Hobby Project: LegacyClone

  5. #5
    TannerLD is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by mrmatt1111 View Post
    Oops, sorry, paths are such a pain in java, hehe.

    Java Code:
    TestFiles base = new TestFiles();
    
    String path = base.getClass().getResource("").toString();
    System.out.println(path);
    This gives you a relative path to where things will be at run time if you don't happen to know the actual path on the machine you run it on. You could put your files there. Otherwise you will have to give an exact path to the file.
    Thanks, but I can't move all the files to the source code directory due to the amount of them being in the several hundred gigabytes, which is why I'm trying to copy a specific one from the /Volumes/public/Music path. That's what I'm trying to get at.

    Still confusing? :\

    -Tanner

  6. #6
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    5

    Default

    This should give you the path:

    Java Code:
    javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
    
    int ok = fileChooser.showOpenDialog(null);
    if(ok == javax.swing.JFileChooser.APPROVE_OPTION)
    {
       File file = fileChooser.getSelectedFile();
       System.out.println(file); //here be dragons... err, your file
    }
    You can use it once just to locate the correct path.
    My Hobby Project: LegacyClone

  7. #7
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Quote Originally Posted by mrmatt1111 View Post
    This should give you the path:

    Java Code:
    javax.swing.JFileChooser fileChooser = new javax.swing.JFileChooser();
    
    int ok = fileChooser.showOpenDialog(null);
    if(ok == javax.swing.JFileChooser.APPROVE_OPTION)
    {
       File file = fileChooser.getSelectedFile();
       System.out.println(file); //here be dragons... err, your file
    }
    You can use it once just to locate the correct path.
    System.out.println(file,getAbsolutePath()); //here be dragons... err, your file

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,846
    Rep Power
    19

    Default

    Quote Originally Posted by TannerLD View Post
    Does Not Work (both - what I need/want):
    File source = new File("/Users/tanner/Desktop/buzzer.wmv");

    Thanks
    -Tanner
    OK, this works, where test.txt is located at "C:\test\test.txt":

    Java Code:
    public static void main(String args[]) {
    	File source = new File("/test/test.txt");
    	System.out.println(source.exists());
    }
    So, taking your line above, where is buzzer.wmv located exactly...paste the path here.

  9. #9
    TannerLD is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Ah. I fixed it by simply not using the urge to escape everything that is a file path. They don't need to be. Then it works.

    Thanks
    -Tanner

Similar Threads

  1. package java.text does not exist
    By sknn in forum NetBeans
    Replies: 12
    Last Post: 02-03-2010, 03:14 PM
  2. conversion of java .class files to .java files
    By kotturupraveen in forum New To Java
    Replies: 2
    Last Post: 06-09-2008, 12:58 PM
  3. JAR file with .java files
    By ravian in forum New To Java
    Replies: 7
    Last Post: 12-24-2007, 08:15 PM
  4. Replies: 20
    Last Post: 12-17-2007, 03:20 PM
  5. Replies: 3
    Last Post: 11-03-2007, 01:58 PM

Tags for this Thread

Posting Permissions

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