流与字符串的转换

思想:

流转为字符串:

用byte中转,将字节流转为存入byte数组,再将其转为字符串

byte[] bytes  = new byte[is.available()];//定义容器 
is.read(bytes);//将流读入byte
String str = new String(bytes,"utf-8");//将byte转为String

ByteArrayOutputStream 实现

ByteArrayOutputStream baos = new ByteArrayOutputStream();  
int i;  
while ((i = is.read()) != -1) {  
    baos.write(i); 
}  
String str = baos.toString("utf-8"); 

BufferedReader 实现(Reader–>String)

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));  
StringBuffer sb = new StringBuffer();  
String str;   
int i=0;
while ((str = reader.readLine()) != null)  
{  
	sb.append(str).append("\n");  
}
str = sb.toString();

字符串转inputStream

InputStream is = new ByteArrayInputStream(string.getBytes("utf-8"));  

String写入OutputStream

OutputStream os = System.out;  
os.write(str1.getBytes()); 

OutputStream写入String

String str1 = "但还是笔记本试试ss数据可能怕skksks";
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
baos.write(str1.getBytes("utf-8"));
String str = baos.toString("utf-8");  
System.out.println(str);

//将指定路径下的文件转为输入流
InputStream is =  new FileInputStream(new File(inPath));

//读取输入流转为String
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));  
	StringBuffer sb = new StringBuffer();  
	String str;   
	int i=0;
	while ((str = reader.readLine()) != null)  
	{  
		sb.append(str).append("\n");  
	}
str = sb.toString();//接收STRING内容

// String 转outputStream,将指定文本内容写入指定目录文件
OutputStream os = new FileOutputStream(path);//输出路径+文件名
os.write(str.getBytes());  //写入String

others

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class ConvertUtil {
    // inputStream转outputStream
    public ByteArrayOutputStream parse(final InputStream in) throws Exception {
        final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = in.read()) != -1) {
            swapStream.write(ch);
        }
        return swapStream;
    }

    // outputStream转inputStream
    public ByteArrayInputStream parse(final OutputStream out) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = (ByteArrayOutputStream) out;
        final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream;
    }

    // inputStream转String
    public String parse_String(final InputStream in) throws Exception {
        final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
        int ch;
        while ((ch = in.read()) != -1) {
            swapStream.write(ch);
        }
        return swapStream.toString();
    }

    // OutputStream 转String
    public String parse_String(final OutputStream out) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        baos = (ByteArrayOutputStream) out;
        final ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
        return swapStream.toString();
    }

    // String转inputStream
    public ByteArrayInputStream parse_inputStream(final String in) throws Exception {
        final ByteArrayInputStream input = new ByteArrayInputStream(in.getBytes());
        return input;
    }

    // String 转outputStream
    public ByteArrayOutputStream parse_outputStream(final String in) throws Exception {
        return parse(parse_inputStream(in));
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值