I have created Hidden folder in windows from this link..Now i have to delete one file in that hidden folder.
eg.C:\program files\test\abc.xml
I want to delete abc.xml and i have used following code for delete in java
String path="C:\program files\test\abc.xml"
if(new File(path).exists()){
new File(path).delete()
}
The attribute set as N for this file instead of deleting. I do not know what happening?
Note: abc.xml file is encrypted file.
Can any one help me.
Thanks.
String path="C:\\program files\\test\\abc.xml"
Try escaping your backslashes and see if that works.
FileUtils.cleanDirectory(new File("C:\\\program files\\\test\\\"));
It will take care of everything in the directory.
Just need to add Apache Commons IO jar to your path.
Related
I have a jar file 'BIO.jar' that I want to be able to use in BluJ.
I have copied the .jar file to:
/Program Files/Java/jdk1.8.0_60/jre/lib/ext/BIO.jar
and
/Program Files/Java/jre1.8.0_60/lib/ext/BIO.jar
But I still can't utilise the statement int y = BIO.getInt();
BluJ throws me a cannot find symbol - variable BIO like it always does.
So, how do I add a .jar file so I can use it in BluJ?
I would assume it's the same process for adding a .jar file to eclipse or netbeans, so, what's the process?
#Marged Thank you very much, that answered my question. For anyone wondering the instructions on how to do so are at bluej.org.
Remember to check documentation!
How can I check that I can delete a file in Java?
For example, I should be able to delete file C:/file.txt but I never will be able to delete the C:/ or Computer, or My Documents etc.
Solution described in possible duplicate does not work for me.
Removing file requires write permission of the file's parent, i.e. directory where file is stored. Directory in java is also represented by instance of class java.io.File that has method canWrite().
So, to check whether file can be deleted you should call file.getParent().canWrite().
On my Windows 7 64 bit box using NTFS and Java 7 (Oracle JDK), the only thing which worked for me reliably is
boolean canDelete = file.renameTo(file)
This is surprisingly simple and works also for folders, which have "somewhere below" an "open" or "locked" file.
Other things I tried and produced false-positives: aquire a FileLock, File#canWrite, File#setLastModified ("touch"), file.getParent().canWrite()
This is a very simple question for many of you reading this, but it's quite new for me.
Here is a screenshot for my eclipse
When i run this program i get java.io.FileNotFoundException: queries.xml (The system cannot find the file specified) i tried ../../../queries.xml but that is also not working. I really don't understand when to use ../ because it means go 1 step back in dir, and in some cases it works, can anyone explain this? Also how can I refer to queries.xml here. Thanks
Note: I might even use this code on a linux box
I assume it is compiling your code into a build or classes folder, and running it from there...
Have you tried the traditional Java way for doing this:
def query = new XmlSlurper().parse( GroovySlurping.class.getResourceAsStream( '/queries.xml' ) )
Assuming the build step is copying the xml into the build folder, I believe that should work
I don't use Eclipse though, so can't be 100% sure...
Try
file = new File("src/org/ars/groovy/queries.xml");
To check the actual working directory of eclipse you can use
File f = new File(".");
System.out.println(f.getAbsolutePath());
You could try using a property file to store the path of the xml files.
This way you can place the xml files in any location, and simply change the property file.
This will not require a change/recompilation of code.
This would mean you will only need to hardcode the path of the property file.
If you prefer not hardcoding the path of the property file, then you could pass it as an argument during startup in your server setup file. (in tomcat web.xml). Every server will have an equivalent setup file where you could specify the path of the property file.
Alternatively you could specify the path of the xml in this same file, if you don't want to use property files.
This link will show you an example of reading from property files.
http://www.zparacha.com/how-to-read-properties-file-in-java/
I have created a Java application that loads some configurations from a file conf.properties which is placed in src/ folder.
When I run this application on Windows, it works perfectly. However when I try to run it on Linux, it throws this error:
java.io.FileNotFoundException: src/conf.properties (No such file or directory)
If you've packaged your application to a jar file, which in turn contains the properties file, you should use the method below. This is the standard way when distributing Java-programs.
URL pUrl = this.getClass().getResource("/path/in/jar/to/file.properties");
Properties p = new Properties();
p.load(pUrl.openStream());
The / in the path points to the root directory in the jar file.
Instead of
String PROP_FILENAME="src/conf.properties";
use
String PROP_FILENAME="src" + File.separator + "conf.properties";
Check the API for more detail: http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html
I would also check what your current working directory is if your path to that file is relative. You just need to make a File test = new File("."); and then print that files canonical path name.
If you are referencing any other locations like user.dir or something to that effect by using System.getProperty(), you'll want to at least verify that the directory you are using as the relative root is where you think it is.
Also, as Myles noted, check the slashes used as file path separators. Although you can always use the "/" and it works.
And if you are referencing the path absolutely, you'll have trouble going between one OS and another if you do something silly like hard-code the locations.
What you want to do is check out System.getProperties() and look for file.separator. The static File.pathSeprator will also get you there.
This will allow you to build a path that is native for whatever system you're running on.
(If indeed that is the problem. Sometimes I like to get the current directory just to make sure the directory I think I'm running in is the directory I'm really running in.)
Check your permissions. If you (or rather, the user that the Java process is running under) doesn't have appropriate permissions to read the file, for example, you would get this error message.
This is a typical Windows -> Linux migration problem. What does ls -l src/conf.properties show when run from a prompt?
Additionally, check capitalisation. Windows isn't case-sensitive, so if the file was actually called e.g. CONF.properties it would still be found, whereas the two would be considered different files on Linux.
You should check the working directory of your application. Perhaps it is not the one you assume and that's why 'src' directory is not present.
An easy check for this is to try the absolute path (only for debugging!).
I would check your slashes, windows often uses '\' vs linux's '/' for file paths.
EDIT: Since your path looks fine, maybe file permissions or executing path of the app is different?
check your slashes and colons
in my case i set my PS1 to following value
PS1='\n[\e[1;32m]$SYSNAME(\u)#[\e[1;33m]\w [\e[1;36m](\d \T) [!]\e[0m]\n\$ '
i am trying to read from the env .such as system.getenv
Java was throwing exception
java.lang.IllegalArgumentException: Malformed \uxxxx encoding
Try the double slash, after doing things in JBoss I often had to refactor my code to use the double slashes
I've never seen that,internally generated?How does it work?
Can check what I meen here:
http://issues.apache.org/jira/secure/attachment/12401970/nutch_0.9_OR.patch
search "java~"
and you can see "java.old" there,what's that again?
It's probably some cruft leftover from emacs. With emacs, whenever you save a file, it saves a backup of the previous version of the file, and the backup is named with the original filename with a tilde appended to it. If this is the case (which you can easily verify by comparing file with file~), then you can safely ignore all of the files named with tildes.
Are you sure its generated from some java process? ~ in files typically means a temporary file created by editors, such as vim when you modify something.