Results 1 to 10 of 10
Thread: cannot delete file.
- 08-30-2011, 12:57 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
cannot delete file.
Hi,
here is a method i use in order to extract a string from a text file named "filename":
Java Code:import java.awt.AWTException; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream.GetField; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.charset.Charset; public class example { public static void main(String[] args) throws InterruptedException, IOException, AWTException, LexicalError{ String s = fileToString("c:\\1.txt"); File f = new File("c:\\1.txt"); f.delete(); // DOESNT WORK } static String fileToString(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ fc.close(); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } } }
The problem is : i cannot delete the file "filename" after this method is called.
anyone knows why? and how can i delete it anyway?
Thanks.Last edited by urbanleg; 08-30-2011 at 05:36 PM.
- 08-30-2011, 02:03 PM #2
Can you make a small complete program that compiles, executes and shows the problem?
- 08-30-2011, 03:06 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
Java Code:public class example { public static void main(String[] args) throws InterruptedException, IOException, AWTException, LexicalError{ String s = fileToString("c:\\1.txt"); File f = new File("c:\\1.txt"); f.delete(); // DOESNT WORK } static String fileToString(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ fc.close(); return Charset.defaultCharset().decode(bb).toString(); } finally { stream.close(); } } }
- 08-30-2011, 03:52 PM #4
You left off the import statement needed to compile the code.
- 08-30-2011, 04:34 PM #5
Also, learn how to format your code consistently: Code Conventions for the Java(TM) Programming Language: Contents
db
- 08-30-2011, 05:36 PM #6
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
- 08-30-2011, 08:58 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
ok managed to handle it.
thanks anyway
- 08-30-2011, 09:01 PM #8
What did you have to do to fix it?
- 08-30-2011, 09:02 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
changed the whole method.
i created a new method which uses SCANNER instead
- 08-30-2011, 09:05 PM #10
Similar Threads
-
Not able to delete the file from the temp location after creating a zip file.
By renu in forum New To JavaReplies: 2Last Post: 05-26-2011, 05:38 AM -
Can't Delete A File
By blazecode in forum Threads and SynchronizationReplies: 4Last Post: 03-07-2011, 10:16 AM -
Delete A File
By nitinverma in forum CLDC and MIDPReplies: 13Last Post: 06-15-2010, 08:12 AM -
Delete From .txt file
By Sarinam in forum New To JavaReplies: 86Last Post: 06-28-2008, 10:17 AM -
How to delete a file
By Alpha in forum New To JavaReplies: 1Last Post: 05-26-2007, 08:11 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks