Results 1 to 2 of 2
Thread: Trying to make delete work.
- 05-01-2012, 04:16 AM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
Trying to make delete work.
The delete part is not working. It supposes to delete all the temporary file that was created to sort numbers. Can you help me fix it ?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 = "temperaryfile"; 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(); // if (!success) // throw new IllegalArgumentException("Delete: deletion failed"); } 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; } }
Thank you.
The file integers.txt includes:
120
12
7483
72
47
9
5385
83
75
621
947
273
946
725
745
7341
88
5
825
7
Note: I just want to delete the temporaryfile (text file).
-
Re: Trying to make delete work.
Duplicate of a previous post. Closing.
Similar Threads
-
f.delete() and f.renameTo() does not work
By doobybug in forum New To JavaReplies: 6Last Post: 08-08-2011, 11:48 PM -
can't make ui:repeat work
By kaghamdi in forum JavaServer Faces (JSF)Replies: 6Last Post: 12-31-2010, 11:43 AM -
Make it work !
By PhQ in forum New To JavaReplies: 6Last Post: 09-20-2010, 08:22 AM -
help~ delete method cant work
By reeveliew in forum New To JavaReplies: 4Last Post: 05-07-2010, 02:24 AM -
How to make delete particular extension file from a directory
By Java Tip in forum java.ioReplies: 0Last Post: 04-05-2008, 10:13 AM


LinkBack URL
About LinkBacks

Bookmarks