InputStream与String,Byte之间互转

  1. 本文将介绍InputStream与String,Byte之间的相互转换。以代码来说明:

  2. import java.io.ByteArrayInputStream;  
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6.   
  7. /**  
  8.  *   
  9.  * @author Andy.Chen  
  10.  * @mail Chenjunjun.ZJ@gmail.com  
  11.  *  
  12.  */  
  13. public class InputStreamUtils {  
  14.       
  15.     final static int BUFFER_SIZE = 4096;  
  16.       
  17.     /**  
  18.      * 将InputStream转换成String  
  19.      * @param in InputStream  
  20.      * @return String  
  21.      * @throws Exception  
  22.      *   
  23.      */  
  24.     public static String InputStreamTOString(InputStream in) throws Exception{  
  25.           
  26.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  27.         byte[] data = new byte[BUFFER_SIZE];  
  28.         int count = -1;  
  29.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  30.             outStream.write(data, 0, count);  
  31.           
  32.         data = null;  
  33.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  34.     }  
  35.       
  36.     /**  
  37.      * 将InputStream转换成某种字符编码的String  
  38.      * @param in  
  39.      * @param encoding  
  40.      * @return  
  41.      * @throws Exception  
  42.      */  
  43.          public static String InputStreamTOString(InputStream in,String encoding) throws Exception{  
  44.           
  45.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  46.         byte[] data = new byte[BUFFER_SIZE];  
  47.         int count = -1;  
  48.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  49.             outStream.write(data, 0, count);  
  50.           
  51.         data = null;  
  52.         return new String(outStream.toByteArray(),"ISO-8859-1");  
  53.     }  
  54.       
  55.     /**  
  56.      * 将String转换成InputStream  
  57.      * @param in  
  58.      * @return  
  59.      * @throws Exception  
  60.      */  
  61.     public static InputStream StringTOInputStream(String in) throws Exception{  
  62.           
  63.         ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));  
  64.         return is;  
  65.     }  
  66.       
  67.     /**  
  68.      * 将InputStream转换成byte数组  
  69.      * @param in InputStream  
  70.      * @return byte[]  
  71.      * @throws IOException  
  72.      */  
  73.     public static byte[] InputStreamTOByte(InputStream in) throws IOException{  
  74.           
  75.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  76.         byte[] data = new byte[BUFFER_SIZE];  
  77.         int count = -1;  
  78.         while((count = in.read(data,0,BUFFER_SIZE)) != -1)  
  79.             outStream.write(data, 0, count);  
  80.           
  81.         data = null;  
  82.         return outStream.toByteArray();  
  83.     }  
  84.       
  85.     /**  
  86.      * 将byte数组转换成InputStream  
  87.      * @param in  
  88.      * @return  
  89.      * @throws Exception  
  90.      */  
  91.     public static InputStream byteTOInputStream(byte[] in) throws Exception{  
  92.           
  93.         ByteArrayInputStream is = new ByteArrayInputStream(in);  
  94.         return is;  
  95.     }  
  96.       
  97.     /**  
  98.      * 将byte数组转换成String  
  99.      * @param in  
  100.      * @return  
  101.      * @throws Exception  
  102.      */  
  103.     public static String byteTOString(byte[] in) throws Exception{  
  104.           
  105.         InputStream is = byteTOInputStream(in);  
  106.         return InputStreamTOString(is);  
  107.     }  
  108.   
  109. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java中,inputstream和outputstream是常用于文件操作的类。Inputstream是用来读取文件数据的,Outputstream则是用来写入文件数据的。File类则是用来表示文件或目录的抽象表示。 要实现inputstream与outputstream及file间的互转,需要使用Java IO中的一些方法。其中,FileInputStream和FileOutputStream用于将文件与inputstream和outputstream进行互转换。FileReader和FileWriter用于将字符数据与inputstream和outputstream进行互转换。另外,ByteArrayInputStreamByteArrayOutputStream用于在内存中进行数据流操作。 以FileInputStream和FileOutputStream为例,可以通过以下步骤实现inputstream与file及outputstream间的换: 1.创建一个FileInputStream对象,将要读取的文件传递给构造函数。 2.通过read()方法读取文件中的数据。 3.创建一个FileOutputStream对象,将要写入的文件传递给构造函数。 4.通过write()方法将读取到的数据写入到输出流中。 以下是代码示例: ``` import java.io.*; public class FileOperation { public static void main(String args[]) { try { //创建一个输入流 FileInputStream in = new FileInputStream("input.txt"); // 读取第一个字节 int c = in.read(); while(c != -1) { System.out.print((char)c); //读取下一个字节 c = in.read(); } //关闭输入流 in.close(); //创建输出流 FileOutputStream out =new FileOutputStream("output.txt"); //写入数据 String str = "Hello Java Programming!"; byte[] bytes = str.getBytes(); out.write(bytes); //关闭输出流 out.close(); } catch (IOException e) { System.out.println("IOException"); } } } ``` 在上面的示例中,程序首先通过FileInputStream读取了一个文件的内容,然后通过FileOutputStream将读取到的内容写入到了另一个文件中。 总之,通过Java IO的各种操作,可以很方便地实现inputstream与outputstream及file间的互转。这些方法在读写文件、网络编程等方面都有广泛应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值