In my java project, I'm passing FileInputStream to a function, I need to convert (typecast FileInputStream to string), How to do it.??
public static void checkfor(FileInputStream fis) { String a=new String; a=fis //how to do convert fileInputStream into string print string here } 56 Answers
You can't directly convert it to string. You should implement something like this Add this code to your method
//Commented this out because this is not the efficient way to achieve that //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(); } } 8public 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(); } } 5Using Apache commons IOUtils function
import org.apache.commons.io.IOUtils; InputStream inStream = new FileInputStream("filename.txt"); String body = IOUtils.toString(inStream, StandardCharsets.UTF_8.name()); 1Don't make the mistake of relying upon or needlessly converting/losing endline characters. Do it character by character. Don't forget to use the proper character encoding to interpres the stream.
public String getFileContent( FileInputStream fis ) { StringBuilder sb = new StringBuilder(); Reader r = new InputStreamReader(fis, "UTF-8"); //or whatever encoding int ch = r.read(); while(ch >= 0) { sb.append(ch); ch = r.read(); } return sb.toString(); } If you want to make this a little more efficient, you can use arrays of characters instead, but to be honest, looping over the characters can be still quite fast.
public String getFileContent( FileInputStream fis ) { StringBuilder sb = new StringBuilder(); Reader r = new InputStreamReader(fis, "UTF-8"); //or whatever encoding char[] buf = new char[1024]; int amt = r.read(buf); while(amt > 0) { sb.append(buf, 0, amt); amt = r.read(buf); } return sb.toString(); } 4From an answer I edited here:
static String convertStreamToString(java.io.InputStream is) { if (is == null) { return ""; } java.util.Scanner s = new java.util.Scanner(is); s.useDelimiter("\\A"); String streamString = s.hasNext() ? s.next() : ""; s.close(); return streamString; } This avoids all errors and works well.
Use following code ---->
try { FileInputStream fis=new FileInputStream("filename.txt"); int i=0; while((i = fis.read()) !=-1 ) { // to reach until the laste bytecode -1 System.out.print((char)i); /* For converting each bytecode into character */ } fis.close(); } catch(Exception ex) { System.out.println(ex); }