debugging - Java : the first letter of each line of a file dissapair on read -


i'm writing, training purpose, class used read/write/modify text file.

my problem 1 of function (openfile), purpose convert text file content arraylist seem bugged : fist character of each line disappear.

here java file, test script, , test script result:

textfile.java

/**  *   */ package files;  import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.file; import java.io.filereader; import java.io.filewriter; import java.io.ioexception; import java.io.printwriter; import java.util.arraylist;  /**  * @author erwan georget <egeorget@openmailbox.org>  * @licence gplv3  *  */ public class textfile {      //-----------------------------------données------------------------------------      arraylist<string> arraystring;      //--------------------------------constructeurs---------------------------------      public textfile(){//ok         this.arraystring = new arraylist<string>();     }      //------------------------------méthodes publiques------------------------------      //---------modificateurs locaux--------      public void addtext(string text){//ok         arraystring.add(text);     }      public string readtextat(int index){//ok         return arraystring.get(index);     }      public void rmtextat(int index){ //ok         this.arraystring.remove(index);     }      //---------------écriture---------------      public void writetonewfile(string chemin) throws ioexception{//ok         printwriter bw = new printwriter(new bufferedwriter(new filewriter(new file(chemin))));         int i=0;         while (i<this.arraystring.size()){             system.out.println(this.arraystring.get(i)+ " écrit dans le fichier "+chemin);             bw.println(this.arraystring.get(i));             i++;         }         bw.close();     }      //----------------lecture----------------      public string readlinefromfile(string chemin, int ligne) throws ioexception{         string result="eof";         arraylist<string> tempo = new arraylist<string>();         tempo=openfile(chemin);         result=tempo.get(ligne);         return result;     }      public string readfromfile(string chemin) throws ioexception{         string result="";         int i=0;         arraylist<string> tempo = new arraylist<string>();         tempo=openfile(chemin);         result=tempo.get(i);         i++;         while (i < tempo.size() ){             result=result+"\n"+tempo.get(i);             i++;         }         return result;     }      //---------------chargement---------------      public void loadfromfile(string chemin) throws ioexception{//ok         this.writetonewfile(chemin+".old");         this.arraystring=openfile(chemin);     }      //------------------débug-----------------      public void debug(){//ok         int = 0;         system.out.println("contenu de arraystring: ");         while (i < this.arraystring.size()){             system.out.println(this.arraystring.get(i));             i++;         }     }      //-------------------------------méthodes privées-------------------------------      protected arraylist<string> openfile(string chemin) throws ioexception{//bug         arraylist<string> result=new arraylist<string>();         filereader ligne = new filereader(chemin);         bufferedreader buffer = new bufferedreader(ligne);         system.out.println("ouverture du fichier "+chemin+"\nlecture...");         while (buffer.read() != -1){             string tempo=buffer.readline();             system.out.println(tempo);             result.add(tempo);             }         buffer.close();         system.out.println("fermeture du fichier "+chemin);         return result;     }   } 

testtextfile.java

/**  *   */ package files;  import java.io.*; import java.util.*;  /**  * @author erwan  *  */ public class testtextfile {      /**      * @param args      * @throws ioexception       * @throws interruptedexception       */     public static void main(string[] args) throws ioexception, interruptedexception {         // todo stub de la méthode généré automatiquement         int test=0;         textfile file = new textfile();         date date = new date();         system.out.println("-------------test "+test+"-------------");         test++;         system.out.println(date);         system.out.println("-------------test "+test+"-------------");         test++;         file.debug();         system.out.println("-------------test "+test+"-------------");         test++;         file.addtext(date.tostring()+" test 3.1");         system.out.println("emplacement 0 de arraystring :");         system.out.println(file.readtextat(0));         system.out.println("emplacement 1 de arraystring :");         file.addtext(date.tostring()+" test 3.2");         system.out.println(file.readtextat(1));         file.writetonewfile("/home/erwan/data_test");         system.out.println("-------------test "+test+"-------------");         test++;         file.debug();         system.out.println("-------------test "+test+"-------------");         test++;         file.loadfromfile("/home/erwan/data_test");         file.debug();         system.out.println("-------------test "+test+"-------------");         test++;         thread.sleep(2000);         date = new date();         file.addtext(date.tostring()+" test 5");         file.debug();         file.writetonewfile("/home/erwan/data_test");         system.out.println("-------------test "+test+"-------------");         test++;         file.rmtextat(0);         file.debug();         system.out.println("-------------test "+test+"-------------");         test++;         file.rmtextat(0);         file.debug();         system.out.println("-------------test "+test+"-------------");         test++;         system.out.println(file.readlinefromfile("/home/erwan/data_test",0));         system.out.println("-------------test "+test+"-------------");         test++;         system.out.println(file.readfromfile("/home/erwan/data_test"));     }  } 

java console:

-------------test 0------------- tue jun 03 12:04:19 cest 2014 -------------test 1------------- contenu de arraystring:  -------------test 2------------- emplacement 0 de arraystring : tue jun 03 12:04:19 cest 2014 test 3.1 emplacement 1 de arraystring : tue jun 03 12:04:19 cest 2014 test 3.2 tue jun 03 12:04:19 cest 2014 test 3.1 écrit dans le fichier /home/erwan/data_test tue jun 03 12:04:19 cest 2014 test 3.2 écrit dans le fichier /home/erwan/data_test -------------test 3------------- contenu de arraystring:  tue jun 03 12:04:19 cest 2014 test 3.1 tue jun 03 12:04:19 cest 2014 test 3.2 -------------test 4------------- tue jun 03 12:04:19 cest 2014 test 3.1 écrit dans le fichier /home/erwan/data_test.old tue jun 03 12:04:19 cest 2014 test 3.2 écrit dans le fichier /home/erwan/data_test.old ouverture du fichier /home/erwan/data_test lecture... ue jun 03 12:04:19 cest 2014 test 3.1 ue jun 03 12:04:19 cest 2014 test 3.2 fermeture du fichier /home/erwan/data_test contenu de arraystring:  ue jun 03 12:04:19 cest 2014 test 3.1 ue jun 03 12:04:19 cest 2014 test 3.2 -------------test 5------------- contenu de arraystring:  ue jun 03 12:04:19 cest 2014 test 3.1 ue jun 03 12:04:19 cest 2014 test 3.2 tue jun 03 12:04:21 cest 2014 test 5 ue jun 03 12:04:19 cest 2014 test 3.1 écrit dans le fichier /home/erwan/data_test ue jun 03 12:04:19 cest 2014 test 3.2 écrit dans le fichier /home/erwan/data_test tue jun 03 12:04:21 cest 2014 test 5 écrit dans le fichier /home/erwan/data_test -------------test 6------------- contenu de arraystring:  ue jun 03 12:04:19 cest 2014 test 3.2 tue jun 03 12:04:21 cest 2014 test 5 -------------test 7------------- contenu de arraystring:  tue jun 03 12:04:21 cest 2014 test 5 -------------test 8------------- ouverture du fichier /home/erwan/data_test lecture... e jun 03 12:04:19 cest 2014 test 3.1 e jun 03 12:04:19 cest 2014 test 3.2 ue jun 03 12:04:21 cest 2014 test 5 fermeture du fichier /home/erwan/data_test ue jun 03 12:04:21 cest 2014 test 5 -------------test 9------------- ouverture du fichier /home/erwan/data_test lecture... e jun 03 12:04:19 cest 2014 test 3.1 e jun 03 12:04:19 cest 2014 test 3.2 ue jun 03 12:04:21 cest 2014 test 5 fermeture du fichier /home/erwan/data_test e jun 03 12:04:19 cest 2014 test 3.1 e jun 03 12:04:19 cest 2014 test 3.2 ue jun 03 12:04:21 cest 2014 test 5 

you using bufferedreader in openfile() method, on right track.

you should use bufferedreader.readline() instead of read() read lime @ time:

string nextline = null; while ((nextline = buffer.readline()) != null) {   result.add(nextline); } 

where result arraylist<string> , buffer bufferedreader


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -