This question already has answers here:
Java Paths.get .... readAllBytes(path)) not working with relative path
(2 answers)
Java isn't getting the file in the source code when compiled
(2 answers)
Closed 5 years ago.
I have an input file data, name input.dat which is stored in the src/main/resources. When I tried to read from this input file as follows:
String[] lines = Files.readAllLines(new File("input.dat").toPath()).toArray(new String[0]);
I received the following exception:
java.nio.file.NoSuchFileException: input.dat
Can anyone help me with what did I do wrong here? Thank you in advance!
The java file you are running is probably not in the same directory as the file input.dat.
When referencing the file in your Java code include the relative path to it. If the java file that you are running is in src/main then the relative path would be resources/input.dat.
In that case, your code would look like this:
String[] lines = Files.readAllLines(new File("resources/input.dat").toPath()).toArray(new String[0]);
Hope this helped you and if you have any further questions please feel free to post a comment below!
Replace input.dat with the exact path of input.dat. Such as C:/Users/Soe/Desktop/input.dat
Related
This question already has answers here:
How do I programmatically change file permissions?
(12 answers)
Closed 3 years ago.
I'm using the following Java code to create a file inside several folders:
Path pathOut = Paths.get("./", year, month, day, processId, "METADATA.txt");
File fileOut = pathOut.toFile();
boolean f = fileOut.getParentFile().mkdirs();
It works perfectly, but I'd need to set the permissions for each subfolder to '777'. Is it possible using mkdirs method or do I need to change the logic and use another approach?
Thanks!
Permission 777 is the same as rwxrwxrwx. Thus, you can set the required permission as follows:
Files.setPosixFilePermissions(path, PosixFilePermissions.fromString("rwxrwxrwx"))
This question already has an answer here:
Any way to get a File object from a JAR
(1 answer)
Closed 5 years ago.
I'm trying to implement a button into my project which, when clicked, automatically loads a specific file. Currently there are buttons for users selecting a file from their hard disk.
So, I downloaded the specific file and inserted it into the project. When using File f = new File("demofile") or something like this
getClass().getResource("/resources/file.txt").getFile(); the code WORKS locally.
However, when the project is packaged, a FileNotFoundException is thrown.
After much research online, there are suggestions to use something like:
InputStream is = getClass().getResourceAsStream("/resources/file.txt");
However, for this project, I need the file to be referenced as a file object so that it can be passed as an argument to other functions, such as:
in = new TextFileFeaturedSequenceReader(TextFileFeaturedSequenceReader.FASTA_FORMAT, file, DiffEditFeaturedSequence.class);
Any ideas on how I can solve this, or read a stream into a file object?
Thanks!
If you absolutely must pass a File, copy your resource to a temporary file:
Path path = Files.createTempFile(null, null);
try (InputStream stream =
getClass().getResourceAsStream("/resources/file.txt")) {
Files.copy(stream, path, StandardCopyOption.REPLACE_EXISTING);
}
in = new TextFileFeaturedSequenceReader(
TextFileFeaturedSequenceReader.FASTA_FORMAT,
path.toFile(),
DiffEditFeaturedSequence.class);
// Use the TextFileFeaturedSequenceReader as needed
// ...
Files.delete(path);
This question already has answers here:
Accessing files with spaces in filename from Java
(6 answers)
Closed 7 years ago.
I need parse .yml file with configuration.
File is located in src/main/resources/configFile.yml
Right now i have no problem until there is no blank space in some folder name in the path.
Example:
E:\Tomcat-7.0\webapps\applicationName\WEB-INF\classes\configFile.yml (OK)
E:\Tomcat 7.0\webapps\applicationName\WEB-INF\classes\configFile.yml (FAIL)
In second example i got java.io.FileNotFoundException.
I can not affect path. So i am looking for solution which will be able to handle it. Could you suggest how to proceed?
You should escape the space in the name, like:
"E:\Tomcat\ 7.0\webapps\applicationName\WEB-INF\classes\configFile.yml"
Or decode the path:
String decodedPath = URLDecoder.decode(path);
This question already has answers here:
getClass().getResource() always returning null
(5 answers)
Closed 8 years ago.
I am trying to export an image inside my runnable jar. After some research i have come across this method:
ImageIO.read(getClass().getClassLoader().getResource("image path"));
After following several response and looking at the API, i have attempted this myself. My current directory set up is as follows:
This is my code:
cherryImg = ImageIO.read(getClass().getClassLoader().getResource("/res/cherry.png"));
which is being called from the Snake.java source file.
This is the error message:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at cje.chris.edwards.snake.game.Snake.<init>(Snake.java:51)
at cje.chris.edwards.snake.game.Snake.main(Snake.java:156)
Snake.java:51 is the line of code posted above. I understand this is because it cannot find the resource, what am i doing wrong? I have added the res folder to my build path.
I have also tried:
cherryImg = ImageIO.read(getClass().getClassLoader().getResource("res/cherry.png"));
This produces the same error message.
Answer:
cherryImg = ImageIO.read(getClass().getClassLoader().getResource("cherry.png"));
Thanks.
I think your problem is the leading "/"
"/res/cherry.png"
Will make it an absolute path (from your root of your drive) not a relative path from your starting from the root of your jar
Making it "res/cherry.png" should work
This question already has answers here:
How do I read a resource file from a Java jar file?
(9 answers)
Closed 9 years ago.
I know it seems like my question has been answered before, but I can't find an answer for my case.
What I am trying to do, is to get a File type from resource, so when it's packaged, it can be accessed. The answers I found were trying to read that file, but what I really need, is to construct a File object, because I have a third party library that is expecting that object.
Here is a the code I tried:
String xslFilePath = new Test().getClass().getResource("/com/test/decision_template.xsl").getPath();
System.out.println(xslFilePath);
File xsltfile = new File(xslFilePath);
System.out.println(xsltfile.getAbsolutePath()+", exist:"+xsltfile.exists());
I got this result:
C:\>java -jar test.jar
file:/C:/test.jar!/com/test/decision_template.xsl
C:\\file:\C:\test.jar!\com\test\decision_template.xsl, exist:false
java.io.FileNotFoundException: file:\C:\test.jar!\com\test\decision_template.xsl
(Syntaxe du nom de fichier, de rÚpertoire ou de volume incorrecte)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at com.test.Test.main(Test.java:30)
I need a way to get that file so the .exists() return true.
You can try the TrueZip library. It's quite good. I believe you'll need to implement your own DirectoryScanner or something.
Better yet, just knock up an implementation of your own which scans through all the jars on your classpath. Here's an example of how I've done it in one of my projects on GitHub. I think that you could easily tailor it to suit your needs.
Have fun! :)