package org.apache.cxf.helpers; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.nio.charset.Charset; public final class IOUtils { public static final Charset UTF8_CHARSET = Charset.forName("utf-8"); private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; private IOUtils() { } /** * Use this function instead of new String(byte[], String) to avoid surprises from * non-standard default encodings. * @param bytes * @param charsetName * @return */ public static String newStringFromBytes(byte[] bytes, String charsetName) { try { return new String(bytes, charsetName); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Impossible failure: Charset.forName(\"" + charsetName + "\") returns invalid name."); } } /** * Use this function instead of new String(byte[]) to avoid surprises from non-standard default encodings. * @param bytes * @return */ public static String newStringFromBytes(byte[] bytes) { return newStringFromBytes(bytes, UTF8_CHARSET.name()); } /** * Use this function instead of new String(byte[], int, int, String) * to avoid surprises from non-standard default encodings. * @param bytes * @param charsetName * @param start * @param length * @return */ public static String newStringFromBytes(byte[] bytes, String charsetName, int start, int length) { try { return new String(bytes, start, length, charsetName); } catch (UnsupportedEncodingException e) { throw new RuntimeException("Impossible failure: Charset.forName(\"" + charsetName + "\") returns invalid name."); } } /** * Use this function instead of new String(byte[], int, int) * to avoid surprises from non-standard default encodings. * @param bytes * @param start * @param length * @return */ public static String newStringFromBytes(byte[] bytes, int start, int length) { return newStringFromBytes(bytes, UTF8_CHARSET.name(), start, length); } public static int copy(final InputStream input, final OutputStream output) throws IOException { return copy(input, output, DEFAULT_BUFFER_SIZE); } public static int copyAndCloseInput(final InputStream input, final OutputStream output) throws IOException { try { return copy(input, output, DEFAULT_BUFFER_SIZE); } finally { input.close(); } } public static int copyAndCloseInput(final InputStream input, final OutputStream output, int bufferSize) throws IOException { try { return copy(input, output, bufferSize); } finally { input.close(); } } public static int copy(final InputStream input, final OutputStream output, int bufferSize) throws IOException { int avail = input.available(); if (avail > 262144) { avail = 262144; } if (avail > bufferSize) { bufferSize = avail; } final byte[] buffer = new byte[bufferSize]; int n = 0; n = input.read(buffer); int total = 0; while (-1 != n) { if (n == 0) { throw new IOException("0 bytes read in violation of InputStream.read(byte[])"); } output.write(buffer, 0, n); total += n; n = input.read(buffer); } return total; } public static void copy(final Reader input, final Writer output, final int bufferSize) throws IOException { final char[] buffer = new char[bufferSize]; int n = 0; n = input.read(buffer); while (-1 != n) { if (n == 0) { throw new IOException("0 bytes read in violation of InputStream.read(byte[])"); } output.write(buffer, 0, n); n = input.read(buffer); } } public static String toString(final InputStream input) throws IOException { return toString(input, DEFAULT_BUFFER_SIZE); } public static String toString(final InputStream input, String charset) throws IOException { return toString(input, DEFAULT_BUFFER_SIZE, charset); } public static String toString(final InputStream input, int bufferSize) throws IOException { return toString(input, bufferSize, null); } public static String toString(final InputStream input, int bufferSize, String charset) throws IOException { int avail = input.available(); if (avail > bufferSize) { bufferSize = avail; } Reader reader = charset == null ? new InputStreamReader(input, UTF8_CHARSET) : new InputStreamReader(input, charset); return toString(reader, bufferSize); } public static String toString(final Reader input) throws IOException { return toString(input, DEFAULT_BUFFER_SIZE); } public static String toString(final Reader input, int bufSize) throws IOException { StringBuilder buf = new StringBuilder(); final char[] buffer = new char[bufSize]; int n = 0; n = input.read(buffer); while (-1 != n) { if (n == 0) { throw new IOException("0 bytes read in violation of InputStream.read(byte[])"); } buf.append(new String(buffer, 0, n)); n = input.read(buffer); } input.close(); return buf.toString(); } public static String readStringFromStream(InputStream in) throws IOException { StringBuilder sb = new StringBuilder(1024); for (int i = in.read(); i != -1; i = in.read()) { sb.append((char) i); } in.close(); return sb.toString(); } /** * Load the InputStream into memory and return a ByteArrayInputStream that * represents it. Closes the in stream. * * @param in * @return * @throws IOException */ public static ByteArrayInputStream loadIntoBAIS(InputStream in) throws IOException { int i = in.available(); if (i < DEFAULT_BUFFER_SIZE) { i = DEFAULT_BUFFER_SIZE; } LoadingByteArrayOutputStream bout = new LoadingByteArrayOutputStream(i); copy(in, bout); in.close(); return bout.createInputStream(); } public static void consume(InputStream in) throws IOException { int i = in.available(); if (i == 0) { //if i is 0, then we MAY have already hit the end of the stream //so try a read and return rather than allocate a buffer and such int i2 = in.read(); if (i2 == -1) { return; } //reading the byte may have caused a buffer to fill i = in.available(); } if (i < DEFAULT_BUFFER_SIZE) { i = DEFAULT_BUFFER_SIZE; } if (i > 65536) { i = 65536; } byte bytes[] = new byte[i]; while (in.read(bytes) != -1) { //nothing - just discarding } } public static byte[] readBytesFromStream(InputStream in) throws IOException { int i = in.available(); if (i < DEFAULT_BUFFER_SIZE) { i = DEFAULT_BUFFER_SIZE; } ByteArrayOutputStream bos = new ByteArrayOutputStream(i); copy(in, bos); in.close(); return bos.toByteArray(); } }
相关推荐
统一修改目录下文件后缀名,IO流的基本操作,递归遍历目录下所有文件,找到符合条件的后缀并修改
对于查询出对象,此工具包目前支持的比较好的是mysql,鉴于数据库字段全大写的问题,如果你的类属性愿意全大写,那支持oracle也没问题。换下驱动就ok了。 附DOC文档和源代码。如有改进意见,请邮件我。supperman_...
Java IO流学习总结 Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据...
- 由第三方研发的工具类 - 要使用commons-io工具包,就需要先从第三方下载该工具包 - 在当前项目工程下,导入commons-io工具包(借鉴之前使用过的logback) 导commons-io包步骤: 1. 在idea的项目工程下,创建目录...
是一款处理io流的工具,封装了很多处理io流和文件的方法,可以大大简化我们处理io流和操作文件的代码。从common-io的官方使用文档可以看出,它主要分为工具类、尾端类、行迭代器、文件过滤器、文件比较器和扩展流。
import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /**...
commons-io是一款初六io流的工具,封装了很多处理io流和文件的方法,可以大大简化我们处理io流和操作文件的代码.commons-io主要分为,工具类,尾端类,迭代器,文件过滤器等.
java.io.File myFilePath = new java.io.File(filePath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { System.out.println("新建目录操作出错"); e.printStackTrace...
IO流 在Java当中可以使用流来完成读写文件等操作,那么什么是流呢? 1. 什么是流? Java当中的流大概的分为输入流和输出流两类,输入流,就是从不同的数据源当中读取数据,数据源可以是磁盘文件、内存数据等。 输出...
包含 图片 字符串 android系统信息 网络请求 IO操作流 集合 等等···工具类,经典算法等
在Java的io流操作中可能会用到复制文件或是文件夹,在拷贝文件的时候回很费事,不如直接封装为工具类,在使用的时候直接调用方法
3. IO流工具类的抽取,将所有的关流(字节流和字符流)方法抽取出来,优化代码 4. 字符流:(重点) * 使用场景:使用于处理文本相关的文件 * * Reader 抽象类: * |子类 * ...
第13天:算法工具类包装类 第14天:正则及其他类 第15-18天:数组到集合 第19天:文件操作IO 第20、21天:IO流文件操作类图 第22天:其他流 第23天:多线程 第24天:多线程深入 第25天:GUI 第26天:网络变成 第27天...
io模块在解释器的内置open()之上实现了一些类来完成基于文件的输入和输出操作。这些类得到了适当的分解,从而可以针对不同的用途重新组合——例如,支持向一个网络套接字写Unicode数据。 1.1 内存中的流 StringIO...
在早期的 Java 版本中,文件 IO 操作功能一直相对较弱,主要存在以下问题: 缺乏对现代文件系统的支持:只提供的基础的文件操作,不支持很多现代的文件系统 API 不够直观:文件操作的 API 设计相对较为复杂和冗长,...
jdk是 java 的开发工具,全称为Java Development Kit,包含java... java核心类库是指java提供的一组基础类和接口,用于处理常见的任务和操作,例如字符串的操作,数组的操作,集合的操作,多线程,IO流,网络编程等。
FileInputStream、FileOutputStream、BufferedReader、BufferedWriter等:用于进行文件和流的输入输出操作,可以读取、写入文件和处理数据流。 字符串处理类: String、StringBuffer、StringBuilder等:用于处理...
通过java操作excel表格的工具类库 支持Excel 95-2000的所有版本 生成Excel 2000标准格式 支持字体、数字、日期操作 能够修饰单元格属性 支持图像和图表 应该说以上功能已经能够大致满足我们的需要。最...
Powerful collection utilities(强大的集合工具类): java.util.Collections 中未包含的常用操作工具类 4. Extension utilities(扩展工具类): 给 Collection 对象添加一个装饰器? 实现迭代器? 我们可以更容易...