String to file not maintaing line breaks displays in terminal correclty java -


i want read text file , change text output text file. i'd open file in notepad

new java - has been rehashed , posted in different way throughout forum. seem missing /n in string - thought did in below code.

the part confusing me displays in terminal correctly when use showfile method.

i assume way i'm writing file. i'd continue use method instead of string variable

original file contains text

bob

red

door

i use read text file , input in string.

public void readfile(string filename) {     filetext = "";      try     {         scanner file = new scanner(new file(filename));         while (file.hasnextline())         {             string line = file.nextline();             filetext += line +"\n";         }         file.close();     }     catch (exception e)     {          system.out.println(e);     } 

i use method swap "r"s "b"s.

i use showfile method , displays in terminal correctly

this shows correctly

bob

bed

door

public string showfile() {     return filetext; } 

but try output string file using.

try {         file file = new file("test1.txt");         filewriter filewriter = new filewriter(file);         filewriter.write(startupmodified.showfile());         filewriter.flush();         filewriter.close();     } catch (ioexception e) {         e.printstacktrace();     } 

it keeps spacing loose line breaks

displays:

bob bed door

this constructor class startupmodified

 public startupfile(string filename) {     readfile(filename);         } 

functions correctly

changed filetext += line +"\n";

to filetext += line +"\r\n";

i assumed writer because displayed correctly in terminal.

thank you


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -