How to write encoded text to a file using Java? -


how write encoded text file using java/jsp filewriter?

filewriter testfilewriter = new filewriter(testfile, true); testfilewriter.write(testtext);  testtext:- text testfile:- string (encoded) 

what m trying encoding testfile base64 , storing in file. best way so?

since data not plain text, can't use filewriter. need fileoutputstream.

encode text base64:

byte[] encodedtext = base64.encodebase64( testtext.getbytes("utf-8") ); 

and write file:

try (outputstream stream = new fileoutputstream(testfile)) {     stream.write(encodedtext); } 

or if don't want lose existing data, write in append mode setting append boolean true:

try (outputstream stream = new fileoutputstream(testfile, true)) {     stream.write(encodedtext); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -