Can't rename file using java - java

I am currently trying to append some text onto the end of a file name using java but nothing seems to be happening. I have never used java to manipulate files before but going by posts online I've come up with this...
for (File f : fullFileList)
{
System.out.println(f.getName());
if (moveToFailed(f))
{
/*
* Moved to failed successfully
*/
File newf= new File(f.getAbsolutePath() + ".processed");
f.renameTo(newf);
}
}
Can anyone help with this?
Thanks!
(The file is being moved successfully so it is entering that loop)
Update:
boolean moveToFailed(File f) // Moves file to failed directory
{
try
{
FileUtils.moveToDirectory(f, failedDirectory, true);
return true;
} catch (IOException e)
{
System.out.println("Couldn't move " + f.getName()
+ " to failed directory!\n" + e.getMessage());
return false;
}

String name = sourceFile.getName();
String dstName = name + ".processed";
File dest = new File(sourceFile.getParent(), dstName);

Related

Java application crashed due to lots of files being copied

I have an application which copies a number of files from a directory to a certain destination. The problem is that when you select a large folder it gets more intense for the app itself and then crashes. Is there any way to make it not crash? Maybe split it up into smaller parts?
This is my code:
public void startProcess(File orgDir, File destDir) {
Screen1Controller sf = new Screen1Controller();
String selectedExtension = sf.selectedExtension; // Gets selected extension from Screen1
String extensionType = sf.typeOfExtension; // Gets selected extension type from Screen1
int y = 1; // This is for searching for duplicates.. See below.
try {
File[] files = orgDir.listFiles();
for (File file : files) { // Goes through the files in the given directory
if (!file.isDirectory() && file.getName().endsWith(selectedExtension)){
File destinationPath = new File(destDir.getCanonicalPath() + "\\");
destDir = new File(destinationPath + "\\" + extensionType); // Sets the destination path
destDir.mkdir();
System.out.println("file:" + file.getCanonicalPath()); // Prints the file path
try{
String fileNameWithOutExt = file.getName().replaceFirst("[.][^.]+$", ""); // Gets the current file without the extension
File destFile = new File(destDir.getPath() + "\\" + file.getName()); // If a file of the same name exists in the dest folder
if (Files.exists(Paths.get(destFile.getPath()))) // Checks if there is a file with the same name in the folder
{
System.out.println("There is a duplicate.");
File[] destFiles = destDir.listFiles();
for (File destinationFile : destFiles) // Searches through the destination folder
{
if(destinationFile.getName().startsWith(fileNameWithOutExt)){ // Checks if the selected file has the same name as the file that's going to be moved.
y++; // Increments y by 1 to keep track of how many there are of the same/similar name
}
}
File newFile = new File(orgDir.getPath() + "\\" + fileNameWithOutExt + "." + y + selectedExtension); // Creates a new file with new name.
file.renameTo(newFile); // Renames to a unique name and moves the file to the destination folder
File destPath = new File(destDir.getPath() + "\\" + newFile.getName()); // Gets the destination path for the file
System.out.println(newFile.getCanonicalPath());
Files.copy(Paths.get(newFile.getCanonicalPath()), Paths.get(destPath.getPath())); // Renames the original file back to its original name
newFile.renameTo(new File(orgDir.getPath() + "\\" + fileNameWithOutExt + selectedExtension));
} else {
Files.copy(Paths.get(file.getPath()), Paths.get(destFile.getPath())); // Moves the file to the destination folder
}
}catch(Exception e){
e.printStackTrace();
}
} else{
startProcess(file, destDir);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am assuming your program is correct and this is purely memory issue. Increase the memory settings by running program with options -Xms 1024m -Xmx 1g, increase the values if necessary, be aware of your total available memory.
Will be nice to see a stack trace of exception to know for sure what caused the crash.

Problems with duplicate filenames when creating via widget

I've got a widget that allows the user to drag and drop an email message or a file into the widget to copy it to their file system. It's the FileExplorer project in OpenNTF, designed by people far more experienced than I am. I want to modify it to provide a new filename if the current filename already exists in the location they're dropping it on. With emails I'd hoped to be able to grab the sender and date, but I've been throwing errors when I try to access the file contents during a drag-and-drop of email.
So, my issue is actually simple. I've got the 'if' to determine if the filename is taken, but I'm overwhelmed trying to figure out how to test multiple options for the filename (like numbering then 'file1.eml', 'file2.eml', 'file3.eml'). I tried, below, inserting the word DUPLICATE, but I'm having no joy.
try {
if (source.isDirectory()) {
File dirTarget = new File(fDest.getAbsoluteFile() + File.separator + source.getName());
if (!dirTarget.exists()) {
dirTarget.mkdir();
}
copyDir(monitor, source, dirTarget);
}
if (source.isFile()) {
File dest = new File(fDest.getAbsolutePath() + File.separator + source.getName());
if (dest.getAbsolutePath().compareTo(source.getAbsolutePath()) != 0) {
copyFile(monitor, source, dest);
} else {
dest = new File(fDest.getAbsolutePath() + File.separator + "DUPLICATE" + File.separator + source.getName());
copyFile(monitor, source, dest);
}
}
} catch (IOException e) {
}
For reference, the copyFile method's parameters are
private void copyFile(IProgressMonitor monitor, File fSource, File fTarget) throws IOException
You need to construct your file name different.
File.seperator
results in / \ or : depending on your platform since it is the char separating the directory from the file.
Since you are dropping a file, you don't need check for the directory, up to you. You need a loop to test file names. To make it easy use (DUPLICATE 1) (DUPLICATE 2) etc. Something like this:
private final static String DUPLICATE = "DUPLICATE";
private void copyOut(File source, File fDest, Monitor monitor) {
try {
if (!source.exists() || !fDest.exists()) {
// one or two files missing, can't copy
// handle error here!
} else {
String destName = fDest.getAbsolutePath()+ File.separator + source.getName();
File dest = new File(destName);
if (source.isDirectory()) {
if (!dest.exists()) {
destPath.mkdirs(); // Fix missing
} else if (dest.isFile()) {
// Raise an error. Destination exists as file source is directory!!!
}
} else { // We checked for existence and dir, so it is a file
// Don't overwrite an existing file
dest = this.checkforDuplicate(dest);
}
copyFile(monitor, source, dest);
}
} catch (IOException e) {
// Error handling missing here!
}
}
private File checkforDuplicate(File dest) {
if (!dest.exists()) {
return dest;
}
int duplicateNum = 1;
while (true) {
ArrayList<String> pieces = Arrays.asList(dest.getAbsolutePath().split("."));
pieces.add(pieces.size()-1, DUPLICATE);
if (duplicateNum > 1) {
pieces.add(pieces.size()-1,Integer.toString(duplicateNum));
}
duplicateNum++;
StringBuilder newName = newStringBuilder();
for (String s : pieces) {
newName.append(s);
newName.append(".");
}
// Strip the last .
String outName = newName.substring(0, newName.length()-2);
File result = new File(outName);
if (!result.exists()) {
return result;
}
}
}
Check the code, written off memory, will contain typos. also doesn't deal with file names that don't contain a dot.

Can't delete file after being renamed (file is opened)

I am using icefaces to upload files to relative path in my web app (mywebapp/audio), then after the file is getting uploaded I rename it to save its extension as follows:
public static File changeExtToWav(FileInfo fileInfo,
StringBuffer originalFileName) {
log.debug("changeExtToWav");
int mid = fileInfo.getFile().getName().lastIndexOf(".");
String fileName = fileInfo.getFile().getName().substring(0, mid);
originalFileName.append(fileName);
log.debug("originalFileName: " + originalFileName);
String newFileName = fileName + "_" + new Date().getTime() + "."
+ "wav";
File newFile = new File(fileInfo.getFile().getParent() + "/"
+ newFileName);
log.debug("newFileName: " + newFile.getName());
fileInfo.getFile().renameTo(newFile);
return newFile;
}
after the file is getting uploaded, sometimes I want to delete it from UI button as follows:
try {
File fileToDelete = new File(filePath); // correct file path
log.debug("file exists: " + fileToDelete.exists()); // true
fileToDelete.delete();
} catch (Exception e) {
e.printStackTrace();
}
the file path is correct, and I get no exceptions, but the file is not deleted (I am using java 6 btw).
please advise how to fix this issue.
UPDATE: using the following useful method, I can get that the file is opened, any ideas how to close it ?
public String getReasonForFileDeletionFailureInPlainEnglish(File file) {
try {
if (!file.exists())
return "It doesn't exist in the first place.";
else if (file.isDirectory() && file.list().length > 0)
return "It's a directory and it's not empty.";
else
return "Somebody else has it open, we don't have write permissions, or somebody stole my disk.";
} catch (SecurityException e) {
return "We're sandboxed and don't have filesystem access.";
}
}
Well if the file is open, then there is two solutions :
You have a stream in your program open on this file. Note that afaik it's a problem on Windows, with Unix I can delete a File even if a stream is opened on it.
You have an other process using this file. So in this case you can't do anything from Java.
In the log it tells also that it can be a permission problem, are you sure you have enough privileges?
You can also use Files#delete(Path path) (jdk7) to have more details about the issue.

Why am I having nondeterministic file deletion errors when using DiskFileItem?

My upload servlet keeps throwing me an exception saying that the file that I'm trying to replace (near the end of my code) could not be deleted at (seemingly) random. I don't know what's causing this since I'm not using any streams and the file isn't open in my browser. Does anyone know what could be causing this? I'm completely clueless on this one as the code seems correct to me. This is the first time I've used DiskFileItem so I'm not sure if there are any nuances to handle there.
Keep in mind that it sometimes works, sometimes doesn't. I'm lost on that.
Problem Area:
File destination = new File(wellnessDir + File.separator + fileName + ".pdf");
System.out.println("destination file exists: " + destination.exists());
System.out.println("file to be moved exists: " + uploadedFile.exists());
if(destination.exists()){
boolean deleted = destination.delete();
if(!deleted)
throw new Exception("Could not delete file at " + destination);
}
My System outs always say that both file and destination exist. I'm trying to get the upload to overwrite the existing file.
Full code: (& pastebin)
private void uploadRequestHandler(ServletFileUpload upload, HttpServletRequest request)
{
// Handle the request
String fileName = "blank";
try{
List items = upload.parseRequest(request);
//Process the uploaded items
Iterator iter = items.iterator();
File uploadedFile = new File(getHome() + File.separator + "temp");
if(uploadedFile.exists()){
boolean tempDeleted = uploadedFile.delete();
if(!tempDeleted)
throw new Exception("Existing temp file could not be deleted.");
}
//write the file
while (iter.hasNext()) {
DiskFileItem item = (DiskFileItem) iter.next();
if(item.isFormField()){
String fieldName = item.getFieldName();
String fieldValue = item.getString();
if(fieldName.equals("fileName"))
fileName = fieldValue;
//other form values would need to be handled here, right now only need for fileName
}else{
item.write(uploadedFile);
}
}
if(fileName.equals("blank"))
throw new Exception("File name could not be parsed.");
//move file
File wellnessDir = new File(getHome() + File.separator + "medcottage" + File.separator + "wellness");
File destination = new File(wellnessDir + File.separator + fileName + ".pdf");
System.out.println("destination file exists: " + destination.exists());
System.out.println("file to be moved exists: " + uploadedFile.exists());
if(destination.exists()){
boolean deleted = destination.delete();
if(!deleted)
throw new Exception("Could not delete file at " + destination);
}
FileUtil.move(uploadedFile, new File(wellnessDir + File.separator + fileName + ".pdf"));
writeResponse();
} catch (Exception e) {
System.out.println("Error handling upload request.");
e.printStackTrace();
}
}
edit: to add, getHome() and "home" aren't really in the code, that's just to protect my home path
After much testing and aggravation, finally tried it on a different machine, same code, worked great. Has something to do with me transferring domains on my work machine and it messing with permissions.

My working directory, files, and urls

I have a convergence of needs revolving around where my data files are. The current application has all class and data files in a JAR (building using Eclipse IDE).
I've noticed there seem to be a variety of ways in which people get this information. I need the path for where my image files are (graphics). I also need it as a URL o use the toolkit call.
Toolkit tk = frame.getToolkit();
image = tk.getImage(url.toFile());
But I am having trouble with creating the URL or something. I have tried a few different methods. At this point, I keep data files next to class files in the file system. I am adding another function to what I do - strip the /bin directory off when running in debug.
// in class BwServices at init:
try {
rootDataPath = BwServices.class.getProtectionDomain()
.getCodeSource().getLocation().getPath();
rootDataPath = URLDecoder.decode(rootDataPath, "UTF-8");
fileSystemAccess = true;
}
if(rootDataPath.endsWith("/bin/"))
rootDataPath = rootDataPath.substring(0, rootDataPath.length() - 4);
Later on... I go to get images, and some calls don't work, I don't know why
I've tried two things....
// 1
String s = "file://" + rootDataPath + d.toString() + fileName;
url = frame.getClass().getResource(s);
// 2
try {
url = new URL(s);
} catch (MalformedURLException e) {
trace("getImage - can't get url: " + s);
e.printStackTrace();
}
Either of which has problems.
I have calls to get images from different places. The 'frame' is the parent frame throughout the execution, in class BwFrame.
My path comes out like this in any attempts...
rootDataPath: /C:/Users/Markgm/Documents/DEV/workspace/bwp/
So, I'm looking for ways to open either FileInputStream or URLs for the toolkit, for files relative to where the class file is (and with some trick for when in debug). For a client-side app this is what this is. Someday, I might want to run this as an Applet, but I don't know where the image files will have to be. (50x75 playing cards and a few button images) Any help or advice is appreciated!
TIA,
Mark
Toolkit tk = frame.getToolkit();
Don't use Toolkit to load images, since Java 1.4. Use ImageIO.read(String/File/URL) instead, which is a blocking method that ensures the entire image is loaded before returning.
image = tk.getImage(url.toString());
And there is an actual problem. Once you have an URL (or File) object, don't toss it away and use the String representation. Even more importantly, don't provide a String that is supposed to represent a File, but actually represents an URL!
I didn't read the rest of that mess of code snippets (in the question or follow-up 'answer'), you might look to post an SSCCE in future.
I got rid of using URLs at all, which started with the use of pasted code. The behavior of URLs changed, as did everything, when running from a class file versus running from a JAR file. So I have code here that shows some of what I ended up doing, and comments to what happens (class file or jar file), and I also got the adjustment for debug time to work (snipping off bin/ from the end).
// root path - starts with jar file or pathspec
try {
rootDataPath = BwServices.class.getProtectionDomain()
.getCodeSource().getLocation().getPath();
rootDataPath = URLDecoder.decode(rootDataPath, "UTF-8");
if(rootDataPath.endsWith("BwPlayer.jar"))
rootDataPath = rootDataPath.substring(0, rootDataPath.length() - 12);
fileSystemAccess = true;
} catch(SecurityException e) {
trace("No file system access: " + e.getMessage());
messageBox(frame, "BwServices", "Cannot access file system");
rootDataPath = "";
} catch (UnsupportedEncodingException e) {
trace("Jar URL decode exception: "+ e.getMessage());
}
if(rootDataPath.endsWith("/bin/")) // remove bin/ portion if in debug
rootDataPath = rootDataPath.substring(0, rootDataPath.length() - 4);
trace("rootDataPath: "+rootDataPath);
Above: from init() time. Below, a getImage() function, including extra debug-related lines. Note: url.getFile() doesn't directly work for 2 reasons - one is it has the file:/ when out of a jar, and the other because it won't take a full pathspec beneath its pre-stated root /.
static public Image getImage(Directory d, String fileName) {
Image image = null;
String s = d.toString() + fileName;
/*
// Note: Start with / required for this url call (w/o full pathsepc)
URL url = parentFrame.getClass().getResource("/" + s);
if(url == null) {
trace(s + " - null url ");
return null;
}
*/
String file = rootDataPath + s; // url.getFile();
// end note
String t = s = "getImage(" + file + ") ";
Toolkit tk = parentFrame.getToolkit();
if(tk == null)
s = s + "NULL tk ";
else {
try {
// full pathspec needed here
// url.getFile() returns with file:/ when running from the .jar file
// but not when running from .class file (so don't use it)
s = t = "getImage(" + file + ") ";
image = tk.getImage(file); //url.getFile());
MediaTracker media = new MediaTracker(parentFrame);
media.addImage(image, 0);
try {
media.waitForID(0);
} catch(InterruptedException e) {
s = s + e.getMessage();
}
if(image == null) {
// image = null;
s = s + "NULL image ";
} else if(image.getHeight(parentFrame) < 1) {
s = s + "invalid height";
}
} catch (SecurityException e) {
s = s + e.getMessage();
}
}
if(! s.equals(t)) {
s = "file=" + file + "\n" + s;
s = "rootDataPath=" + rootDataPath + "\n" + s;
messageBox(parentFrame, "getImage()", s);
}
return image;
}

Categories