Getting a time of deleted folder \ file - java

I am a newbie. A wanna to check for existing a folder or file in directory in past.
For better. Example, i may a directory C:\Users\Admin\AppData\ and i wanna to check of existing a directory Test in that path. That maybe be checket by:
File file = new File(System.getenv("APPDATA") + "\\Test\\");
if(file.isDirectory()){
///...
} else { ////....}
But i wanna to check if that directory is deleted - when. Please help with code examples... be VERY and VERY thanks

Instead of the File class, I recommend looking at the Files class - it is there to help you do many things. For example, Files.createFile(...) will check to see if a file exists before creating. You can then pass a positive result to FileWriter(...) for your work.
You can check for the presence of a file of folder, but not that it was deleted (e.g. checking a log file of past actions). I recommend using the logic of "if not there then it never existed or was deleted". Another option when working with files is to use parameters to always overwrite the file if that is what you want.
You are asking a question about the operating system. What happens after a file or folder is deleted is unique to each operating system. A notional recycle bin's awareness of a file or folder's original location was, and where that content may have been moved to is specific to an operating system (and usually isn't just moved into another folder).

Related

Scanner cannot find (correct) file path

I'm programming in Java with IntelliJ and have been trying to use the Scanner class to read the file. Even with the correct path, I still get a "No such file or directory" error. Does anyone have any suggestions?
My working directory is /Users/kevinliu/Desktop/test
Here is a picture of how the project is set-up.
Are you trying to create a swing/console application using maven?
If yes, maven is not able to find the source. You have to add it on the pom file. See here on how to add it on pom file.
if no, do you have rights to access the address of the image file? Some times, folder are protected by the OS.
You can also use YourClassName.class.getResource("input/input1.txt") to locate file/s under the directory that your class was in.
Even with the correct path, I still get a "No such file or directory" error.
The path is NOT correct. That path says look for a directory called "src" in the root directory of your computer. That is almost certainly not where the input file lives.
If you are going to use an absolute pathname for a file within the working directory that you stated, it should look like this:
/Users/kevinliu/Desktop/test/src/input/input1.txt
(You can check what it will actually be using a file browser ... outside of Intellij.)
If you want to use a relative pathname, try this
src/input/input1.txt
Notes:
There is no leading "/" on a relative pathname. A leading "/" means it is an absolute pathname. Absolute pathnames start at the root directory.
A relative path is resolved relative to the >>current<< working directory. That will depend on where and how you run the application ...
For a production application, you would not want to refer to a file in the source tree. The end user typically won't have the source tree.
Consider making the path a command line argument or configuration setting for your application.
Consider making the file a "resource" that is part of the application's JAR file. (You would open it a different way ...)
If you ever get a "No such file or directory" message, that means that the path is not correct in some sense. You might be in the wrong place, you might not have permission on a parent directory, the file may have been removed or renamed, there may be a you, or something else. Either way, that error comes from the operating system and the OS doesn't make mistakes about these things. The mistake will be yours (or the user's).

How can I get Eclipse to find my .txt file in java ON A MAC? [duplicate]

I need to write a program that asks for the file name of a text document of number and then calculates average, median, etc., from this data set. I have written the program so that runs correctly when I input the full path such as "C:\Users\COSC\Documents\inputValues2.txt", however it will not run when I simply input inputValues2.txt. I have been researching the different between the two but am not fully understanding how to fix this. Since it is running correctly, otherwise, I don't believe it is a problem with the code, but I am new to this so I may be wrong.
Your program needs to know the full path in order to find the file. It isn't just searching your computer for the file "inputValues2.txt". It needs to know exactly how to get there. If you wanted to, you could move the file into your project folder, and then you would just be able to write "inputValues2.txt" to access it. I normally create a folder called "res" in my project folder, and then let's say I am trying to create an image:
Image i = new Image("res/img.png");
Your file should be in the class-path. That's in the same directory that your main class is in.
The suggested practice is to place it in a Resources directory inside your class-path, then you can access it via, "Resources/inputValues2.txt".

Relative/Absolute Path in Java

I need to write a program that asks for the file name of a text document of number and then calculates average, median, etc., from this data set. I have written the program so that runs correctly when I input the full path such as "C:\Users\COSC\Documents\inputValues2.txt", however it will not run when I simply input inputValues2.txt. I have been researching the different between the two but am not fully understanding how to fix this. Since it is running correctly, otherwise, I don't believe it is a problem with the code, but I am new to this so I may be wrong.
Your program needs to know the full path in order to find the file. It isn't just searching your computer for the file "inputValues2.txt". It needs to know exactly how to get there. If you wanted to, you could move the file into your project folder, and then you would just be able to write "inputValues2.txt" to access it. I normally create a folder called "res" in my project folder, and then let's say I am trying to create an image:
Image i = new Image("res/img.png");
Your file should be in the class-path. That's in the same directory that your main class is in.
The suggested practice is to place it in a Resources directory inside your class-path, then you can access it via, "Resources/inputValues2.txt".

copy the contents of a local directory to a directory in hdfs

I have got a requirement that I should be able to copy the contents of a directory from local system to a directory on HDFS.
The condition is only the directory contents should be copied to the location I have specified, not the source directory itself. Using command copyfromlocal I can achieve this. But I need to use Java. There is this method copyFromLocalFile which should be used for making a copy from local file system, the problem is it copies the directory itself. Also tried using FileUtils.copy method, gives the same result as the copyFromLocalFile
As a test I tried to copy directory contents from a test directory to another directory , both on the local file system. I used FileUtils.copyDirectory. This works but I cannot use it for HDFS. I have seen many links to related to this same question but could not find any way.
Guys, could you please let me know if this is possible or not or is it some design flaw? If this is possible how can I proceed ?
Ya it's really hard to get things done with FileSystem api exactly the way you want. Insufficient documentation makes things even harder(Too many methods, little explanation). I faced the same problem an year ago.
The only solution I got is iterative :
void copyFilesToDir(String srcDirPath, FileSystem fs, String destDirPath)
throws IOException {
File dir = new File(srcDirPath);
for (File file : dir.listFiles()) {
fs.copyFromLocalFile(new Path(file.getPath()),
new Path(destDirPath, file.getName()));
}
}
I think the code needs little explanation.

How to create an .INI file to store some settings in Java?

I want to create an ini file to store some settings for my application. Is it a good idea to find where the jar file is located and create an ini file there? If yes, then how can I find the location of the jar file?
But if you know a better solution for something like this, I would like to hear some of them.
EDIT: I'm using mac and I want to run the same application in windows. I could write something in the System.getProperty("user.home") directory, but I want to keep the system clean, if the user decides to remove the app. There is no a better way to store the settings file, for example in the same directory with the application?
You can locate your application directory using the ClassLoader. See: Java: finding the application directory. Rather than an .INI file, use a .properties file - you can load and save this via the Properties class.
As others have noted, you should not write user settings to your application directory. What if the user does not have write access to the application directory? What if your application is being used by multiple users on the same system at the same time? Neither of these situations are unusual, even on Windows.
You might still want to load some settings from the application directory - perhaps the administrator has configured default settings there.
A common convention is to save user settings to the user's home directory:
/home/user/.eclipse
C:\Documents and Settings\User\.eclipse
Although this means you might leave stray files behind, this can be beneficial if the user re-installs the app. Document such things in a README. Here is how to create and get a reference to the directory:
public static File getSettingsDirectory() {
String userHome = System.getProperty("user.home");
if(userHome == null) {
throw new IllegalStateException("user.home==null");
}
File home = new File(userHome);
File settingsDirectory = new File(home, ".myappdir");
if(!settingsDirectory.exists()) {
if(!settingsDirectory.mkdir()) {
throw new IllegalStateException(settingsDirectory.toString());
}
}
return settingsDirectory;
}
On unix-like operating systems, starting the directory name with a period (".myappdir") will make the directory hidden. On Windows, it will be located below My Documents, so users will not see the directory unless they go looking for it.
If the settings are only written by your application (rather than edited manually), consider using the Preferences API.
You should not be storing temp files in the install directory of an application. Remember, the user running the application may not have write access to that directory. The safest place to put stuff like that is in C:\Documents and Settings\username\Application Data\ApplicationName folder (adjusting the name as necessary).
That said, however, I would probably store that type of stuff in the registry instead of a file on their computer. (But, that's just me.)
Typically Java programmers don't use .ini files, but .properties files (different format). You can use the java.lang.Properties class as a nice programmatic wrapper if you do.
While you can get the location of your jar file by calling getProtectionDomain().getCodeSource().getLocation() on your class's .class member, I do not recommend that you do this.
I would instead write the file to the System.getProperty("user.home") directory - the users' home directory, or if it is truly temporary, System.getProperty("java.io.tmpdir")
It depends whether your ini needs to be human readable/writable under normal circumstances. If not, you can use a properties file rather than an ini file, and store it in the "user" directory.
As for finding the jar file, you would have to find the ClassLoader for a class known to be loaded from the jar, check that it was the appropriate type of ClassLoader (ie that it's really been loaded from a jar), and you can extract the path from that. I can probably dig out the code to do this if that's really what you want. I wouldn't necessarily recommend it.
EDIT The user.home property will give you the user directory, which you can safely use.
The idea with the .properties file instead of the INI file is good. Also, if you store some sensitive data in there, you may consider encrypting it. Check this out:
https://www.owasp.org/index.php/How_to_encrypt_a_properties_file
or this:
encrypt and decrypt property file value in java

Categories

Resources