Results 1 to 9 of 9
- 09-01-2009, 02:00 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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.
Am I missing something, because this seems like it should work?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()); } }
Thanks
-TannerLast edited by TannerLD; 09-01-2009 at 02:29 AM.
- 09-01-2009, 02:22 AM #2
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
- 09-01-2009, 02:28 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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
- 09-01-2009, 03:11 AM #4
Oops, sorry, paths are such a pain in java, hehe.
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.Java Code:TestFiles base = new TestFiles(); String path = base.getClass().getResource("").toString(); System.out.println(path);Last edited by mrmatt1111; 09-01-2009 at 03:22 AM.
My Hobby Project: LegacyClone
- 09-01-2009, 03:34 AM #5
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
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
- 09-01-2009, 03:43 AM #6
This should give you the path:
You can use it once just to locate the correct 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 }My Hobby Project: LegacyClone
- 09-01-2009, 07:28 AM #7
- 09-01-2009, 01:08 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
OK, this works, where test.txt is located at "C:\test\test.txt":
So, taking your line above, where is buzzer.wmv located exactly...paste the path here.Java Code:public static void main(String args[]) { File source = new File("/test/test.txt"); System.out.println(source.exists()); }
- 09-10-2009, 12:03 AM #9
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
package java.text does not exist
By sknn in forum NetBeansReplies: 12Last Post: 02-03-2010, 03:14 PM -
conversion of java .class files to .java files
By kotturupraveen in forum New To JavaReplies: 2Last Post: 06-09-2008, 12:58 PM -
JAR file with .java files
By ravian in forum New To JavaReplies: 7Last Post: 12-24-2007, 08:15 PM -
can java.io.File create a list of all files and folders.
By MattStone in forum New To JavaReplies: 20Last Post: 12-17-2007, 03:20 PM -
problem with viewing .java files when Visual J# 2005 Express Edition exist
By unhurt in forum New To JavaReplies: 3Last Post: 11-03-2007, 01:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks