java - Linux vs. Windows File.delete() -
i'm having bit of trouble understanding how windows handles files compared linux. i'm trying remove entry in file i'm using "database" (it's regular file). java unfortunately offers no method doing this, have copy entries except 1 want delete temporary file, delete old database, create new empty database file, copy contents of temporary file new database file, , finish off deleting temporary.
here's code function:
private void removeselecteditem(string entrytoremove) { //original database file file database = new file("catalog"); //all entries except selected written here first file temp = new file("temp"); boolean endoffileflag = false; string line = ""; try { //used reading original database file bufferedreader databasereader = new bufferedreader(new filereader(database)); //used writing newly-created temp file bufferedwriter tempwriter = new bufferedwriter(new filewriter(temp, true)); /* * read original database line line , test see if line * has course prefix , id. if does, won't written * temp file. */ while (endoffileflag == false) { line = databasereader.readline(); /* * code ugly. if possible, check needs * made in conditions of while loop. */ if (line == null) { endoffileflag = true; break; } //tests see if line removed if ( !line.contains(entrytoremove)) tempwriter.write(line + "\r\n"); } endoffileflag = false; //reset next loop databasereader.close(); //database deleted tempwriter.close(); //temp file written database.delete(); //delete create new updated 1 below database.createnewfile(); //writes new database bufferedwriter databasewriter = new bufferedwriter(new filewriter(database, true)); //reads temp file bufferedreader tempreader = new bufferedreader(new filereader(temp)); //read temp line line , add each line new catalog file while (endoffileflag == false) { line = tempreader.readline(); /* * code ugly. if possible, check needs made * in conditions of while loop. attempts far have */ if(line == null){ endoffileflag = true; break; } databasewriter.write(line + "\r\n"); } tempreader.close(); //temp deleted databasewriter.close(); //new database has been written temp.delete(); //temp file no longer needed setupinfo(); //update lists new catalog info }catch(ioexception e){ system.out.println("removeselecteditem()"); e.printstacktrace(); } }
this code (for exception of "\r\n," in linux "\n") executes under linux, in windows found when activate event handler removing entry, program add entries. after debugging found call database.delete()
isn't deleting database file, while call temp.delete()
deleting temporary file (like it's supposed to). found odd checked permissions of file , set "read/write." tried following fix found on internet:
endoffileflag = false; //reset next loop databasereader.close(); //database deleted tempwriter.close(); //temp file written database.delete(); //delete create new updated 1 below //new code database = null; system.gc();
but didn't work. can't think of else happening.
afaik, no os supports deleting part of file (except end)
you can't delete file have open have sure closed everywhere, can create temporary file , rename original. (no need copy back)
here how might write it
public static void removeline(string filename, string line) { file = new file(filename); file tmp = new file(filename + ".tmp"); printwriter pw = null; bufferedreader br = null; try { pw = new printwriter(tmp); br = new bufferedreader(new filereader(from)); boolean found = false; (string line2; (line2 = br.readline()) != null; ) if (line2.equals(line)) found = true; else pw.println(line2); pw.close(); br.close(); if (found) { from.delete(); tmp.renameto(from); } else { tmp.delete(); } } catch (ioexception e) { // log error. try { if (br != null) br.close(); } catch (ioexception ignored) { } if (pw != null) pw.close(); } }
Comments
Post a Comment