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 (
目录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 许可协议。转载请注明出处!
相关文章
在Linux-Ubuntu上配置开发JavaFx程序-(更新)
前言 不清楚什么原因,网络上关于JavaFx的中文资料很少,并且都很老,所以建议大家有兴趣学习JavaFx还是看官方文档比较好,而且貌似部分人不看好JavaFx。 关于Swing的资料倒是要多很多。我觉得挺奇怪的,从设计上来将,还是JavaFx高明一些嘛。 PS:经过近一周的折腾和探索,发现OpenJDK使用JavaFX真的好多坑~ OpenJDK 和 Oracle JDK的配置是有差别的,这里推
2022-05-27 · Java · 约 1 分钟
Maven国内镜像配置
配置maven安装目录下的conf/settings.xml。 在<mirrors>节点下,设置: mirror idaliyun-public/id mirrorOf*/mirrorOf namealiyun public/name urlhttps://maven.aliyun.com/repository/public/url /mir
2021-12-18 · Java · 约 1 分钟
深入Jar包:Gradle构建可执行jar包与访问jar包中文件夹与文件
前言 Java的跨平台功能听起来很诱人可口,号称“Write Once,Run Everywhere”,实际上是“Run Once,Debug Everywhere”... 在实际开发过程中还是会遇到各种各样的坑的,刚刚解决了一系列问题,特地写个文章总结一下。 使用Gradle构建Jar包 感谢万能的Gradle,极大提高了Java开发的生产力~ 在Gradle中生成jar包可以使用官方的插件:a
2022-05-27 · Java · 约 1 分钟
Spring-Boot:创建第一个应用
简介 Spring Boot 是一个轻量级框架,可以完成基于 Spring 的应用程序的大部分配置工作。在本教程中,将学习如何使用 Spring Boot 的 starter、特性和可执行 JAR 文件结构,快速创建能直接运行的基于 Spring 的应用程序。 本文使用IDEA作为开发工具,Gradle作为构建工具,创建一个简单的SpringBoot应用,暂时不涉及数据库的配置。 使用IDEA创建
2022-05-27 · Java · 约 1 分钟
讨论
共 0 条加载评论中…