IO操作的工具类

import java.io.*;
public final class StreamUtil
{
/**
* 将输入流复制到输出流
* @param is
* @param os
* @throws IOException
*/
public final static void write(InputStream is, OutputStream os) throws IOException
{
BufferedInputStream bis = new BufferedInputStream(is, 1024);
int c = bis.read();
while(c != -1)
{
os.write(c);
c = bis.read();
}
os.flush();
}

/**
* 将输入流按指定的编码转成字符串
* @param is
* @param encoding
* @return
* @throws IOException
*/
public final static String toString(InputStream is, String encoding) throws IOException
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
write(is, bos);
if(encoding == null) return new String(bos.toByteArray());
else return new String(bos.toByteArray(), encoding);

}

/**
* 将输入流按默认的编码转成字符串
* @param is
* @return
* @throws IOException
*/
public final static String toString(InputStream is) throws IOException
{
return toString(is, null);
}

/**
* 关闭输入流,忽略异常
* @param is
*/
public final static void closeQuitely(InputStream is)
{
try
{
if(is != null) is.close();
}
catch (IOException e)
{
}

}

/**
* 关闭输出流,忽略异常
* @param os
*/
public final static void closeQuitely(OutputStream os)
{
try
{
if(os != null) os.close();
}
catch (IOException e)
{
}
}

/**
* 半闭输入输出流,忽略异常
* @param is
* @param os
*/
public final static void closeQuitely(InputStream is, OutputStream os)
{
closeQuitely(is);
closeQuitely(os);
}
}

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理

相关文章

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部