I have a text file that I am trying to read from using a BufferedReader however I'm not sure where I should place this text file in my Java Project and also what that directory would be called.
For example:
File file = new File(Directory that needs to be specified);
From what I understood you are not sure where is your root directory, put your file in you project home directory instead of /src . For example if your project is called MyProject, put your file in .../MyProject/myfile.txt and your relative path would look like this: File file = new File("myfile.txt");
If you want your file in separate folder put it for example in /MyProject/resources/ and then your path would look like this:
File file = new File("/resources/myfile.txt");
Related
I am making a game with java as kind of a side project and am still new to the language. I was wondering how I could run a file when i didn't know the complete path, like if I were to send the game to a friend, and he has a different path location than me.
Thank you in advance for any help.
code I am currently using:
File file = new File("/Users/(my name)/Desktop/script1.vbs");
Desktop desktop = Desktop.getDesktop();
if(file.exists()) desktop.open(file);`
You could place the file (script1.vbs) in the project folder, that way the path would always be like this...
File file = new File("script1.vbs")
Place the file, not in the src or bin folder, but in the root folder.
Instead of
File file = new File("/Users/(my name)/Desktop/script1.vbs");
you should better use
File file = new File(System.getProperty("user.home"), "Desktop/script1.vbs");
See also the list of System Properties
Or you can use this, to locate your file in the resources folder:
File file = new File("classpath:com/company/script1.vbs");
"classpath:Route Of Source folders/Name and extension of the file"
I have myFile.txt located in myJavaProject\webResources\sampleTransmissions. I am trying to read the file in myJavaProject\src\package1\notMyMainClass.java and I am having trouble building the path properly to access the webResources directory. Also, I would like to read the file using Files.readAllBytes(Path path) because I need to get non-printable characters to work with
edit: this would be my project structure
+myJavaProject
++src
+++notMyMainClass.java
++webResources
+++sampleTransmissions
++++myFile.txt
Are you looking for something like this:
Scanner scanner = new Scanner(new File("webResources/sampleTransmissions/myFile.txt"));
This assumes that "myJavaProject" is your working directory. If you want it to work indepenently from the working directory, use an absolute path.
The forward slashes should work on Windows as well. If you prefer backslashes, you need to write "webResources\\sampleTransmissions\\myFile.txt"
Note: The working directory has nothing to do with the location of your .java or .class file.
Think I found the answer to this question on another post..
Reading .txt file from another directory
getParentFile() would be the way to do this.
http://www.tutorialspoint.com/java/io/file_getparentfile.htm
File currentDir = new File(".");
File parentDir = currentDir.getParentFile();
File newFile = new File(parentDir,"Example.txt");;
I have an application that creates a temporary mp3-file and puts it in a directory like C:\
File tempfile = File.createTempFile("something", ".mp3", new File("C:\\));
I'm able to read it by just using that same tempfile again.
Everything works fine in the Eclipse IDE.
But when I export my project for as a Runnable jar, my files are still being made correctly (I can play them with some normal music player like iTunes) but I can't seem to read them anymore in my application.
I found out that I need to use something like getClass().getResource("/relative/path/in/jar.mp3") for using resource files that are in the jar. But this doesn't seem to work if I want to select a file from a certain location in my file system like C:\something.mp3
Can somebody help me on this one?
It seems you dont have file name of the temp files . When you was running your program in eclipse that instance was creating a processing files, but after you made a runable you are not able to read those file that instance in eclipse created, You runable file can create its own temp file and can process them,
To make temp files globe put there (path + name ) entries in some db or property file
For example of you will create a temp file from the blow code
File tempfile = File.createTempFile("out", ".txt", new File("D:\\"));
FileWriter fstream = new FileWriter(tempfile);//write in file
out = new BufferedWriter(fstream);
the out will not be out.txt file it will be
out6654748541383250156.txt // it mean a randum number will be append with file
and you code in runable jar is no able to find these temp files
getClass().getResource() only reads resources that are on your classpath. The path that is passed to getResource() is, in fact, a path relative to any paths on your current classpath. This sounds a bit confusing, so I'll give an example:
If your classpath includes a directory C:\development\resources, you would be able to load any file under this directory using getResource(). For example, there is a file C:\development\resources\mp3\song.mp3. You could load this file by calling
getClass().getResource("mp3/song.mp3");
Bottom line: if you want to read files using getResource(), you will need those files to be on your classpath.
For loading from both privileged JARs and the file system, I have had to use two different mechanisms:
getClass().getClassLoader().getResource(path), and if that returns null,
new File(path).toURI().toURL();
You could turn this into a ResourceResolver strategy that uses the classpath method and one or more file methods (perhaps using different base paths).
I am using a method that requires a string path to a file that is in my src directory structure in eclipse. Is the path to this file simply src\fileName.txt or is there a different way i should be getting this file as it doesnt seem to be working currently
Thanks
Run this and you will never forget how to remember.
File file = new File("sample.txt");
System.out.println(file.getAbsolutePath());
If you are following the maven standard directory structure, class files will be located relative to the classpath like so:
If you want SomeClass.class, you would access it by com.mydomain.packageorappname.deeperpackage.SomeClass. Is that what you are asking?
I'm trying to get the path to a file that it is located out of the java jar and I don't want to use an absolute path. An exampel: lets say that the jar is located in ~/lib/myjar.jar and the file is located in the same folder. What I've trying is something like this, but it fails:
File myfile = new File(this.getClass().getResource("../../../").toURI());
Note: my package is com.contro.gui, that's why I have "../../../", in order to acces to the "root"
I'm not sure how I can access to the file. Any suggestion?? And what about if the file that I want to access is in other folder like ~/res/ ???
If the file is in the same directory as the jar, I think this will work (feels fairly hacky, but...):
URL url = getClass().getProtectionDomain().getCodeSource().getLocation();
File myfile = new File(url.toURI());
File dir = myfile.getParentFile(); // strip off .jar file
(Haven't tested this, but it seems feasible. Will only work with file-based jars of course).
If the file is in some random location, I think you will need to either pass in parameters or check "known" locations like user.home. (Or, you could always put the file in the jar a use getResource().)
No just do
File FileName = new File(".");
String Directory = FileName.getCanonicalPath();
that will get you the parent directory of your class in a file or jar just remember to set the directory if it's in a jar like this "NameOfJar\NameOfFolder\etc"