简体   繁体   English

Java库,例如org.apache.commons.io.FileUtils,但包含实例方法而不是静态方法

[英]Java library like org.apache.commons.io.FileUtils but contains instance methods instead of static methods

I was wondering what is the point of only having static methods in FileUtils? 我想知道在FileUtils中仅使用静态方法有什么意义? Is there a reason not to have instance methods? 是否有没有实例方法的理由? I believe FileUtils is thread-safe (Correct me if I am wrong lol), but I do have a bunch of threads using the FileUtils methods at the same time, and it seems like having multiple instances would work better than having synchronized code. 我相信FileUtils是线程安全的(如果我错了,请纠正我,大声笑),但是我确实有很多线程同时使用FileUtils方法,而且似乎有多个实例比同步代码更好。 Is there a library that mimics the FileUtils library, but does not contain any static methods? 是否有一个模仿FileUtils库但不包含任何静态方法的库?

I was wondering what is the point of only having static methods in FileUtils? 我想知道在FileUtils中仅使用静态方法有什么意义? Is there a reason not to have instance methods? 是否有没有实例方法的理由?

Because FileUtils is a collection of stateless methods. 因为FileUtils是无状态方法的集合。 It wouldn't make sense to introduce an object for it - what would it contain? 为此引入一个对象是没有意义的-它包含什么?

I believe FileUtils is thread-safe. 我相信FileUtils是线程安全的。

It all depends on what and how you're trying to achieve sth. 这完全取决于您要达到的目标以及目标。 If you eg try to read from multiple or even the same file in multiple threads it'll work fine. 如果您尝试从多个线程中读取多个甚至同一文件,则可以正常工作。 However if you try eg to write to the same file from within multiple threads you may have some problems. 但是,如果您尝试从多个线程中写入同一文件,则可能会遇到一些问题。 These problems will stem however from the fact that you're using the same file for writing things and thus do not actually make FileUtils not thread-safe. 但是,这些问题将源于以下事实:您使用同一文件来编写内容,因此实际上并未使FileUtils线程安全的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Servlet中改善org.apache.commons.io.FileUtils copyInputStreamToFile - How to improve org.apache.commons.io.FileUtils copyInputStreamToFile in Servlet org.apache.commons.io.FileUtils-更改文本编码 - org.apache.commons.io.FileUtils - change text encodig 引起原因:java.lang.ClassNotFoundException:org.apache.commons.io.FileUtils,maven pom.xml wa - Caused by: java.lang.ClassNotFoundException: org.apache.commons.io.FileUtils, maven pom.xml wa 我有导入org.apache.commons.io.FileUtils; 在我的代码中,它适用于eclipse但不适用于命令行 - I have import org.apache.commons.io.FileUtils; in my code and it works on eclipse but not in command line 使用org.apache.commons.io.FileUtils时Android内存泄漏 - Android memory leak when using org.apache.commons.io.FileUtils 使用Netbeans 8.0.2,并希望使用org.apache.commons.io.FileUtils; 怎么样? - Using Netbeans 8.0.2 and want to use org.apache.commons.io.FileUtils; how? org / apache / commons / io / FileUtils-NoClassDefFoundError - org/apache/commons/io/FileUtils - NoClassDefFoundError java.lang.NoSuchMethodError : org.apache.commons.io.FileUtils.isSymLink(Ljava/io/File;)Z - java.lang.NoSuchMethodError : org.apache.commons.io.FileUtils.isSymLink(Ljava/io/File;)Z 线程“主”中的java异常java.lang.NoClassDefFoundError:org / apache / commons / io / FileUtils - java Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils java.lang.NoSuchMethodError:org.apache.commons.io.FileUtils.readFileToByteArray - java.lang.NoSuchMethodError: org.apache.commons.io.FileUtils.readFileToByteArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM