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()));
}
Related
This question already has answers here:
System.out.println to text file
(3 answers)
Closed 2 years ago.
Running my code, I would like to save all the System.out.println() in a file.
For example:
System.out.println("Save this!");
I would need a file .txt in which is stored the string "Save this!".
Someone could help me?
Regards,
Francesco Campanile
Use this to Write console to a file.
PrintStream out = new PrintStream(
new FileOutputStream("output.txt", true), true);
System.setOut(out);
The output file will be on your project's root directory
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 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 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:
How do I save a String to a text file using Java?
(24 answers)
Closed 6 years ago.
I have two string variables containing some user input.
String name="neil";
String mobile="5555";
now I want to display neil an 5555 in a text file. Please help me to display this in a text file, I know how to display content of a file which is already exist, but donot know how to work with this case, please help me...
PrintWriter out = response.getWriter();
This should be changed to use "java.io.File" class.
Has been explained in this thread: How to use PrintWriter and File classes in Java?
File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();
PrintWriter out = new PrintWriter(file);
Something like this.
ps: check out "try-with-resources"