Results 1 to 4 of 4
- 05-01-2012, 02:15 AM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
The code didn't delete the text file
I have this program and on the part deleting, it didnt delete the temporary files. It done well the rest but just dont delete the temporaryfile.
Can you guys help me to fix it? Thank you :(
/////////////
Java Code:import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class ExternalSort { static int Y = calculateFileLength(); // size of the file in disk static int X = 10; // max items the memory buffer can hold public static void externalSort(String fileName) { String tf = "temperoryfile"; int[] buffer = new int[X < Y ? X : Y]; try { FileReader fr = new FileReader(fileName); BufferedReader br = new BufferedReader(fr); int slices = (int) Math.ceil((double) Y/X); int i, j; i = j = 0; // Iterate through the elements in the file for (i = 0; i < slices; i++) { // Read M-element chunk at a time from the file for (j = 0; j < (X < Y ? X : Y); j++) { String t = br.readLine(); if (t != null) buffer[j] = Integer.parseInt(t); else break; } // Sort X elements Arrays.sort(buffer); // Write the sorted numbers to temp file FileWriter fw = new FileWriter(tf + Integer.toString(i) + ".txt"); PrintWriter pw = new PrintWriter(fw); for (int k = 0; k < j; k++) pw.println(buffer[k]); pw.close(); //pw.delete(); fw.close(); } br.close(); fr.close(); // Now open each file and merge them, then write back to disk int[] topNums = new int[slices]; BufferedReader[] brs = new BufferedReader[slices]; for (i = 0; i < slices; i++) { brs[i] = new BufferedReader(new FileReader(tf + Integer.toString(i) + ".txt")); String t = brs[i].readLine(); if (t != null) topNums[i] = Integer.parseInt(t); else topNums[i] = Integer.MAX_VALUE; } FileWriter fw = new FileWriter("sorted.txt"); PrintWriter pw = new PrintWriter(fw); for (i = 0; i < slices; i++) { String file = tf + Integer.toString(i) + ".txt"; // A File object to represent the filename File f = new File(file); // Make sure the file or directory exists and isn't write protected if (!f.exists()) throw new IllegalArgumentException( "Delete: no such file or directory: " + file); if (!f.canWrite()) throw new IllegalArgumentException("Delete: write protected: " + file); // If it is a directory, make sure it is empty if (f.isDirectory()) { String[] files = f.list(); if (files.length > 0) throw new IllegalArgumentException( "Delete: directory not empty: " + file); } // Attempt to delete it boolean success = f.delete(); } for (i = 0; i < Y; i++) { int min = topNums[0]; int minFile = 0; for (j = 0; j < slices; j++) { if (min > topNums[j]) { min = topNums[j]; minFile = j; } } pw.println(min); String t = brs[minFile].readLine(); if (t != null) topNums[minFile] = Integer.parseInt(t); else topNums[minFile] = Integer.MAX_VALUE; } for (i = 0; i < slices; i++) brs[i].close(); pw.close(); fw.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { String fileName = "integers.txt"; externalSort(fileName); } public static int calculateFileLength() { int count = 0; String filename = "integers.txt"; Scanner inputStream = null; try { inputStream = new Scanner( new File(filename)); } catch(FileNotFoundException e) { System.out.println("Error openning the file" + filename); System.exit(0); } while(inputStream.hasNextLine()) { inputStream.nextLine(); count++; } inputStream.close(); return count; } }
The delete part in the program is
///////////
Java Code:PrintWriter pw = new PrintWriter(fw); for (i = 0; i < slices; i++) { String file = tf + Integer.toString(i) + ".txt"; // A File object to represent the filename File f = new File(file); // Make sure the file or directory exists and isn't write protected if (!f.exists()) throw new IllegalArgumentException( "Delete: no such file or directory: " + file); if (!f.canWrite()) throw new IllegalArgumentException("Delete: write protected: " + file); // If it is a directory, make sure it is empty if (f.isDirectory()) { String[] files = f.list(); if (files.length > 0) throw new IllegalArgumentException( "Delete: directory not empty: " + file); } // Attempt to delete it boolean success = f.delete(); }
I cant attach the text file, and here is inside of my text file
The integers text file :
120
12
7483
72
47
900
521
833
901
643
625
5558
00
15
648
82
540
412
741
9044Last edited by amyng; 05-01-2012 at 05:33 AM.
- 05-01-2012, 05:30 AM #2
Re: The code didn't delete the text file
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 05-01-2012, 07:09 AM #3
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 762
- Rep Power
- 14
Re: The code didn't delete the text file
I think the file can't be deleted because your program still using it. In line 65 you create a BufferedReader to read this file. And then in line 101 you tried to deleted it, but at this point the file is still opened by the BufferedReader, so you can't delete it. You close the BufferedReader object at line 129.
Make sure that the order of your program execution is correct. The used resources such as file must be closed before the delete method can delete your file.Website: Learn Java by Examples
- 05-01-2012, 07:37 AM #4
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Search and Delete code help
By nyy132 in forum New To JavaReplies: 1Last Post: 04-17-2012, 11:57 PM -
class contains constructor with one variable, driver code didn't work, ask for help!
By myloveca in forum New To JavaReplies: 0Last Post: 02-27-2012, 11:29 PM -
delete a line from a text file in Java
By ddatta8 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:08 AM -
a simple code (cldc or midp) to write a text file ouside of the application package
By sina in forum CLDC and MIDPReplies: 3Last Post: 12-12-2008, 01:12 PM -
How to delete pre-generated code?
By Terentius in forum NetBeansReplies: 0Last Post: 08-19-2008, 05:02 PM
Bookmarks