Renaming and deleting text files

Reply

Join Date: Oct 2008
Posts: 25
Reputation: shubhang is an unknown quantity at this point 
Solved Threads: 0
shubhang shubhang is offline Offline
Light Poster

Renaming and deleting text files

 
0
  #1
Nov 20th, 2008
I have made this delete class of my address book program.
It gives an error when I use the renameTo() function.
Please look into my program.
import java.io.*;
import java.util.*;
class del
{

	private String stru,strf;
	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	public void calc()throws IOException
	{
		System.out.println("enter record to be deleted");
		stru=br.readLine();

		BufferedReader in=new BufferedReader(new FileReader("adbk.txt"));

		File fi=new File("tadbk.txt");

		PrintWriter p=new PrintWriter(new FileWriter("tadbk.txt",true));

		while((strf=in.readLine())!=null)
		{
			if(stru==strf)
			{
				continue;
			}
			else
			{
				p.println(strf);
			}
		}
		p.close();
		fi.renameTo("adbk.txt");//gives error here
		fi.delete();
	}
}
Please tell me how to go about it.
Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Renaming and deleting text files

 
2
  #2
Nov 20th, 2008
Here is part of description of this function, given by java help:
public boolean renameTo(File dest)

It asks a File as parameter, but you give String to it.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 37
Reputation: bondo is an unknown quantity at this point 
Solved Threads: 0
bondo's Avatar
bondo bondo is offline Offline
Light Poster

Re: Renaming and deleting text files

 
0
  #3
Nov 20th, 2008
http://java.sun.com/javase/6/docs/ap...(java.io.File)

Check that out. It's expecting a file, not a string. It seems like you need to create a new file with a filename of what you want to rename the file to and pass that into renameTo().
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 37
Reputation: bondo is an unknown quantity at this point 
Solved Threads: 0
bondo's Avatar
bondo bondo is offline Offline
Light Poster

Re: Renaming and deleting text files

 
1
  #4
Nov 20th, 2008
Whoops, too slow. Check out the javadocs though, it might be helpful in explaining what you need to do.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC