File tempFile = new File(fw.getAbsolutePath() + ".tmp");
        BufferedReader br = new BufferedReader(new FileReader(fw));
     	PrintWriter p = new PrintWriter(new FileWriter(tempFile));
     	
		   String serData=(String)loadService().elementAt(i);
		   StringTokenizer st = new StringTokenizer(serData, ":");
		   String code=st.nextToken();
		   String name=st.nextToken();
			 if (!name.trim().equals(deleteString)) {

          		p.println(st);
         		 p.flush();
        		}
      
      		p.close();
      		br.close();
      		
		if (!fw.delete()) {
			
        System.out.println("Could not delete file");
        return;
      }
      
      //Rename the new file to the filename the original file had.
      if (!tempFile.renameTo(fw)){
      	
        System.out.println("Could not rename file");
      
   		 }

I want to delete the file and rewrite is but can't. Can u tell me what is wrong with it??

Can you please provide more information about your calss.

Can you please provide more information about your calss.

import java.io.*;
import java.util.Scanner;
import java.util.Vector;
import java.util.*;


public class Driver {
	public static void main (String args[])throws IOException {
		tvProgramme programme = new tvProgramme("0001", "my", "Japanese Comedy Drama series about Yakuza members", "Japan", "27/11/2002", "18SG", "Comedy");
		
			File fw = new File("test.txt");
			PrintWriter pw = new PrintWriter(fw);
			
		System.out.println("CURRENT INFO");
		System.out.println(programme.printInfo());
		
		programme.setStatus();
		
		System.out.println("AFTER MOD INFO");
		System.out.println(programme.printInfo());
		
		try {
			FileWriter f = new FileWriter("test.txt");
			PrintWriter p = new PrintWriter(f,true);
			
			pw.write(programme.printInfo());
			System.out.println("SUCCESS WRITING FILE");
			pw.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Vector<tvProgramme> programlist = new Vector<tvProgramme>();
		programlist.add(programme);
		
		Scanner keyboard = new Scanner(System.in);
		
		System.out.println("\n\nADDING NEW PROGRAM...");
		System.out.println("Code: ");
		String newCode = keyboard.nextLine();
		System.out.println("Title: ");
		String newTitle = keyboard.nextLine();
		System.out.println("Description: ");
		String newDescription = keyboard.nextLine();
		System.out.println("Content Origin: ");
		String newContentOrigin = keyboard.nextLine();
		System.out.println("Creation Date: ");
		String newDate = keyboard.nextLine();
		System.out.println("Viewer Status: ");
		String newViewStatus = keyboard.nextLine();
		System.out.println("Type: ");
		String newType = keyboard.nextLine();
		pw = new PrintWriter(new FileWriter("test.txt",true));
		tvProgramme newProgram = new tvProgramme(newCode, newTitle, newDescription, newContentOrigin, newDate, newViewStatus, newType);
		programlist.add(newProgram);
		pw.write("\n"+newProgram.printInfo());
		pw.close();
	
		
		System.out.println("\n\nCHECKING PROGRAMS ROUND 1");
		for(int i = 0; i < programlist.size(); i++) {
			System.out.println(programlist.get(i).printInfo());
			System.out.println("\n");
		}
		
		System.out.println("\n\nDELETING PROGRAM");
		System.out.println("Type in the title to delete: ");
		String deleteString = keyboard.nextLine();
	
		for(int i = 0; i < programlist.size(); i++) {
			if(programlist.get(i).getTitle().equalsIgnoreCase(deleteString)) {
					programlist.remove(i);
					
		File tempFile = new File(fw.getAbsolutePath() + ".tmp");
        BufferedReader br = new BufferedReader(new FileReader(fw));
     	PrintWriter p = new PrintWriter(new FileWriter(tempFile));
     	
		   String serData=(String)loadService().elementAt(i);
		   StringTokenizer st = new StringTokenizer(serData, ":");
		   String code=st.nextToken();
		   String name=st.nextToken();
			 if (!name.trim().equals(deleteString)) {

          		p.println(st);
         		 p.flush();
        		}
      
      		p.close();
      		br.close();
      		
		if (!fw.delete()) {
			
        System.out.println("Could not delete file");
        return;
      }
      
      //Rename the new file to the filename the original file had.
      if (!tempFile.renameTo(fw)){
      	
        System.out.println("Could not rename file");
      
   		 }
		}

	System.out.println("\n\nCHECKING PROGRAMS ROUND 2");
		for( i = 0; i < programlist.size(); i++) {
			System.out.println(programlist.get(i).printInfo());
			System.out.println("\n");
		}
	
		}
	}
	public static Vector loadService(){
    	Vector v = new Vector();
    	String inputLine;
    	try{
    		File inFile = new File("test.txt");
    		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile)));
    		while ((inputLine = br.readLine()) != null){
    			v.addElement(inputLine.trim());
    		}
    		br.close();
    	}
    	catch (FileNotFoundException e){
    		System.out.println("Oops!file not found!");

    	}catch (IOException e){

    	}
    	return (v);


    }
}

Thank u for helping me bro.

Well take this change:

[B]//try {
           // FileWriter f = new FileWriter("test.txt");
           // PrintWriter p = new PrintWriter(f, true);[/B]

            pw.write(programme.printInfo());
            System.out.println("SUCCESS WRITING FILE");
            pw.close();

          [B]  /*} catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }*/[/B]

hope it helps.

Thank you bro. It's work. I'm continuing my program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.