Rename a file in Camel when a duplicate is found - java

I am trying to rename a file in Camel when a duplicate is found. I have a directory that has x number of files and if another file is put in that folder I want the original to be renamed rather than overwritten.
I tried to use fileExists and tempFileName options but it does not solve the issue.
.to("file://"+decryptedFailed+"?delay=3000&tempFileName=tempName&fileExist=TryRename")
Is there anything wrong with how I am doing this?

From the Camel documentation:
TryRename [...] allows to try renaming the file from the temporary name to the actual name, without doing any exists check.
This is not what you want. Instead of TryRenameuse Move together with the moveExisting option:
.to("file://"+decryptedFailed+"?delay=3000&fileExist=Move&moveExisting=${file:name.noext}-${date:now:yyyyMMddHHmmssSSS}.${file:ext}");

Related

Renaming Files renameTo() workaround Java

The following code file.renameTo(new File(newPath)); dosen't rename all the files properly it skips over some I have even used Files.move(file.toPath(), Paths.get(newPath)); but i get an exception error in eclipse saying java.nio.file.FileAlreadyExistsException which i think is occurring because there are sets of files that when they are cut off they will have the same name is there a way to bypass this error in eclipse or fine tune the renameTo()?
I also have tried .substring(0,22);, name.replaceFirst("-2017.*", ""); and
name.substring(0, file.getName().indexOf("-2017") same result.
example:
orginal file name: 3-M-ALABAMA-SUIQUARTER2-2017200346-CD6140
Console Output: 3-M-ALABAMA-SUIQUARTER2
some of Files in folder unchanged: 3-M-ALABAMA-SUIQUARTER2-2017200346-CD6140
for(File file:filesInDir) {
String name = file.getName().substring(0, file.getName().indexOf("-2017"));
String newName = name;
System.out.println(newName); // prints prints to file
String newPath = absolutePathOne + "\\" + newName;
file.renameTo(new File(newPath));
or
Files.move(file.toPath(), Paths.get(newPath));
You can not rename a particular file to a file name that already exists within the folder you are renaming the file in. IMHO ... Even if you can, you shouldn't for a bunch of common sense reasons.
In other words, if we have a folder (directory) named: All_My_Files and in this folder we have two text files, one is named MyFile-2016.txt and the other is named MyFile-2017.txt. Now, we want to rename each of these two files so that the dash and the year (ie: -2016 or -2017) from each file name no longer exists. Essentially what you will end up trying to do is have both file names be MyFile.txt which is not allowed. Your first rename will be fine since on the first go at it there is no file within the folder named MyFile.txt but once the second rename attempt is done on the second file name it's simply going to fail since the name MyFile.txt already exists within that folder which was done from the first rename attempt. This not a code problem, this is an issue with the local file system. Those are the rules of the local file system (No file can have the same name within the same folder). Look at the file names you are going to rename, are there any that will actually create the very same file name once you remove the unwanted text? If there are then those will fail to rename.
The same applies to Moving files. You can not move a file to a folder (directory) that already contains a file with the very same name. The file system rule above applies. You can however overwrite the existing duplicate file name within the destination path if it exists during a move if you tell the Files.move() method to do so:
Files.move(sourcePathAndFileName, destinationPathAndFileName,
StandardCopyOption.REPLACE_EXISTING);
You will need to import:
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
Keep in mind though, before you blatantly overwrite an existing file you better make pretty sure that this is what you want to do. Prompting the User to carry out an overwrite would be a normal course of action but doesn't necessarily need to be the case for specific in house operations.

Update property using Properties

I want to add new property to an existing file. Whenever, I add the new property, the entire file gets overwritten. Is there a way to update the file and not overwrite property.
FileOutputStream fo = new FileOutputStream(PROPERTIES_FILE);
Properties pr = new Properties();
pr.setProperty("Key1", "KeyValue");
try {
pr.store(fo, " Comments");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
(1) Now if I want to add a new property called Key2 and the set a value KeyValue2. Is it possible ?
(2) Also when I deploy in tomcat, only when I give the absolute path, the file is getting updated. Is there a way to find the file location at runtime. Because when I run test case, the file will be present locally and the path will be different.
(3) Is there a way to leverage classpath in anyway for this.
Thanks in advance!
**I am writing it down since I did not find clear answer for the problem mentioned **
(1) Now if I want to add a new property called Key2 and the set a value KeyValue2. Is it possible ?
**Yes it is possible. The key to understanding here is that, the properties object will be store on calling 'store' api. Here appending does not happens. The file will be overwritten by the contents of the properties object. THE API DOES NOT SUPPORT APPENDING IT.
The solution this problem will be to :
1) Load the properties from the same file
2) Update the new property or existing property
3) then store using the output stream
IN THIS WAY THE CONTENT OF THE FILE WILL NOT BE LOST**
(2) Also when I deploy in tomcat, only when I give the absolute path, the file is getting updated. Is there a way to find the file location at runtime. Because when I run test case, the file will be present locally and the path will be different.
** There are two ways to do it
1) Make sure that the file is present in the classpath. If present in the classpath, we need not give absolute path before the filename
2) Provide another class which set the path. In this way, the path can be set when running the testcases. (TESTNG/JUNIT)**
(3) Is there a way to leverage classpath in anyway for this.
** Already covered above **
Hope this helps

How to create a Path and a File that does not Exist in Java

This is the problem I have: If part or all of the path does not already exist, the server should create additional directories as necessary in the hierarchy and then create a new file as above.
Files.createDirectories(path);
That's what I am currently using, but it does not create the end file. For example is the path="/hello/test.html" it will create a directory called "hello" and one called "test.html", I want the test.html to be a file. How can I do that?
This is what I did to solve this "problem" or misuse of the libraries.
Files.createDirectories(path.getParent());
Files.createFile(path);
The first line will get the parent directory, so lets say this is what I want to create "/a/b/c/hello.txt", the parent directory will be "/a/b/c/".
The second like will create the file within that directory.
Have you looked at the javadoc? createDirectories only creates... directories. If you're intent on using Files.createDirectories, parse off the file name, call createDirectories passing only the path portion, then create a new file passing the entire path. Otherwise this is a better approach.
Files.createDirectories(path.substring(0, path.lastIndexOf(File.separator)+1));
File yourFile = new File(path);
you can parse the 'path' variable to isolate the file and the directory using delimiter as '/', and do File file = new File(parsedPath); This would work only when you know that you ALWAYS pass the file name at the end of it.
If you know when you are a) creating a directory b) creating a directory and file, you can pass the boolean variable that would describe if file needs to be created or not.

Using File to create directory which contains periods

File testDir = new File("C:\temp\test");
testDir.createNewFile();
As I understand it, the above will create a directory called test in the directory c:\temp
File testDir = new File("C:\temp\test.dir");
testDir.createNewFile();
As I understand it, the above will create a file called test.dir in the directory c:\temp
What should I be doing to the code above if I wish for test.dir to actually be a directory?
No, the first one will create a regular file - after all, that's what you asked it to do:
Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.
Nothing there says it will create a directory. You'll want to escape the backslashes though, or it's trying to find C:<tab>emp<tab>est
If you want to create a directory, use File.mkdir or File.mkdirs(). You'll still need to escape the backslashes:
File testDir = new File("C:\\temp\\test.dir");
bool created = testDir.mkdir();
(Use mkdirs to create parent directories as well.) The return value tells you whether or not it actually created a directory.
That's not true.
File.createFile() will create a file.
File.mkdir() creates a directory.
http://download.oracle.com/javase/6/docs/api/java/io/File.html
File testDir = new File("C:\temp\test");
testDir.createNewFile();
As I understand it, the above will
create a directory called test in the
directory c:\temp
Wrong - it will create file called "test". Files do not have to have a "filename extension".
To create a directory:
testDir.mkdir();
BTW, this kind of question is most easily and quickly answered by looking at the API doc. Do yourself a favor and get familiar with it.

jface.preference.FileFieldEditor can't specify a new file

I'm setting up a series of preferences in my Eclipse (3.5.2) application and I'm having a problem with the FileFieldEditor. I want to allow the user to specify a log file to print output to. Often, this will be a new file. But when I use the file select dialog with FileFieldEditor, it complains that the file doesn't exists ("Value must be an existing file"). Is there a way, without extending the FileFieldEditor class, to suppress this error and have Java create that file if it doesn't exist? Thanks!
When I look the source code of org.eclipse.jface.preference.FileFieldEditor, the only solution would be to extend it and write your own version of a FileFieldEditor, with:
an overwritten changePressed() method in order to keep the file path even if the file does not exists
an overwritten checkState() method in order to avoid that error message.
So I do not see a way to avoid that FileFieldEditor extension here.

Categories