曦远 (DealiAxy)一份私人的文字刊物

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 许可协议。转载请注明出处!

相关文章

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

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

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

JavaWeb开发---使用IDEA创建Servlet程序

前言 最近做项目需要,从之前熟悉的PHP和Python转到了JavaWeb,所以就有了这个笔记。资源图片都是来自网上的资源,根据自己的实际操作应用,做了总结归纳。 所需工具 JDK Tomcat IDEA 创建工程 设置工程名字: 创建完成后工程结构如下: 上面的图片是在网络上复制的,好像我用的IDEA 2017.3.2没有这个Create web.xml的选项,我也不知道为啥,难道是IDEA越更

2021-12-18 · Java · 约 1 分钟

Java开发:JDK安装和环境配置

前言 环境配置是开发中最基础的部分,不过最近有很多新入坑的同学,都在问怎么安装JDK和配置环境,索性写一篇。 这里要注意一下,在Windows平台和Linux平台下做开发是不同的,Windows平台开发Java需要自己手动配置JDK的环境变量,而大多数Linux都是不需要的,安装了OpenJDK就可以了(可能很多发行版都自带了OpenJDK),这点是比较方便的。 下载Java JDK Window

2023-02-08 · Java · 约 1 分钟

讨论

共 0 条

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

加载评论中…