java io 常用方法
来源:互联网 发布:液压系统绘图软件 编辑:程序博客网 时间:2024/06/14 06:10
/**
* 数据流的工具类
*
*
*/
public final class IOUtil {
public static final String DFT_CHARSET = "utf-8";
private IOUtil(){}
private static final int BUFFER_SIZE = 2048;
/**
* 关闭所有closeables子类的流,close异常不会抛出
* @param closeables
*/
public static void close(java.io.Closeable... closeables){
if( closeables != null ){
for (java.io.Closeable c : closeables) {
if (c != null) {
try{
c.close();
}catch (Exception e) {
Log.d("IOUTil","Close failed",e);
}
}
}
}
}
/**
* 网络条件不好的情况下关闭网络流会等很久,这里增加了一个异步关闭的方法
* @param is
*/
public static void asynchronousClose( final java.io.Closeable... closeables ){
new Thread(new Runnable() {
@Override
public void run() {
IOUtil.close( closeables );
}
}).start();
}
/**
* 把输入流写到输出流中.不关闭该流
* @param is
* @param os
* @throws IOException
*/
public static void fromTo( InputStream is, OutputStream os ) throws IOException{
fromTo( is, os, false );
}
/**
* 把输入流写到输出流中.
* @param is
* @param os
* @param closeIO 指定是否关闭该流
* @throws IOException
*/
public static void fromTo( InputStream is, OutputStream os, boolean closeIO ) throws IOException{
if( is == null || os == null ) {
return;
}
try{
byte [] buffer = new byte[BUFFER_SIZE];//1k bytes buffer
int size = is.read( buffer, 0, BUFFER_SIZE);
while( size != -1 ){
os.write( buffer, 0 , size );
size = is.read( buffer, 0, BUFFER_SIZE);
}
os.flush();
}finally{
if( closeIO ){
IOUtil.close( is, os );
}
}
}
/**
* 把reader读成字符串,不关闭该流
* @param reader
* @return
* @throws IOException
*/
public static String read( Reader reader ) throws IOException{
return read( reader, false );
}
/**
* 把reader读成字符串
* @param reader
* @param closeReader 是否关闭该流
* @return
* @throws IOException
*/
public static String read( Reader reader, boolean closeReader ) throws IOException{
if( reader == null ) {
return null;
}
char [] buffer = new char[BUFFER_SIZE];
StringBuilder sb = new StringBuilder();
try {
int count = reader.read(buffer);
while( count != -1 ){
sb.append( buffer, 0, count );
count = reader.read(buffer);
}
} finally{
if( closeReader ){
IOUtil.close( reader );
}
}
return sb.toString();
}
/**
* 根据输入流解压成gzip,并根据编码生产字符串
* @param inputStream
* @param charset
* @return
* @throws IOException
*/
public static String readGzip(InputStream inputStream, String charset) throws IOException{
GZIPInputStream gis = null;
try {
byte[] realBytes = IOUtil.readBytes(inputStream);
byte[] unzipBytes = GzipUtil.ungzip(realBytes);
return new String(unzipBytes, charset);
} finally {
IOUtil.close(gis);
}
}
/**
* close InputStream after read.
* @param inputStream
* @param charSet
* @return
* @throws IOException
*/
public static String readText( InputStream inputStream, String charSet ) throws IOException{
if( inputStream == null ) {
return null;
}
try {
InputStreamReader reader = new InputStreamReader( inputStream, charSet );
return read( reader, true );
} finally {
IOUtil.close(inputStream);
}
}
/**
*
* close InputStream after read.
* @param inputStream
* @return
* @throws IOException
*/
public static String readText( InputStream inputStream ) throws IOException{
return readText(inputStream, DFT_CHARSET);
}
/**
*
* 读取完所有inputStream内容之后close掉inputStream
* @param inputStream
* @return
* @throws IOException
*/
public static byte [] readBytes( InputStream inputStream ) throws IOException{
ByteArrayOutputStream bos= new ByteArrayOutputStream();
fromTo(inputStream, bos, true);
return bos.toByteArray();
}
/**
* 读取固定长度的数据()
* @param inStream
* @param length
* @return
* @throws IOException
*/
public static byte[] readBytes(InputStream inStream, int length,boolean closeIO) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
fromTo(inStream, bos, length,closeIO);
return bos.toByteArray();
}
/**
* 从输入流读取固定长度到输出流中(不关闭输入流)
* @param is
* @param os
* @param length
* @throws IOException
*/
public static void fromTo(InputStream is,OutputStream os,int length,boolean closeIO) throws IOException {
if( is == null || os == null ) {
return;
}
try{
byte[] bf = new byte[BUFFER_SIZE];
int len = -1;
int readCount = 0;
int readLength = length > BUFFER_SIZE ? BUFFER_SIZE : length;
while((len = is.read(bf, 0, readLength)) != -1 && readCount < length){
os.write(bf, 0, len);
readCount += len;
readLength = (length - readCount) > BUFFER_SIZE ? BUFFER_SIZE : (length - readCount);
}
os.flush();
}finally{
IOUtil.close(os);
}
}
}
- java io 常用方法
- JAVA IO处理文件的常用方法
- java IO流File常用的方法
- java io流常用方法总结
- java IO常用类和方法
- IO常用方法
- java.io.File操作文件的一些常用方法
- java io新类及常用输入输出方法
- java IO的File类常用方法总结一
- java IO的File类常用方法总结二
- java.io.File 进行文件操作常用方法
- java 常用IO操作
- java 常用IO操作
- 常用Java IO 上
- JAVA IO常用总结
- java io常用操作方法
- JAVA常用IO流类
- Java常用IO流
- cocos2d-x2.2 新建项目
- Linux-C学习笔记-双向循环链表(支持DEBUG调试)
- 级联查询在javascript与struts2中区别
- Expect 教程中文版
- object-c runloop
- java io 常用方法
- 深入分析 Linux 内核链表
- IOS推送证书的制作
- windows下安装ruby on rails 环境搭建·
- LeetCode 之 Palindrome Number
- 在label中设置上下标
- App在闲置一段时间(也即用户无动作)执行指定动作
- Android使用缓存优化ListView
- Director类与Caches