BGS5T deleting the file | Thales IoT Developer Community
November 5, 2014 - 3:43pm, 4549 views
Hi,
I am using FileConnection, to write to files and save data for the nex time that midlet is run. But I wan't to refresh some data and for that I need to clear everything inside the file or delete it. So I tryed this pice of code:
String filename_new = "file:///a:/users.txt";
FileConnection fc=null;
fc = (FileConnection) Connector.open(filename_new, Connector.READ_WRITE);
if(fc.exists())
{
fc.delete();
fc.close();
}
fc = (FileConnection) Connector.open(filename_new, Connector.READ_WRITE);
fc.create();
OutputStream os = fc.openDataOutputStream();
But I am getting exception: java.io.IOException: cannot delete file. Do I need to change any settings or what is the proper way to delte file?
Regards
Jure
Hi Jure,
Your above code is OK. You don't need any special settings to delete the file. This should work.
You have written that you save data for the next time the MIDlet runs - so you probably read the file before you delete it - is it possible that the file is open for reading and not closed (by another thread, FileConnection instance) when you open it again to delete it?
Regards,
Bartłomiej
Thank you, I closed the FileConnection but I totaly forgot about DataInputStream.
Regards
Jure
can you tell me how to copy files from one folder to another
HI,
You can open one file in one folder and create new file in new location. You can simply read data from that opened file and save it to new file.
Best regards
Krzysztof
HI,
i need to copy files from one folder to another.not to read data from a files.
Hello Harish,
The code you wrote tried to open a directory for writing, because you forgot to append the filename.
Please find enclosed a slightly more verbose version of your code which should help explain the process.
I tend not to "throw" errors as this is not very helpful, better to write your code to handle errors. I have added conditional stream/file closing.
If writing large files, then you would do better to use a com.sun.midp.io.BufferedOutputStream with default size of nx64KBytes, but that is probably overkill for this example.
FileConnection ois;
FileConnection oos = null;
FileConnection w1;
InputStream is = null;
OutputStream os = null;
String filename;
byte b[] = new byte[1024];
int length;
try {
w1 = (FileConnection) Connector.open("file:///A:/Demo/watch1");
if (w1.isDirectory()) {
System.out.println("w1 is a directory... retrieving file names::");
Enumeration files = w1.list();
while (files.hasMoreElements()) {
System.out.println("w1 iterating through file names::");
filename = (String) files.nextElement();
System.out.println("w1 - " + filename);
System.out.println("ois open+ file:///A:/Demo/watch1/" + filename);
ois = (FileConnection) Connector.open("file:///A:/Demo/watch1/" + filename, Connector.READ);
System.out.println("ois open-");
if (ois.exists() == false) {
System.out.println("File 'file:///A:/Demo/watch1/" + filename + "' does not exist.");
} else {
System.out.println("is open+");
is = ois.openInputStream();
System.out.println("is open-");
System.out.println("oos open+ file:///A:/Demo/finalwatch/" + filename + ".copy");
oos = (FileConnection) Connector.open("file:///A:/Demo/finalwatch/" + filename + ".copy", Connector.READ_WRITE); // finalwatch
System.out.println("oos open-");
oos.create();
if (oos.exists()) {
System.out.println("destination path found: file:///A:/Demo/finalwatch/" + filename + ".copy");
}
System.out.println("os opened+");
os = oos.openOutputStream();
System.out.println("os opened-");
System.out.println("is: reading/writing (copying) data+");
while ((length = is.read(b)) > 0) {
System.out.print(".");
os.write(b, 0, length);
}
System.out.println("");
System.out.println("is: reading/writing (copying) data-");
}
if (os != null) {os.close();}
if (oos != null) {oos.close();}
if (is != null) {is.close();}
ois.close();
}
}
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
}
Hi Mullengers,
Thank you so much for your reply sir.It solved my requirement.
.