返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
Java

Apache Commons IO Best Practices

This article lists a large number of “best practices” for the Java input/output area (IO area). java.io.File Usually when you need to deal with files or filenames, there are many situations that can go wrong: A class can run on Unix but cannot run on Windows, and vice versa. Invalid filenames due to double path separators (path separator) or missing path separators (path separator). (On Windows) conforming to the Universal Naming Convention (

曦远 (DealiAxy)约 11 分钟
目录2 个章节

This article lists a large number of “best practices” for the Java input/output area (IO area).

java.io.File

Usually when you need to deal with files or filenames, there are many situations that can go wrong:

  • A class can run on Unix but cannot run on Windows, and vice versa.
  • Invalid filenames due to double path separators (path separator) or missing path separators (path separator).
  • UNC (UNC) filenames (on Windows) that conform to the Universal Naming Convention cannot work in developers’ home-grown (home-grown) utility methods
  • and so on

The above are the exact reasons why string (String) filenames do not work. Using the java.io.File class to handle the above situations would be more appropriate. Therefore our best practice (best practice) recommends using the java.io.File class instead of strings (String) to handle filenames to avoid depending on a specific platform (platform).

Commons-io 1.1 includes a class dedicated to handling filenames—the filename utility class (FilenameUtils). It handles many of the above filename problems, but we still recommend that you use java.io.File objects as much as possible.

Let’s look at an example.

public static String getExtension(String filename) {
   int index = filename.lastIndexOf('.');
   if (index == -1) {
     return "";
   } else {
     return filename.substring(index + 1);
   }
 }

Simple, right? Yes, but what happens if a developer passes (pass) a full path (full path) string rather than just a filename? Consider the following case, passing a perfectly valid path: "C:\Temp\documentation.new\README". The method (method) defined above might return (return) "new\README"—completely not the result you wanted.

Please use java.io.File instead of strings (String) to handle filenames. The functionality provided by this class is well tested (well tested). In the file utility class (FileUtils) you will find other useful utility methods (function) related to java.io.File.

For example:

String tmpdir = "/var/tmp";
 String tmpfile = tmpdir System.getProperty("file.separator") "test.tmp";
 InputStream in = new java.io.FileInputStream(tmpfile);

Change to:

File tmpdir = new File("/var/tmp");
 File tmpfile = new File(tmpdir, "test.tmp");
 InputStream in = new java.io.FileInputStream(tmpfile);

Input/Output Stream Buffering (Buffering streams)

Input/output performance depends heavily on the buffering strategy (buffering strategy). Generally speaking, buffering 512 or 1024 bytes in size to read data blocks (packets) is very fast, because this size matches very well with the size of data blocks (packets) in the hard disk (harddisk) file system (file system) or file cache (file system cache). And when you only read a few bytes, time performance will drop significantly (drop).

When reading or writing input/output streams, especially when accessing files, make sure you correctly buffer the input/output streams. You only need to use a buffered byte input stream (BufferedInputStream) to decorate (decorate) your file byte input stream (FileInputStream)

Translator’s note: Java’s input/output streams use the 修饰模式, also known as the 装饰模式:

InputStream in = new java.io.FileInputStream(myfile);
 try {
   in = new java.io.BufferedInputStream(in);
   
   in.read(.....
 } finally {
   IOUtils.closeQuietly(in);
 }

Note that you cannot cache an already buffered input/output stream. Some components, for example XML parsers (XML parser), will use their own byte input stream (InputStream) cache decoration (decorate). Passing a cached input/output stream to an XML parser (XML parser) does not have much effect but will slow down (slow down) your code performance. If you use our copy utility class (CopyUtils) or input/output utility class (IOUtils), you do not need to additionally buffer the input/output streams; treat this code as already buffered during the copy process. Check the Javadoc for more information. Other cases, such as when you write a byte array output stream (ByteArrayOutputStream) to memory (memory), do not require buffering.

This article is published with authorization from Hou Junxiong. Translation: Hou Junxiong (QQ: 2442278700) Blog: http://blog.csdn.net/houjx114

_EOF_

Apache Commons IO Best Practices

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

Spring-Boot:创建第一个应用

简介 Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作。在本教程中,将学习如何使用 Spring Boot 的 starter、特性和可执行 JAR 文件结构,快速创建能直接运行的基于 Spring 的应用程序。 本文使用IDEA作为开发工具,Gradle作为构建工具,创建一个简单的SpringBoot应用,暂时不涉及数据库的配置。 使用IDEA创建

2022-05-27 · Java · 约 1 分钟

2020年第一次用上Struts2框架做Web开发的体验

SliiyStruts2 又名 sb-struts2,因为struts真的太难用了(也许在很多年前是很好用的,但是现在看来,被其他框架秒成渣) 前言 想不到我还是得和这struts框架打交道啊,从一开始学web的时候就十分抵制这类古老,使用反人类的框架,不过为了帮女朋友做个学校的作业,还是得搞一下,然而,半小时就写好的业务代码,因为我不熟悉这个框架和Java的这套体系,调试了半天才成功run起来…

2022-06-17 · Java · 约 1 分钟

在Linux-Ubuntu上配置开发JavaFx程序-(更新)

前言 不清楚什么原因,网络上关于JavaFx的中文资料很少,并且都很老,所以建议大家有兴趣学习JavaFx还是看官方文档比较好,而且貌似部分人不看好JavaFx。 关于Swing的资料倒是要多很多。我觉得挺奇怪的,从设计上来将,还是JavaFx高明一些嘛。 PS:经过近一周的折腾和探索,发现OpenJDK使用JavaFX真的好多坑~ OpenJDK 和 Oracle JDK的配置是有差别的,这里推

2022-05-27 · Java · 约 1 分钟

JavaWeb开发---Servlet的生命周期实验

前言 Servlet是运行在服务器端的程序,它的运行状态由Servlet容器(简称容器)来维护。 通常,在容器收到客户对Servlet的请求时,容器会判断这个Servlet是否为第一次被访问,如果是第一次被访问,则会创建一个Servlet实例同时调用该实例的init()方法,进行初始化。 每个Servlet只会被创建一个实例,同时也只会被初始化一次。然后将这个实例一直保存在内存中,对所有的请求进行

2022-05-27 · Java · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…