When I run this code
String path = "E:";
File F = new File(path,"TEST.txt");
File FF = new File(path, "áéíóúñ.txt" );
F.renameTo(FF);
I get a file with this name: áéÃóúñ.txt
Can I get the rigth name somehow?
I think my .txt file is saved in UTF-8 and It still does not work, i save it again anyway. i used the syntax \uXXXX, but it doesnt work, anyway i have to read the name since a txt file, when i read the name, i can see name with characters like "áéñ.. (using scanner class)" but when used renameTo, it doesnt work.
Related
I have to create a temp file in the /tmp directory the code that I am using is adding random numbers to the filename. I have to use the name of the file in order to do something. With the random number, I am not able to use that file. The code I wrote is :
File dir = new File("/tmp");
String prefix = "temp";
String suffix = ".txt";
File tempFile = File.createTempFile(prefix, suffix, dir);
After using the file with the correct file name I also have to delete it how can I do that?
If you need to access the File again, you can store the path of the file to be accessed at another time.
You can get the absolute path file using:
tempFile.getAbsolutePath();
To answer your question about deleting the file after you are finished using it, you can either use the detete() or deleteOnExit() methods of File.
If your code needs a predictable filename, and you want that file to be cleaned up automatically when the program ends, don’t use a temp file (they have a random name - it’s just how they work) but rather just use deleteOnExit() with a regular file:
File file = new File("some/filename.ext");
file.deleteOnExit();
I am working in Netbeans IDE.
What I want to do is:
Get The directory of the Current Java Application (Ex: "F:\PadhooWorld")
Join a file name to it. (Ex: "\Somestuff.txt")
Check if that File exists (Ex: "F:\PadhooWorld\Somestuff.txt")
Do a if.. else activity
When I tam trying to Join Directory + Filename, it is throwing lots of error messages like Path cannot be converted to string etc . Searching the net the whole day, doesn't yield any simple usable solution
Please specify a very simple solution.
EDIT
I have only 2 lines of code as yet
String AppPath = System.getProperty("user.dir");
String fullPath = AppPath + "\Surabhi.txt";
The First Line resolves alright
The Second line (I tried different variations) No Luck. It is underlined in red. Error hints say stuffs like 'Path cannot be converted to string'..
I cannot RUN the code.
It sounds like you're overthinking it. You can just create a File object with the file name you want (the path to the current directory will be used by default) and then call exists() on it:
File f = new File("filename.txt");
System.out.println(f.getAbsolutePath()); //Just for debug if you want to check the path
if(f.exists()) {
//Whatever
}
Alternatively, if you want to specify the path as well as the file name:
String AppPath = System.getProperty("user.dir");
String fileName = "Surabhi.txt";
File f = new File(AppPath, fileName); //f.getAbsolutePath() will give the concatenated name
if(f.exists()) {
//Whatever
}
i read on several posts that we for deleting a file through java which has spaces in the name, i can use delete() method (Java 6). eg:
File f = new File("/mnt/test ex.txt");
f.delete();
but when I'm making a file object like this () :
StringBuilder fullFileName = "C:/Temp_Folder\week month.xlsx";
fileToRead = new File(fullFileName.toString());
fileToRead.delete();
I'm not able to do so and i get the following exception :
java.io.FileNotFoundException: "C:\Temp_Folder\week month.xlsx" (The filename, directory name, or volume label syntax is incorrect)
What am i missing here?
P.s. : I tried using quotes on the filename as well without success
fileToRead = new File('"'+fullFileName.toString()+'"');
Edit : I've edited the quotes on the stringBuilder (a type from my end). Actually the StringBuilder object is a parameter and we are appending objects to fetch the actual name. I just gave you the final declaration.
As far as week month.xlsx goes, that is the name of the file and not two different variables (which means the filename DOES have spaces in between; it could be something like
Name with spaces.xlsx
Thanks for the quick turnaround everyone.
You do NOT need a specific treatment for file names with spaces in Java -- or any other programming language with a file access API for that matter.
Do not mix Java with a command interpreter.
In your case, your File should be declared as:
new File("C:\\Temp_Folder\\name with spaces.xlsx")
and that's it.
If Java reports a FileNotFoundException then there is a problem. Unfortunately, the File API is broken and this exception can be thrown if the file exists but you cannot read it, for instance. Have a look at the complete stack trace.
Do yourself a favour: use Java 7 and the new Files API. With this API, exceptions actually make some sense -- and a delete operation will not "silently" fail either.
As to building the filename itself, you can for example use String.format():
final String filename = String.format("C:\\Temp_Folder\\%s %s.xlsx", month, week);
According to the exception:
java.io.FileNotFoundException: "C:\Temp_Folder\week month.xlsx"
You are looking for the following file:
"C:\Temp_Folder\week month.xlsx"
Note the quotes! This file does not exist.
You will have to modify your code to ensure that your file name does not include the surrounding quotes (not needed).
I.e. (Assuming java 6 here)
File file = new File("C:\\Temp_Folder\\week month.xlsx");
file.delete();
Note, the backslash is an escape character hence it is doubled in the string.
Lets say I've got a file in D: which has a filepath as:
D:\work\2012\018\08\2b558ad8-4ea4-4cb9-b645-6c9a9919ba01
at the moment the name of this file is not meaningful. It doesn't have a format. I would like to have the ability to create file object from the above filepath in Java and change its name and format before writing it into bytes. The new path could be as following:
D:\work\2012\018\08\mywork.pdf
After the changes have been made, I should still be able to gain access to the original file per normal.
I already know the type of the file and its name so there won't be a problem getting those details.
Just rename the file:
File file = new File(oldFilepath);
boolean success = file.renameTo(new File(newFilepath));
You could also just give a filename and use the same parent as your current file:
File file = new File(oldFilepath);
file.renameTo(new File(file.getParentFile(), newFilename));
It's not really clear what exactly you want; I hope this answer is useful to you.
Suppose you have a java.io.File object that represents D:\work\2012\018\08\2b558ad8-4ea4-4cb9-b645-6c9a9919ba01, and that you want to have a File object that represents D:\work\2012\018\08\mywork.pdf. You already know the new filename mywork.pdf but you want to get the directory name from the original File object. You can do that like this:
File original = new File("D:\\work\\2012\\018\\08\\2b558ad8-4ea4-4cb9-b645-6c9a9919ba01");
// Gets the File object for the directory that contains the file
File dir = original.getParentFile();
// Creates a File object for a file in the same directory with the name "mywork.pdf"
File result = new File(dir, "mywork.pdf");
I have a file which contains sql statements.
Now in this file, I want to add a word "collate" after every 'char()' and 'varchar()' in the file.
How do you do that?
Iterate through the file line by line. On each String do two replaceAll( ... ) using your Strings above. Then write each line into a new File. When done, rename the original file to some back-up name and rename the new file to the original file's name.
Edit 1
I just noticed your javascript tag. So what type of problem is this, Java or Javascript?