Java里经常会有将输入输出流的来源或目录设定为一个字符串,实现方法有两种
第一种,使用StringReader或StringWriter,但是只有writer,不能转换为InputStream或OutputStream
示例
package net.nyist.io;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
public class StringNodeTest {
public static void main(String[] args) {
String src = "从明天起,做一个幸福的人,\n喂马,劈材,周游世界,\n从明天起,关心粮食和蔬菜,\n我有一所房子,面朝大海,春暖花开,\n从明天起,和每一个人通信,告诉他们我的幸福\n";
char[] buffer = new char[32];
int hasRead = 0;
try(
StringReader sr = new StringReader(src);
){
//采用循环读取的方式,读取字符串
while((hasRead = sr.read(buffer))> 0 ){
//
System.out.println(new String(buffer,0,hasRead));
}
}catch(IOException ioe){
ioe.printStackTrace();
}
try(
//
StringWriter sw = new StringWriter();
){
//调用方法执行输出
sw.write("有一个美丽的新世界\n");
writer.flush();
sw.write("有一个美丽的新世界\n");
writer.flush();
sw.write("有一个美丽的新世界\n");
writer.flush();
sw.write("有一个美丽的新世界\n");
writer.flush();
sw.write("有一个美丽的新世界\n");
writer.flush();
System.out.println(sw.toString());
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
writer.flush(); 方法在写入以后一定要调用,将字符从缓冲区写入目标区,
否则输出的字符串会有不完整的情况。
BufferedReader.readLine()方法,读入时,行尾的换行符是不会读入的,需要换行时
需要重新追加换行符\n。
第二种方法 使用ByteArrayInputStream
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author Andy.Chen
* @mail [email protected]
*
*/
public class InputStreamUtils {
final static int BUFFER_SIZE = 4096;
/**
* 将InputStream转换成String
* @param in InputStream
* @return String
* @throws Exception
*
*/
public static String InputStreamTOString(InputStream in) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return new String(outStream.toByteArray(),"ISO-8859-1");
}
/**
* 将InputStream转换成某种字符编码的String
* @param in
* @param encoding
* @return
* @throws Exception
*/
public static String InputStreamTOString(InputStream in,String encoding) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return new String(outStream.toByteArray(),"ISO-8859-1");
}
/**
* 将String转换成InputStream
* @param in
* @return
* @throws Exception
*/
public static InputStream StringTOInputStream(String in) throws Exception{
ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("ISO-8859-1"));
return is;
}
/**
* 将InputStream转换成byte数组
* @param in InputStream
* @return byte[]
* @throws IOException
*/
public static byte[] InputStreamTOByte(InputStream in) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[BUFFER_SIZE];
int count = -1;
while((count = in.read(data,0,BUFFER_SIZE)) != -1)
outStream.write(data, 0, count);
data = null;
return outStream.toByteArray();
}
/**
* 将byte数组转换成InputStream
* @param in
* @return
* @throws Exception
*/
public static InputStream byteTOInputStream(byte[] in) throws Exception{
ByteArrayInputStream is = new ByteArrayInputStream(in);
return is;
}
/**
* 将byte数组转换成String
* @param in
* @return
* @throws Exception
*/
public static String byteTOString(byte[] in) throws Exception{
InputStream is = byteTOInputStream(in);
return InputStreamTOString(is);
}
}