Is there any way to control the random digits appended to a file name when a tempFile is created ?
For eg. if I write File.createTempFile("abc",".pdf"), it creates a file with name abc12323543121.pdf. Instead of these digits, is it possible to append a timestamp ? I need this because for every file that i create, I need to append the timestamp to the file, which makes the file name quite long. So, instead of the randomly generated digits,if I could just use the timestamp, it would be really great.
It seems that the API does not directly provide this. But you can have a look into the File.createTempFile() source code to see how it is implemented, and then implement the required method yourself.
Basically, createTempFile() creates a File object with the intended file name, and then uses FileSystem.createFileExclusively() to create the file. This method returns false if the file already exists, in which case the file name is modified (by using a different random number) and the creation is retried.
You can follow the same approach, but note that FileSystem is a package private class, hence you can not use it in your own method. Use File.createNewFile() instead to create the file atomically. This method also returns false in case the file already exists, so you can use it in a similar loop like createTempFile() uses the createFileExclusively() method.
You can create your own utility method for creating temp file. Basically temp files are stored in temp directory like this :
public File createTempFile(String prefix, String suffix){
String tempDir = System.getProperty("java.io.tmpdir");
String fileName = (prefix != null ? prefix : "" ) + System.nanoTime() + (suffix != null ? suffix : "" ) ;
return new File(tempDir, fileName);
}
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();
In my TestClass I want to read the txt-file. I am always very confused how I should go about getting a reference to the txt-file though. The example I dug out of the internet suggested using a BufferedReader which requires a Path object to instantiate. I thought I'd create a File object and invoke it's .toPath(). But now how do I instantiate my File object? The least scary of its constructors require a string, but which string?
The easiest way to reference the file path within the scope of your project would be to use the System properties. Using the below value would return to you the users current working directory. Something like this would do the trick:
File file = new File (System.getProperty ("user.dir") + "/" + path_to_txt_file);
Depending on your system you may need to modify the delimiter.
Using Java, what is the most resource efficient way to pull a 'list' of files names from a folder location. At the moment I am using the code below:-
File[] files = new File(folderLocation).listFiles();
Before iterating through the file array and dumping the filenames only, into a Hash to be used by my application. So, bearing in mind that I only need the file name's is there a more memory efficient way to do this.
Edit:
I am not using Java 7
I was not actually getting a memory error but the method I'd developed was taking such a long time to run (20mins) that it is not a realistic option for the application that I am developing.
You can use (new File(folderLocation)).list() which returned String[]. Each String is path+separator+filename. You can extract filename from this Strings.
Do you need to differentiate between files and directories. If not, you can use the list() function in File to return an array of strings naming the files and directories in the directory denoted by this abstract pathname. Then, you can easily construct a HashSet with that list. For example,
String[] files = new File(folderLocation).list();
Set<String> mySet = new HashSet<String>(Arrays.asList(files));
There is also a list(FilenameFilter filter) function which accepts a file name filter and returns a list of String names. However, it doesn't allow you to filter based on file/directory.
Using Java 7+ there is:
final Path dir = Paths.get(folderLocation);
final List<String> names = new ArrayList<>();
try (
final DirectoryStream<Path> dirstream
= Files.newDirectoryStream(dir);
) {
for (final Path entry: dirstream)
list.add(entry.getFileName());
}
Also, well, File.listFiles() just doesn't work.
you can check Path
it is mentioned in javadoc
Path getFileName()
Returns the name of the file or directory denoted by this path as a Path object
I have a file with name "aaaäaa.xls"
For this, File.isFile() and File.isDirectory() is returning false? why it is returning false in Linux?
Please try the following code example
if(!pFile.exists()){
throw new FileNotFoundException();
}
boolean isDir = pFile.isDirectory();
boolean isFile = pfile.isFile();
the file is not a file
if it is not a directory and, in addition, satisfies other system-dependent criteria
if the exception is thrown, you have to check the file path.
According to the documentation:
public boolean isFile()
Returns:
true if and only if the file denoted by this abstract pathname exists and is
a normal file; false otherwise.
From this basis, your file either doesn't exist or is not a normal file.
Possible reasons of the 1st:
file doesn't exist;
file can't be accessed;
file name is mistyped;
the character encoding used in your program isn't the
same as that used when you created the file.
Possible reasons of the 2nd:
it's not a regular file.
Or it's a bug in JVM. It's also possible though unlikely. For example at once I had problems with an exclamation mark in path names - Bug 4523159.
If you want to access the file in any way, consider calling dir.listFiles() and work with its return value.
(answer is partially based on this thread)
Check the permissions of the parent directories of this file. Some of these directories may not have execute permission for the current user.
The execute bit of directory allows the affected user to enter it and access files and directories inside
I know that this question was asked five years ago, in fact, I got to it because I had the same problem, I am creating a List of all the files in a given path, like this:
File files = Paths.get(path).toFile();
List<String> filenames = Arrays.asList(files.list());
The thing is, that path contains a directory which is called testing_testing, which is being returned as part of the list.
Then when I do the following test:
for (String filename : filenames) {
if (Files.isDirectory(Paths.get(filename))) {
System.out.println(filename + " is a directory.");
} else {
if(filename.equals("testing_testing")) {
System.out.println("Is this a directory?: " + Files.isDirectory(Paths.get(filename)));
System.out.println("Does the file exists?: " + Files.exists(Paths.get(filename)));
System.out.println("Is this a regular file?: " + Files.isRegularFile(Paths.get(filename)));
System.out.println("Is this a symbolic link?: " + Files.isSymbolicLink(Paths.get(filename)));
}
}
}
It is returning false for Files.isDirectory() and for Files.exists().
Tinkering around for a bit, I noticed that I was only getting the filenames, without the full path to them, which meant that I was only passing testing_testing to Paths.get() instead of passing the full path to get to it, that's why it didn't exists and returned false for both scenarios.
I changed the code to:
Paths.get("C:\test", filename);
And now the tests return the proper values. I don't know if you've already figured this out, because it's been five years since you asked. But for anyone with the same problem, make sure that you're passing the correct path to your files, and then try the other things suggested in previous answers on this same question.
I've also had problems with file.isFile() returning false on files, presumably because the file is not "regular", as noted in other responses to this question. As a workaround, I use file.listFiles() != null, which seems to provide the functionality I need. According to the Java File API:
If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of File objects is returned.
I got same error when i was testing isFile() on a .txt file.
The problem was the file i created had something.txt with .txt on the name.
Then i renamed something.txt to something
I was really mad with myself
The character encoding used by Java in your case is different from the character encoding in the source file, so the symbol "ä" in the file name cannot be properly decoded by Java, resulting in a different file name. That's why Java cannot find the file. Therefore, the file manipulation functions over this file return "False".
As the safest way to work properly in different build environments, to avoid setting Java character encoding option and also make handling source files easier, use US-ASCII only (7-bit) characters in the source code. As for the other characters, use their Unicode numbers, e.g., instead of "ä" use "\u00e4". So, your filename would become "aaa\u00e4aa.xls".
I've had this issue several times and if everything has been tried then it might be that you have issues with pathing. Any space character is replaced by %20, and it results in an issue.
Therefore, whereas this doesn't work:
File file = new File(Objects.requireNonNull(getClass().getResource(/path/to/file).getPath());
This actually does:
File file = new File(Objects.requireNonNull(getClass().getResource(/path/to/file).getPath().replace("%20", " "));
Scenario:
I save my drawing with a file name as picture. After a while I made some changes on the file picture and save it again.
Since both file have the same name, is it possible that the new file automatically saved as picture1 without need to manually change the file name in the program? ... I means automatically add number at the end of the file name if the file have the same name?
so at the end if I made changes on the files so many times, I will have many files named as picture, picture1, picture2, picture3...
You could use the create temp file method for this, use:
as prefix the basename of your file, in this case it would be "picture"
as suffix the image type, for instance ".png"
The file created will be unique by definition.
Another way is to create a unique filename based on the current time, as in:
SimpleDateFormat fmt = new SimpleDateFormat("picture_yyyyMMdd_HHmmss.png");
String filename = fmt.format(new Date());
This would give you meaningfull filenames with regards to edit history.
Sure, if you program it so. If your desired filename exists check to see if a file with the same name, with an increasing integer starting at 1, exists. Once you find one that doesn't exist, use it as the name. Make sure to do the right thing with file extensions (you probably want file2.txt, not file.txt.2).
if filename exists
{
loop suffix from 1 to some limit
{
if filename + suffix doesn't exist
{
exit loop and use this name
}
}
}