下面就结合源码,看看IOUTils都有什么用处吧! 代码参考https://github.com/xinghalo/JDK-Learning 常用的静态变量 在IOUtils中还是有很多常用的一些变量的,比如换行符等等 public static final (IOUtils.java:2539) at org.apache.commons.io.IOUtils.readFully(IOUtils.java:2558) at test.java.IOUtilsTest.readFullyTest ("hello world"); try { IOUtils.skip(is,4); System.out.println(IOUtils.toString ("hello world"); try { IOUtils.skipFully(is,30); System.out.println(IOUtils.toString
工具类 IOUtils 该工具类可能是平时使用得最多的工具类了。 IOUtils包含处理读、写和复制的工具方法。 = null) { in.close(); } } } 结果: 控制台打印出了这个网页的所有内容 使用IOUtils: (in, StandardCharsets.UTF_8)); } //finally { // IOUtils.closeQuietly(in); 可以直接,或通过FileUtils或IOUtils的工厂方法创建实例。推荐使用模式: 备注:也是需要close的 Filefilter:包含IOFileFilter、FilenameFilter等。 CopyUtils:已经过期,请使用IOUtils代替
IOUtils的使用: IOUtils.copy(InputStream input, OutputStream output) // 此方法有多个重载方法,满足不同的输入输出流 IOUtils.copy IOUtils.toBufferedReader(Reader reader, int size) // 获取缓冲流 IOUtils.buffer(InputStream inputStream) IOUtils.buffer IOUtils.toString(Reader input) IOUtils.toString(byte[] input, String encoding) IOUtils.toString(InputStream input) IOUtils.toByteArray(InputStream input, int size) IOUtils.toByteArray(URI uri) IOUtils.toByteArray ) // 字符串读写 IOUtils.readLines(Reader input) IOUtils.readLines(InputStream input, Charset encoding) IOUtils.readLines
filePath,e); } catch (Exception e) { log.error("occur error;filePath={}",filePath,e); } finally { IOUtils.closeQuietly (byteArrayInputStream); IOUtils.closeQuietly(inputStreamReader); //IOUtils.closeQuietly(reader ); IOUtils.closeQuietly(bufferedReader); IOUtils.closeQuietly(parser); } 这种情况下解析常规的csv文件没有任何问题 (byteArrayInputStream); IOUtils.closeQuietly(bomInputStream); IOUtils.closeQuietly(inputStreamReader ); IOUtils.closeQuietly(bufferedReader); IOUtils.closeQuietly(parser); } 原理是bom流能检测到bom头,
前言 这次带来的是Apache开源组织的CommonsIO集成组件的FileUtils和IOUtils常用操作。 in ) ); } finally { IOUtils.closeQuietly(in); } List<String> list = IOUtils.readLines(in, "UTF-8 ; byte [] by = "aaaa".getBytes(); IOUtils.write("abc", outputStream); IOUtils.write(ch, outputStream ,IOUtils.LINE_SEPARATOR,os); //把字符串转换流 InputStream inputStream = IOUtils.toInputStream("Himit_ZH", "UTF -8"); Ⅲ流的关闭 try { IOUtils.copy(inputStream, outputStream); } finally { IOUtils.closeQuietly
String s = IOUtils.toString(new FileInputStream(keyWordFile) , "UTF-8"); //去除空格和换行符 String 两种结果的区别:使用BufferedReader是一行一行的读取,随后使用StringBuilder添加,所以是没有换行符的,而IOUtils是直接将整个文件的内容转成了字符串,所以也包括了换行符。 然而IOUtils在写的时候使用了StringBuilderWriter,这个写的效率更高些,比使用StringBuilder一个个append要高许多。 因此综上所述总结:BudderReader读取更快,写满,IOUtils读取慢,写更快。 有人可能会提出疑问,既然IOUtis读慢写快,为何不直接也让它读取更快呢,这样不就读写都快了吗? 因此IOUtils为了大众能够通用所以采取了这种设计。
.*; import org.apache.hadoop.io.IOUtils; import org.junit.Test; import java.io.File; import java.io.FileInputStream (fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream (fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); fs.close (fis); IOUtils.closeStream(fos); fs.close(); } 2. (fis, fos, configuration); // 6 关闭资源 IOUtils.closeStream(fis); IOUtils.closeStream(fos); } 3.
(inStream, outStream, 4096, false); }catch(Exception e){ e.printStackTrace(); }finally{ IOUtils.closeStream (inStream); IOUtils.closeStream(outStream); } 3.文件下载 API: fs.copyToLocalFile(false, new Path("hdfs (inStream, System.out, 4096, false); }catch(Exception e){ e.printStackTrace(); }finally{ IOUtils.closeStream (fis); IOUtils.closeStream(fos); } 下载第二块 @Test // 定位下载第二块内容 public void readFileSeek2() throws Exception (fis, fos, 1024); //关闭流 IOUtils.closeStream(fis); IOUtils.closeStream(fos); } 合并文件 cmd中:
= 0; i < 1024 * 128; i++) { is.read(buffer); os.write(buffer); } //关流 IOUtils.closeStream (is); IOUtils.closeStream(os); } //从HDFS中下载第二个块的文件,即128~256M @Test public void testSecondBlock for (int i = 0; i < 1024 * 128; i++) { is.read(buffer); os.write(buffer); } //关流 IOUtils.closeStream (is); IOUtils.closeStream(os); } //从HDFS中下载第三个块的文件,即256~384M @Test public void testSecondBlock() (is); IOUtils.closeStream(os); } //从HDFS中下载最后一个块的文件 @Test public void testFinalBlock() throws
} 2 Service层 import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils URLEncoder.encode(“标题”,"UTF-8")); ServletOutputStream outputStream = response.getOutputStream(); IOUtils.copy (inputStream, outputStream); IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils // 创建压缩输出流 CompressionOutputStream out = codec.createOutputStream(outputStream); IOUtils.copyBytes (in, out, conf); IOUtils.closeStream(in); IOUtils.closeStream(out); System.out.println (in, System.out, conf); IOUtils.closeStream(in); } // 使用文件扩展名来推断二来的codec来对文件进行解压缩 (in, out, conf); } finally { IOUtils.closeStream(out); IOUtils.closeStream
InputStream in = this.getClass().getResourceAsStream("/xxx.txt"); System.out.println(IOUtils.toString (inputStream2); System.out.println(string2); String string3 = IOUtils.toString(inputStream3); System.out.println this.getClass(); InputStream inputStream4 = class1.getResourceAsStream("b.txt"); System.out.println(IOUtils.toString /index.jsp"); System.out.println(IOUtils.toString(inputStream6)); } public void doGet(HttpServletRequest /index.jsp"); String s = IOUtils.toString(input);//读取输入流内容,转换成字符串返回 System.out.println(s); }
FileOutputStream out = new FileOutputStream(localFile, true); // 将in复制到out,具体方式可自由实现,这里展示IOUtils方式 if (remoteSize - localSize >= 2 * FileUtils.ONE_GB) { // 文件大小超过2G时 IOUtils.copyLarge(in, out); }else { IOUtils.copy(in, out); } // 刷新此输出流,并强制写出所有缓冲的输出字节。 out.flush(); // 关闭相关流,这里展示IOUtils方式 IOUtils.closeQuietly(in); IOUtils.closeQuietly(out); // 接收来自服务器的完成答复并验证整个事务是否成功
IOException { inputStream.reset(); } } 开启并重复使用 ReuseableStream reuse = new ReuseableStream(IOUtils.toInputStream ("hello", Charsets.UTF_8)); System.out.println(IOUtils.toString(reuse.open(),Charsets.UTF_8)); reuse.reset (); System.out.println(IOUtils.toString(reuse.open(),Charsets.UTF_8)); open的时候mark一下,然后想重复使用的时候reset一下
import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import javax.servlet.http.HttpServletResponse zip.putNextEntry(entry); zip.write(FileUtils.readFileToByteArray(file)); IOUtils.closeQuietly 将目标文件打包成zip导出 File file = new File(sourceFilePath); handlerFile(zip, file,""); IOUtils.closeQuietly data.length); response.setContentType("application/octet-stream;charset=UTF-8"); IOUtils.write
import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IOUtils URI.create(args[0]),conf); InputStream in = null; try{ in = hdfs.open(new Path(args[0])); IOUtils.copyBytes (in,System.out,4096,true); }finally{ IOUtils.closeStream(in); } } }
(fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); fs.close // 3 获取输出流 FileOutputStream fos = new FileOutputStream(new File("e:/banhua.txt")); // 4 流的对拷 IOUtils.copyBytes (fis, fos, configuration); // 5 关闭资源 IOUtils.closeStream(fos); IOUtils.closeStream(fis); fs.close (fis); IOUtils.closeStream(fos); fs.close(); } (2)下载第二块 @Test public void readFileSeek2() throws IOException (fis, fos, configuration); // 6 关闭资源 IOUtils.closeStream(fis); IOUtils.closeStream(fos); } (3)合并文件
null; try { in = srcFS.open(src); out = dstFS.create(dst, overwrite); IOUtils.copyBytes (in, out, conf, true); } catch (IOException e) { IOUtils.closeStream(out); IOUtils.closeStream for (int i = 0; i < 1024 * 10; i++) { is.read(buffer); os.write(buffer); } //关流 IOUtils.closeStream (is); IOUtils.closeStream(os); } }
import org.apache.commons.io.IOUtils; import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream FileOutputStream(outputFile)); compressDirectoryToZipfile(sourceDir,sourceDir,zipFile); IOUtils.closeQuietly sourceDir.replace(rootDir, "") + file.getName()); out.putNextEntry(entry); IOUtils.copy IOException e){ e.printStackTrace(); }finally { IOUtils.closeQuietly
file.mkdir(); } if(count < 10){ IOUtils.download ("E:/mp3/957/0"+count+".mp3", inputStream); }else{ IOUtils.download("E:/mp3 再给出IOUtils的代码: import java.io.FileOutputStream; import java.io.InputStream; public class IOUtils {