Results 1 to 10 of 10
Thread: Renaming a file
- 06-13-2011, 06:31 PM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Renaming a file
I'm having some trouble renaming a file. Seems simple enough, close stream, then use file1.renameTo(file1), but I'm getting consistent failures. Perhaps it's something small but I just can't quite figure this one out.
Here is the relevant code, the goal is to open a file, read a line, convert it, and write a changed line.
I get no errors, it simply reports "Failed rename" every time. I believe the streams are closed properly because when I switch around the rename and delete if blocks, the delete occurs.Java Code:import java.io.*; public class EncryptFile{ private final Encryption encryptScheme; private final File file; public EncryptFile(Encryption encryptScheme, File file){ this.encryptScheme = encryptScheme; this.file = file; } public boolean encryptFile(){ File temp = new File("temp.txt"); FileReader fr = null; FileWriter fw = null; BufferedReader br = null; PrintWriter pw = null; int count = 0; try{ fr = new FileReader(file); br = new BufferedReader(fr); fw = new FileWriter(temp); pw = new PrintWriter(fw); String line; while((line = br.readLine()) != null){ if(count == 0){ if(line.equals("Encrypted using " + encryptScheme.toString())) throw new IllegalStateException("File already encrypted"); count++; pw.println("Encrypted using " + encryptScheme.toString()); } line = encryptScheme.encrypt(line); pw.println(line); } } catch(FileNotFoundException fnfe){ fnfe.printStackTrace(); } catch(IOException ioe){ ioe.printStackTrace(); } finally{ try{ if(fr != null) fr.close(); if(fw != null) fw.close(); } catch(IOException ioe){ ioe.printStackTrace(); } } if(!temp.renameTo(file)){ System.out.println("Failed rename"); return false; } if(!file.delete()){ System.out.println("Failed deletion"); return false; } return true; } public boolean decryptFile(){return true;} public static void main(String[] args){ /* if(args.length != 2) System.out.println("Proper usage is: \"java EncryptFile [flags] file\""); */ File file = new File("test.txt"); EncryptFile ef = new EncryptFile(new CaesarCipher(13), file); ef.encryptFile(); } }
- 06-13-2011, 06:33 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Ugh, switching around the blocks now works as expected. Thread solved, I apologize, feel free to delete if necessary, however; if you leave it for others in the future, the solution seems to be deleting the file, than renaming it. Perhaps before the file was deleted it was still open on the os?
- 06-13-2011, 07:33 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 06-13-2011, 07:55 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Abu, makes sense, thanks Jos!
- 06-13-2011, 08:17 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 06-13-2011, 08:21 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Abu should be ahh, damn autocorrect. Is there a way to make threads solved still?
- 06-13-2011, 08:24 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-13-2011, 08:53 PM #8
Since the forum migration, I haven't been able to mark either of my two threads as solved, to be honest. Nor do I see any solved threads anymore, so I think that function is AWOL at the moment...
- 06-13-2011, 09:04 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 06-14-2011, 08:23 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
File renaming help
By themulator in forum New To JavaReplies: 3Last Post: 04-01-2011, 05:24 AM -
need help with package renaming
By Madz in forum New To JavaReplies: 2Last Post: 11-25-2009, 09:39 AM -
Renaming a method/variable
By gapper in forum EclipseReplies: 0Last Post: 01-31-2008, 01:29 PM -
Renaming a class
By mew in forum EclipseReplies: 2Last Post: 12-06-2007, 11:29 PM -
Renaming a class in Eclipse
By Java Tip in forum Java TipReplies: 0Last Post: 12-04-2007, 10:54 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks