943,704 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 691
  • Java RSS
Nov 20th, 2008
0

Renaming and deleting text files

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
shubhang is offline Offline
25 posts
since Oct 2008
Nov 20th, 2008
2

Re: Renaming and deleting text files

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.
Reputation Points: 293
Solved Threads: 82
Posting Whiz
Antenka is offline Offline
361 posts
since Nov 2008
Nov 20th, 2008
0

Re: Renaming and deleting text files

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().
Reputation Points: 11
Solved Threads: 0
Light Poster
bondo is offline Offline
43 posts
since Apr 2007
Nov 20th, 2008
1

Re: Renaming and deleting text files

Whoops, too slow. Check out the javadocs though, it might be helpful in explaining what you need to do.
Reputation Points: 11
Solved Threads: 0
Light Poster
bondo is offline Offline
43 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: File ouput formatting
Next Thread in Java Forum Timeline: binary search tree question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC