Results 1 to 5 of 5

Thread: Renaming and Deleting a file with Java

  1. #1
    Join Date
    Jul 2010
    Posts
    142

    Renaming and Deleting a file with Java

    Renaming a file using Java :

    Among the many operations that can be done on a file have a file renaming. This library of Java, Java IO, offers a method on the File class. If we remember a little philosophy class IO Java will see that the File class represents the abstraction of a file or directory, regardless of the file system that we have below. The first thing to do is instantiate the File class on the file you want to do the rename. We use the File class constructor, as we see in the following line of code :

    Code:
      File f1 = new File ("file1.txt");
    The following will instantiate a new class File with the name of the file you want to. Let's look at the line of code :

    Code:
    File f2 = new File ("file2.txt");
    With the two abstractions of files leaves us with the run method. RenameTo (File) on the first file. The method. RenameTo (File) receives as parameters a File object with the new name. Which we have called f2

    Code:
     boolean right = f1.renameTo (f2);
    The method. RenameTo (File) returns a boolean indicating whether it has been making renowned or not. To that end validate if the Boolean variable to provide information to the user.

    Code:
     if (correct)
       System.out.println ("The right has been renamed");
     else
       System.out.println ("The renowned not been able to do");

  2. #2
    Join Date
    Jul 2010
    Posts
    142

    Re: Renaming and Deleting a file with Java

    Deleting a file with Java :

    Deleting a file with Java is a fairly simple as using the File class, which we abstracted from the file manipulation, we are offered a method to carry out that task. The first thing we do is instantiate a class File. To instantiate this class we pass the constructor the name of the file on which we work.

    Code:
    File file = new File ("file.txt");
    To delete the file we invoke the method. Delete () of the File class. If you can perform the deletion of the file, this method returns true. Otherwise returns false. That is why we must control their response.

    Code:
    if (file. delete ()) 
      System . out. println ("The file has been deleted successfully"); 
      else 
      System . out. println ("File can not be deleted");

  3. #3
    Join Date
    Dec 2009
    Posts
    67

    Re: Renaming and Deleting a file with Java

    Hi all, there is no way to modify the contents of a file? I want to do is add a line to the beginning and end but not erase the initial content, this can be in java? What happens is I read it, save its contents into a buffer, then write a new file and make changes on this and copy the old. Is there a better way? Please help me as soon as possible.

  4. #4
    Join Date
    Apr 2008
    Posts
    439

    Re: Renaming and Deleting a file with Java

    I think the following code will help you :
    Code:
    FileWriter file = new FileWriter ("File.aux", true);
    The true in the constructor indicates that you add lines to the file, so do not overwrite.

  5. #5
    Join Date
    Apr 2008
    Posts
    438

    Re: Renaming and Deleting a file with Java

    Assuming you are in the same directory, and you want to overwrite f1 to f2, the below code will definitely help you :
    Code:
     File f1 = new File ("file1.txt"); 
    File f2 = new File ("file2.txt"); 
    boolean right = f1.renameTo (f2);

Similar Threads

  1. Replies: 3
    Last Post: 21-03-2012, 09:10 AM
  2. Deleting and renaming files using Batch file on Windows 7 x64
    By Aerona in forum Windows x64 Edition
    Replies: 2
    Last Post: 17-05-2011, 11:51 AM
  3. renaming Outlook.pst file
    By jay201971 in forum Windows Software
    Replies: 1
    Last Post: 21-10-2010, 04:45 AM
  4. Issue in renaming jpeg file
    By Gannon in forum Windows Software
    Replies: 5
    Last Post: 21-12-2009, 10:25 PM
  5. A Simple File and Folder renaming batch code : RenameR
    By Rudra.J in forum Tips & Tweaks
    Replies: 2
    Last Post: 05-03-2009, 02:59 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,499,558.61921 seconds with 16 queries