JAVA Help : Append file and remove last character -


am still new java, had 1 semester of it. have first internship , isn't programming internship, general internship since first semester. boss not know java, nor in building. knew had basic programming experience , told me take stab @ problem having. has report saved , last line, last character of report character turn symbol, , need remove because giving problems on website. not sure if on right track, @ point doing trial , error. please :d

 public class removechar {      public static void main(string[] args) throws filenotfoundexception {      // variables , stuff     scanner keyscan = new scanner(system.in);     jframe frameone = new jframe ("file name");     scanner filescan = new scanner(system.in);     string filename;      // ask user file name     system.out.print("what file full file name? ");     filename = filescan.nextline();      // add .txt if user forgets put in prompt     if (!filename.contains(".txt"))         filename += ".txt";      //test see if file exists     file myfile = new file(filename);     if(!myfile.exists()){         system.out.println(filename + " not exist. ");         system.exit(0);     }      fwriter = new filewriter("config/lastwindow.txt", true);     /*while(filename.hasnext()){      }     file      bufferedreader inputfile = new bufferedreader(new filereader("c:\\" +        filescan));     //scanner reader = new scanner (inputfile);     */      }  } 

how big files? if not large, can read entire file string , chop off last character:

//set delimiter end-of-string anchor \z can read //file in 1 call next() //from: http://stackoverflow.com/a/3403112/263004  string content = new scanner(new file("filename")).usedelimiter("\\z").next(); string withoutlastcharacter = content.substring(0, content.length - 1); 

then need write withoutlastcharacter out file.

otherwise need read in original file line line , write out temporary file, , copy file on original one. however, if on last line, chop off last character. here's code should give idea of basic logic:

while(scanner.hasnextline()) {     string line = scanner.nextline();      //if last line chop off last character.     if(!scanner.hasnextline()) {         line = line.substring(0, line.length - 1);     }      //write line out temporary file     ... } 

you mentioned doesn't have java. if you're on linux or mac, can sed:

sed -i '$s/.$//' <filename> 

this delete last character of last line of file.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -