aspose file tools*
The moose likes I/O and Streams and the fly likes Delete a File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Delete a File" Watch "Delete a File" New topic
Author

Delete a File

Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Hi All,
I am trying to delete a file, using file.delete(). But I am not able to delete the file. Here I have attached the method which is trying to delete a file. Please let me know, if anybody knows.
Thanks in advance,
Shanmugam.
====================================================================
public String createMapMethod(BufferedReader in, boolean methodFlag, String attrFile, String objFile)
{
String methodInfo="";
PrintWriter localOut;
String inLine="";
String outLine="";
String newMethod="";

String objectID="";
String tempObjID = "";
String mapMethodName="";
String oneTab = "\t";
String comment = "";
String attributeFile="";
String objectFile="";
PrintWriter outbuf =null;
String methodContent="";
String methodHeader="";
String pkgInfo="";
File f=null;
String newLine = System.getProperty("line.separator");

boolean splFlag = false; // this is to read the content of all the entries in buildClaimsAttributeMap

Calendar calendar = Calendar.getInstance();

int day = calendar.get(Calendar.DATE);
int mon = calendar.get(Calendar.MONTH);
int year = calendar.get(Calendar.YEAR);

System.out.println(day+"-"+mon+"-"+year);


StringTokenizer attr = new StringTokenizer(attrFile,".");
StringTokenizer obj = new StringTokenizer(objFile,".");

String mm = mon+"";
String dd = day+"";

if (mm.length()<2)
mm = "0"+mm;

if (dd.length() < 2)
dd = "0"+dd;

attributeFile = attr.nextToken()+"_"+mm+dd+year+".java";
objectFile = obj.nextToken()+"_"+mm+dd+year+".java";

System.out.println("newMehtod : "+newMethod);
System.out.println("AttrFile : "+attributeFile);
System.out.println("objFile : "+objectFile);

int gCount = 0;
try
{
f = new File(attributeFile);
f.createNewFile();
outbuf = new PrintWriter(new BufferedWriter(new FileWriter(attributeFile)));

while(true)
{
inLine="";
outLine="";
inLine = in.readLine().toString().trim();

System.out.println("InLine : "+inLine);
gCount++;
if(inLine.equals("public static void buildClaimsAttributeMap ( ) {"))
{
//Special Flag is to identify only the buildClaimsAttributeMap() method members
splFlag=true;

//Extract ObjectID
StringTokenizer stz = new StringTokenizer(ksFileName,"\\");
System.out.println("Inside condition 1");
System.out.println("Line : "+inLine);

while(stz.hasMoreTokens())
tempObjID = stz.nextToken().toUpperCase();
objectID = tempObjID.substring(1,5);
mapMethodName = "map"+objectID+"();";

StringTokenizer kst = new StringTokenizer(ksObjName,".");
String myObjName = kst.nextToken().toString().trim();

if(pkgCheckStr.equals("com.twentyfirst."))
{
comment="//com.twentyfirst.pc.claims."+myObjName+".class";
}
else if(pkgCheckStr.equals("csc.fs."))
{
comment="//csc.fs.pc.claims."+myObjName+".class";
}

//methodFlag tells that whether the map method already exists in the Map class or not
// true - exits
// false - not exixts
newMethod="";
methodHeader ="";
if(methodFlag == true)
{
methodHeader = oneTab+"map"+objectID+"_new()";
}
else
{
methodHeader = oneTab+"map"+objectID+"()";
}
newMethod = methodHeader+";"+oneTab+comment;
outLine=inLine+newLine+newLine+oneTab+newMethod+newLine;
outbuf.write(outLine);
}
else if(splFlag)
{
if(inLine.equals("}"))
{
splFlag=false;

StringTokenizer kst = new StringTokenizer(ksObjName,".");
String myObjName = kst.nextToken().toString().trim();
String className = "";
if(pkgCheckStr.equals("com.twentyfirst."))
{
className="com.twentyfirst.pc.claims."+myObjName+".class";
}
else if(pkgCheckStr.equals("csc.fs."))
{
className="csc.fs.pc.claims."+myObjName+".class";
}

int vectorSize = ATTRIBUTE_VECTOR.size();
KsegAttribute kseg = new KsegAttribute();
String attr_name = "";
String tempContent="";

methodContent="public static void "+methodHeader+newLine+"{";

for(int i=0;i<vectorSize;i++)
{
Object object = ATTRIBUTE_VECTOR.elementAt(i);
kseg = (KsegAttribute)object;

attr_name="";
attr_name = kseg.getFieldName();

tempContent=tempContent+newLine+oneTab+"map(\""+objectID+"\", "+(i+1)+", "+className+", \""+attr_name+"\");";
}
System.out.println("TEMP CONTENT : "+tempContent);
methodContent = inLine+newLine+newLine+methodContent+tempContent+newLine+newLine+oneTab+"return;"+newLine+"}"+newLine;
outbuf.write(methodContent);

}
else
{
outLine=oneTab+inLine+newLine;
outbuf.write(outLine);
}
}
else
{
if((gCount == 1) || inLine.equals("}") || inLine.startsWith("public") || inLine.equals("{") || inLine.startsWith("import"))
outLine=outLine+inLine+newLine;
else
outLine=outLine+oneTab+inLine+newLine;
outbuf.write(outLine);
}
}

}
catch(FileNotFoundException fe)
{
System.out.println("createMapMethod : FileNotFoundException"+fe);
}
catch(IOException ioe)
{
System.out.println("createMapMethod : IOException"+ioe);
}
catch(Exception e)
{
System.out.println("createMapMethod : Exception"+e);
}
outbuf.flush();
outbuf.close();

try
{
File f2 = new File(attrFile);
if(f2.exists())
{
System.out.println("attrFile exists...");
boolean del = f2.delete();
boolean exists = f2.exists();
System.out.println("Exists flag : "+exists+" delete "+del);
}
File f3 = new File(attributeFile);
boolean exists = f3.exists();
System.out.println("Exists flag1 : "+exists);
f3.renameTo(new File("ClaimsAttributeMap_401_21st"));
}
catch(Exception fne)
{
System.out.println("createMapMethod : Exception2"+fne);
}
return methodInfo;
}


SJCP 1.4<br />SCWCD 1.4<br />SCEA
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

This is an awful lot of code to show, and you didn't tell us what your actual observations are -- you just say "I am not able to delete the file." Does this mean that the code prints "file exists" twice? If so, then there are any number of possible explanations: on some operating systems, you may lack appropriate permissions to delete a file; for example, a plain user can't delete /etc/passwd on a UNIX system. On other OSs, you can't delete files that are open for reading or writing by another process (Windows works this way.)
If the code doesn't print "file exists" at all, well, then the filename is probably malformed.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Delete a File
 
Similar Threads
translate to pig latin!? completely lost....
Need help!
NoClassDefFoundError
NoClassDefFoundError
files are not beened deleted and renamed