Results 1 to 10 of 10

Thread: Renaming a file

  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

    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();
    	}
    }
    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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,018
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    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?
    Suppose you have two files, A and B, you can't rename A to B if a B already exists; first you have to delete B before you can rename A to B.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Abu, makes sense, thanks Jos!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,018
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Abu, makes sense, thanks Jos!
    You're welcome of course; please set the thread to 'solved' (he said as a moderator ;-)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Abu should be ahh, damn autocorrect. Is there a way to make threads solved still?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,018
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by sunde887 View Post
    Abu should be ahh, damn autocorrect. Is there a way to make threads solved still?
    I guess so, you're the thread starter so there should be an option 'thread tools' (or similar) near the top of this page ... b.t.w. what on earth 'autocorrects' ahh to abu?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    382
    Rep Power
    3

    Default

    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...

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,018
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Toll View Post
    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...
    I'll go and ask; recue is on its way! Fear not!

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,018
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    I'll go and ask; recue is on its way! Fear not!
    I asked the forum gods and their answer is: unfortunately no, this feature is not present in the new release of the forum software. Oh well ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. File renaming help
    By themulator in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 05:24 AM
  2. need help with package renaming
    By Madz in forum New To Java
    Replies: 2
    Last Post: 11-25-2009, 09:39 AM
  3. Renaming a method/variable
    By gapper in forum Eclipse
    Replies: 0
    Last Post: 01-31-2008, 01:29 PM
  4. Renaming a class
    By mew in forum Eclipse
    Replies: 2
    Last Post: 12-06-2007, 11:29 PM
  5. Renaming a class in Eclipse
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-04-2007, 10:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •