Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

HOw to create a text file in the given path and delete it after the use?

843838Sep 19 2005 — edited Sep 19 2005
Hi all,

I am trying to create a text file at the given path and delete the created file after the use.

I am using following code.:
--------------------------------------------------------------
import java.io.*;
// write binary data as characters
public class RanIO {
public static void main(String f[])
{
// First illustrate append
String lineSep = "\n";
try {
File temp= new File("C:/Ash","cute.txt");
boolean ch=temp.createNewFile();
if(ch)
{
System.out.println("file created");
}
else
System.out.println("file Not created");

//writing to file
/* PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter("cute.txt",true)));
p.print("Emp NO");
p.close();*/

// Open fileWriter in append mode
FileWriter fos = new FileWriter(temp, true);
BufferedWriter bw = new BufferedWriter(fos);
PrintWriter pw = new PrintWriter(fos);
double d=550;
// lineSep = System.getProperty("line.separator");
pw.print("Hello");
//pw.print( lineSep );
pw.print( d );
pw.close();

boolean det=temp.delete();

if(det)
{
System.out.println("File deleted");
}
else
System.out.println("File not deleted");

} catch (IOException ioe)
{
System.out.println( "Append IO error:" + ioe );
}
}

}
--------------------------------------------------------------

My problem:
1)
I am not able to write to the file. I want to know, where i am going wrong.
It is giving error message like
"Canot resolve Symbol: temp,"

But, FileWriter Constructor should accept a File type parameter.
here temp is a file parameter.

If i am not using file=new file();
i can't delete the file after the use. i.e if i use
PrintWriter p = new PrintWriter(new BufferedWriter(new FileWriter("cute.txt",true)));
how can i delete cute.txt after the use?

2)
I am not able to write to the text file. file is created but, a blank file.
"Hello" is not written into the text file.

can anyone help me in this regard

Thanks in advance
Ashvini
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 17 2005
Added on Sep 19 2005
7 comments
429 views