How to check if file exists knowing only file name? [duplicate] - java

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
}

Related

Read file in java linux? [duplicate]

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");
}

Get files inside folder [duplicate]

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!

How can I get the actual file extension in Java [duplicate]

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

How to create a file & folder in Java? [duplicate]

This question already has answers here:
create a text file in a folder
(4 answers)
Closed 9 years ago.
Okay, updated this right now
As of now I have this:
File saveGame = new File(Config.saveDir,Config.saveName);
Now how would I create that into a text file? My saveDir has already been created (mkDir), and my saveName is defined as "xyz.txt", so what method do I use to create this file (and later add text into it)?
new File(...) doesn't crete a file on disk, it only creates a path for a Java program to use to refer to a file that may or may not exist on disk (hence the File.exists() method).
Try userFile.createNewFile(); to actually create the file on disk.
To make the directory you would need to use the File.mkdirs() method, but don't call it on userFile or it will make a directory with the Savegame.txt in it.
Edit:
File dir = new File(Config.userpath + "/test/");
File file = new File(dir, "," + Config.name + " Savegame.txt");
dir.mkdir(); // should check to see if it succeeds (if(dir.mkdir())...)
file.createNewFile(); // should also check that this succeeds.

Collect metadata from file java [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Getting the last modified date of a file in Java
Is there a way to get file metadata like "date modified" in java?
The java.io.File class has several methods that could be of interest to you, e.g. lastModified().
java.io.File#lastModified().
File f = new File(...);
if (f.exists())
{
System.out.println(new Date(f.lastModified()));
}

Categories

Resources