import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.InputStreamReader; public class Test4 { public static void main(String[] args) { String str = "我在深圳"; InputStream is = StreamUtil.getStrToStream(str); System.out.println(str); System.out.println(StreamUtil.getStreamToStr(is)); /** * 我在深圳 * 我在深圳 */ } } /** * 字符串和输入流互转类 */ class StreamUtil { /** * 将一个字符串转化为输入流 * * @param sInputString * @return */ public static InputStream getStrToStream(String sInputString) { if (sInputString != null && !sInputString.trim().equals("")) { try { ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes()); return tInputStringStream; } catch (Exception ex) { ex.printStackTrace(); } } return null; } /** * 将一个输入流转化为字符串 * * @param tInputStream * @return */ public static String getStreamToStr(InputStream tInputStream) { if (tInputStream != null) { try { BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(tInputStream)); StringBuffer tStringBuffer = new StringBuffer(); String sTempOneLine; while ((sTempOneLine = tBufferedReader.readLine()) != null) { tStringBuffer.append(sTempOneLine); } return tStringBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); } } return null; } }
原文地址:https://www.cnblogs.com/smartsmile/p/11617349.html
时间: 2024-11-03 04:24:47