This question already has answers here:
Jar can't access txt files
(2 answers)
Java JAR can't find file
(1 answer)
Closed 2 years ago.
I have the following code for check if a file exists on some path, if not, that file is copy from the project, and it works.
public static void verificarDB() throws IOException {
File cygnus_db = new File(System.getProperty("user.home")+File.separator+".local"+File.separator+"cygnus"+File.separator+"cygnus_db_local.db");
if(!cygnus_db.exists()) {
File source = new File("src/assets/bd/cygnus_db_local.db");
File target = new File(System.getProperty("user.home")+File.separator+".local"+File.separator+"cygnus"+File.separator+"cygnus_db_local.db");
FileUtils.copyFile(source, target);
}
}
But when i export my project to a runnable jar i get this exception:
java.io.FileNotFoundException: Source 'src/assets/bd/cygnus_db_local.db' does not exist
at org.apache.commons.io.FileUtils.checkFileRequirements(FileUtils.java:1383)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1060)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1028)
at pruebas.Main.verificarDB(Main.java:123)
at pruebas.Main.main(Main.java:30)
What im doing wrong?
Related
This question already has answers here:
How do I check if a file exists in Java?
(19 answers)
Closed 3 years ago.
In Java using Maven project we may read file's content as a stream by knowing only file's name, for example:
InputStream in = getClass().getResourceAsStream("/" + fileName);
But is there way to check if the file exists without indicating the whole path, just passing file name?
file.exists() can be used to check whether such a file exists or not.Like following:
File file = new File("filepath");
if(file.exists()){
// Do your stuff
}
This question already has answers here:
How to handle ~ in file paths
(5 answers)
Closed 3 years ago.
File f = new File("~/NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
cat ~/NetBeansProjects/ChatApp/src/chatapp/Server.java, prints the content of the file.
But the above program doesn't print "File exist".
The ~ is resolved by the shell, whereas Java do not resolve it. Try something like this:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
The "home" wildcard (~) cannot be resolved in the JVM. You need to load that property via the Java API:
File f = new File(System.getProperty("user.home"), "NetBeansProjects/ChatApp/src/chatapp/Server.java");
if(f.exists()) {
System.out.println("File exist");
}
This question already has answers here:
Create a java file with directory
(4 answers)
Closed 4 years ago.
I wanted to create a file in a directory, using below code:
import java.io.*;
class Test2 {
public static void main (String[] args) throws IOException {
File f = new File("D:\\docs\\voucherList_cinthol_2018-04-25 11-46-29.csv");
System.out.println(f.createNewFile());
}
}
here, docs directory is not there in d: drive. so I wanted both directory and file to be created.
I am getting below exception :
Exception in thread "main" java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at com.wpits.acf.utils.Test2.main(Test2.java:14)
How can I resolve this problem, please suggest
You can use guava to handle this.
https://google.github.io/guava/releases/23.0/api/docs/com/google/common/io/Files.html#createParentDirs-java.io.File-
This ensures that the target file gets created even if embedded in a deep hierarchy of directories.
This question already has answers here:
How to read all files in a folder from Java?
(33 answers)
Get a list of resources from classpath directory
(15 answers)
Closed 6 years ago.
I am trying to read the content of the folder of my Java EE Spring application, but it always return me null. The folder I want is under src/main/resources folder and it`s called context. I try doing it this way :
File file = new File("src/main/resources/context")
But it always return me null for file.
You can use one of the following:
File file = new File("src/main/resources/context");
String[] list = file.list(); // returns an array of all file names in the context folder.
OR
File file = new File("src/main/resources/context");
File[] listFiles = file.listFiles(); // returns an array of all "file objects" for all files in the context folder.
Hope this helps!
This question already has answers here:
Get real file extension -Java code
(3 answers)
Closed 7 years ago.
I know I can get this by doing this
String ext = FilenameUtils.getExtension("/path/to/file/foo.txt");
But what if someone tries to upload a foo.exe file by just changing the extension type to foo.doc. Is there any way through which I can get the actual extension type without reading the content of file
File file = new File("filename.asgdsag");
InputStream is = new BufferedInputStream(new FileInputStream(file));
String mimeType = URLConnection.guessContentTypeFromStream(is);
From this post : Get real file extension -Java code
Also using java7, you should check out this :
public static String probeContentType(Path path)
throws IOException
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#probeContentType%28java.nio.file.Path%29