How to convert FileInputStream into string in java? -
in java project, i'm passing fileinputstream function, need convert (typecast fileinputstream string), how it.??
public static void checkfor(fileinputstream fis) { string a=new string; a=fis //how convert fileinputstream string print string here }
you can't directly convert string. should implement add code method
//commented out because not efficient way achieve //stringbuilder builder = new stringbuilder(); //int ch; //while((ch = fis.read()) != -1){ // builder.append((char)ch); //} // //system.out.println(builder.tostring()); use aubin's solution:
public static string getfilecontent( fileinputstream fis, string encoding ) throws ioexception { try( bufferedreader br = new bufferedreader( new inputstreamreader(fis, encoding ))) { stringbuilder sb = new stringbuilder(); string line; while(( line = br.readline()) != null ) { sb.append( line ); sb.append( '\n' ); } return sb.tostring(); } }
Comments
Post a Comment