Results 1 to 4 of 4
  1. #1
    amyng is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default 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
    9044
    Last edited by amyng; 05-01-2012 at 04:33 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: The code didn't delete the text file

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default 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.

  4. #4
    amyng is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Red face Re: The code didn't delete the text file

    Quote Originally Posted by wsaryada View Post
    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.
    Thank you so much, I see it now. I just try like a crazy person. You are my hero :( thx u

Similar Threads

  1. Search and Delete code help
    By nyy132 in forum New To Java
    Replies: 1
    Last Post: 04-17-2012, 10:57 PM
  2. Replies: 0
    Last Post: 02-27-2012, 10:29 PM
  3. delete a line from a text file in Java
    By ddatta8 in forum New To Java
    Replies: 2
    Last Post: 01-24-2009, 05:08 AM
  4. Replies: 3
    Last Post: 12-12-2008, 12:12 PM
  5. How to delete pre-generated code?
    By Terentius in forum NetBeans
    Replies: 0
    Last Post: 08-19-2008, 04:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •