首页
学习
活动
专区
圈层
工具
发布
    • 综合排序
    • 最热优先
    • 最新优先
    时间不限
  • 来自专栏xingoo, 一个梦想做发明家的程序员

    Java程序员的日常—— IOUtils总结

    下面就结合源码,看看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

    2.7K90发布于 2018-01-17
  • 来自专栏BAT的乌托邦

    【小家java】Java之Apache Commons-IO使用精讲(FileUtils、IOUtils、FileFilter全覆盖)

    工具类 IOUtils 该工具类可能是平时使用得最多的工具类了。 IOUtils包含处理读、写和复制的工具方法。 = null) { in.close(); } } } 结果: 控制台打印出了这个网页的所有内容 使用IOUtils: (in, StandardCharsets.UTF_8)); } //finally { // IOUtils.closeQuietly(in); 可以直接,或通过FileUtils或IOUtils的工厂方法创建实例。推荐使用模式: 备注:也是需要close的 Filefilter:包含IOFileFilter、FilenameFilter等。 CopyUtils:已经过期,请使用IOUtils代替

    4.4K22发布于 2019-09-03
  • 来自专栏Java经验总结

    IO工具包最基本的工具包,难道你还不会用?

    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

    1.8K51发布于 2021-07-23
  • 来自专栏一个执拗的后端搬砖工

    解析csv文件兼容bom头

    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头,

    2.7K40编辑于 2021-12-21
  • 来自专栏Hcode网站

    Java中的IO流(三)Apache Commons IO组件的常用操作

    前言 这次带来的是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

    2.1K41发布于 2020-07-14
  • 来自专栏Java深度编程

    如何将文件内容转成String字符串

    String s = IOUtils.toString(new FileInputStream(keyWordFile) , "UTF-8"); //去除空格和换行符 String 两种结果的区别:使用BufferedReader是一行一行的读取,随后使用StringBuilder添加,所以是没有换行符的,而IOUtils是直接将整个文件的内容转成了字符串,所以也包括了换行符。 然而IOUtils在写的时候使用了StringBuilderWriter,这个写的效率更高些,比使用StringBuilder一个个append要高许多。 因此综上所述总结:BudderReader读取更快,写满,IOUtils读取慢,写更快。 有人可能会提出疑问,既然IOUtis读慢写快,为何不直接也让它读取更快呢,这样不就读写都快了吗? 因此IOUtils为了大众能够通用所以采取了这种设计。

    4K50发布于 2020-06-10
  • 来自专栏不温卜火

    HDFS系列(7) | HDFS的 I/O流 API操作

    .*; 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.

    75240发布于 2020-10-28
  • 来自专栏吟风者

    HDFS:常用客户端API及IO操作

    (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中:

    70430发布于 2019-08-07
  • 来自专栏无题~

    HDFS之下载某个块的文件

    = 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

    60510发布于 2020-07-10
  • 来自专栏java和python

    springboot实现下载附件

    } 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(

    67820编辑于 2022-05-28
  • 来自专栏闵开慧

    hadoop压缩与解压

    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

    2K80发布于 2018-03-30
  • 来自专栏eadela

    servlet--获取类路径下资源

    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); }

    1.4K30发布于 2019-09-29
  • 来自专栏WindCoder

    FTP文件上传下载

    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); // 接收来自服务器的完成答复并验证整个事务是否成功

    2.9K41发布于 2020-01-21
  • 来自专栏码匠的流水账

    重复消费input stream的方法

    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一下

    70510发布于 2018-09-17
  • 来自专栏沁溪源

    【File操作】Java实现导出ZIP压缩包

    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

    4.2K41发布于 2020-11-04
  • 来自专栏开源部署

    HDFS——如何从HDFS上读取文件内容

    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);   }  } }

    4K10编辑于 2022-06-30
  • 来自专栏cwl_Java

    快速学习-HDFS客户端操作

    (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)合并文件

    73910发布于 2020-02-19
  • 来自专栏无题~

    HDFS之自定义上传、下载文件的大小

    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); } }

    71120发布于 2020-07-10
  • 来自专栏从入门到出门

    java压缩文件夹工具类

    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

    1.4K21编辑于 2023-11-30
  • 来自专栏web编程技术分享

    如何用JAVA的IO流下载落网音乐?

    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 {

    1.1K50发布于 2018-05-17
领券