如何在java中将FileInputStream转换为字符串?

时间:2013-03-01 15:47:23

标签: java fileinputstream

在我的java项目中,我将FileInputStream传递给一个函数, 我需要转换(类型转换FileInputStream到字符串), 怎么做。??

public static void checkfor(FileInputStream fis) {
   String a=new String;
   a=fis         //how to do convert fileInputStream into string
   print string here
}

6 个答案:

答案 0 :(得分:26)

您无法直接将其转换为字符串。你应该实现这样的东西 将此代码添加到您的方法

    //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());

使用Aubin的解决方案:

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();
   }
}

答案 1 :(得分:18)

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();
   }
}

答案 2 :(得分:5)

使用Apache commons IOUtils函数

import org.apache.commons.io.IOUtils;

InputStream inStream = new FileInputStream("filename.txt");
String body = IOUtils.toString(inStream, StandardCharsets.UTF_8.name()); 

答案 3 :(得分:4)

不要犯错误依赖或不必要地转换/丢失最终字符。逐字逐句地做。不要忘记使用正确的字符编码来插入流。

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();
}

如果你想提高效率,你可以使用字符数组,但说实话,循环字符仍然可以很快。

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();
}

答案 4 :(得分:1)

使用以下代码---->

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); 
}

答案 5 :(得分:0)

根据我编辑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;
}

这可以避免所有错误并且运行良好。