Results 1 to 13 of 13

Thread: Files

  1. #1
    Maya is offline Member
    Join Date
    Feb 2011
    Posts
    9
    Rep Power
    0

    Default Files

    Hello,

    I am trying to create a file and then delete it. This code does create file a.txt, but won't delete it pls help me. And I would also like to know, if a file is deleted through a program, does it go to recycle bin??

    Java Code:
    import java.io.FileWriter;
    import java.io.File;
    import java.io.Writer;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class fileop {
    
    	public static void main(String[] args) {
    	Writer writer = null;
    		try {
    		String word = "hello";
    		File f1 = new File("a.txt");
    		writer = new BufferedWriter(new FileWriter(f1));
    		writer.write(word);
    		f1.delete();
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		} finally {
    		try {
    			if (writer != null) {
    				writer.close();
    			}
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    
    }
    Regards
    Maya

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    try this to confirm what you are saying:

    Java Code:
    boolean fileDeleted;
    fileDeleted = f1.delete();
    System.out.println("File deleted? " + fileDeleted);

    If the delete is successful, fileDeleted will = true; or false if unsuccessful.

  3. #3
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Files deleted through Java does not go to the Recycle Bin. Recycle Bin / Un-delete etc are OS specific; Java is cross-platform. You have to implement a native OS call to implement those.

  4. #4
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Yes, and that means jni, so good luck

  5. #5
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    Quote Originally Posted by milovan View Post
    Yes, and that means jni, so good luck
    Shhh! Don't say those scary things in a Newbie thread! :p

  6. #6
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    My aplogies, I feel guilty so this is an example, I didn't try it but..
    Scratch Where It's Itching : Weblog

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Too complicated. Just make your own "recycle bin" e.g. copy items to a specified folder "dump" before it is deleted.

  8. #8
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Quote Originally Posted by ozzyman View Post
    Too complicated. Just make your own "recycle bin" e.g. copy items to a specified folder "dump" before it is deleted.
    One cute idea

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by milovan View Post
    One cute idea

    I take it as a complement :)

  10. #10
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    301
    Rep Power
    4

    Default

    Actually it is...

  11. #11
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Lol, thanks.

  12. #12
    cultclassic is offline Member
    Join Date
    Mar 2011
    Posts
    64
    Rep Power
    0

    Default

    JNA is a little less painful than JNI. You don't have to code in another language.

    Java Native Access (JNA): Pure Java Access to Native Libraries — Java.net

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Also check out the API for File#createTempFile(...) and File#deleteOnExit()

    db

Similar Threads

  1. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  2. working with files (text files)
    By itaipee in forum New To Java
    Replies: 1
    Last Post: 02-24-2009, 11:38 AM
  3. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  4. Text and image files within jar files
    By erhart in forum Advanced Java
    Replies: 8
    Last Post: 01-19-2008, 04:43 AM
  5. how to convert mpeg files to .wav files
    By christina in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 04:14 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
  •