Java EE 程序员不写入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1789597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java EE programmers do not write to files
提问by Volodymyr Bezuglyy
Today one person told to me that "Java EE programmers do not write to files". Why can I not write to files from within a Java EE container (for example from JBoss)? What is wrong?
今天有人告诉我“Java EE 程序员不写文件”。为什么我不能从 Java EE 容器(例如从 JBoss)写入文件?怎么了?
回答by oxbow_lakes
You should do everything within the Java EE containeritself: you can have no certainty that you will have any consistent access to the filesystem. There are many reasons for this, the most obvious being that applications running within a container will have:
您应该在 Java EE 容器本身内完成所有操作:您无法确定您将拥有对文件系统的任何一致访问权限。造成这种情况的原因有很多,最明显的是在容器内运行的应用程序将具有:
- no certainty that any subsequent invocation of an EJB would even be on the same physical serverwith access to the same files/filesystem (e.g. upon clustering)
- no possibility of interfering with each other (multiple applications attempting to write to the same file)
- no security issues (one app writing confidential data which another app could read)
- 不确定 EJB 的任何后续调用甚至会在同一物理服务器上访问相同的文件/文件系统(例如,在集群时)
- 不可能相互干扰(多个应用程序试图写入同一个文件)
- 没有安全问题(一个应用程序写入另一个应用程序可以读取的机密数据)
You should also assume that you shouldn't:
您还应该假设您不应该:
- create your own threads (the container will manage this for you; if you create your own you may starve other applications in the container of CPU time)
- use socket-IO (also has security issues)
- 创建您自己的线程(容器将为您管理;如果您创建自己的线程,您可能会占用 CPU 时间的容器中的其他应用程序)
- 使用 socket-IO(也有安全问题)
回答by ewernli
The best page to look at is this one: http://www.oracle.com/technetwork/java/restrictions-142267.html
最好看的页面是这个:http: //www.oracle.com/technetwork/java/restrictions-142267.html
It covers in detail the restrictions over the Java EE programming model.
它详细介绍了对 Java EE 编程模型的限制。
Apart from the point mentionned above Security, Portability, Clustering, Threading also consider transactions and error handling (File systems are not transactional).
除了上面提到的一点之外,安全性、可移植性、集群、线程还考虑事务和错误处理(文件系统不是事务性的)。
There is however no black magic happening in the JVM and you can create files (as long as you have the corresponding rights), use static variables, and create threads if you know what you're doing.
然而,JVM 中并没有发生什么黑魔法,如果您知道自己在做什么,您就可以创建文件(只要您拥有相应的权限)、使用静态变量和创建线程。
Better take the time to understand why these restriction are usually suggested, than to jump and write a JCA connector for the sake of being compliant.
最好花点时间了解为什么通常会建议这些限制,而不是为了兼容而跳转和编写 JCA 连接器。
回答by Bozho
Even if you have access to the filesystem, with distributed systems you can't be sure that the next time a method is called, it will be handled on the same machine where the file was written.
即使您有权访问文件系统,对于分布式系统,您也无法确定下次调用方法时,它将在写入文件的同一台机器上处理。
回答by Pascal Thivent
Per Java EE specifications, EJBs are strictly prohibited from accessing any external resources by any means other than through a "resource manager" (JDBC, JNDI, JCA, etc) and this includes especially access to the local file system through classes of the java.iopackage. Additionally, nor can a ClassLoaderbe used for such access, such as to load a properties file from the application's classpath.
根据 Java EE 规范,严格禁止 EJB 通过“资源管理器”(JDBC、JNDI、JCA 等)以外的任何方式访问任何外部资源,这尤其包括通过java.io包的类访问本地文件系统。此外,也不能ClassLoader用于此类访问,例如从应用程序的类路径加载属性文件。
Reasons for this have already been given in other answers:
其他答案中已经给出了原因:
- Security issues
- Portability issues
- Clustering issues
- Threading issues
- 安全问题
- 便携性问题
- 聚类问题
- 线程问题
Ultimately, the best resource for these matters is a database.
归根结底,处理这些问题的最佳资源是数据库。
回答by Luzifer42
You should consider a Filesystem as a Enterprise Information System (EIS). Then you can create a ResourceAdapter which accesses this EIS, similar to a JDBC adapter which accesses a database. The spec is here: http://java.sun.com/j2ee/connector/download.html. This means file access is possible, but much more complicated. This spec even allows you to create some sort of "threads" called Work.
您应该将文件系统视为企业信息系统 (EIS)。然后,您可以创建一个访问该 EIS 的 ResourceAdapter,类似于访问数据库的 JDBC 适配器。规范在这里:http: //java.sun.com/j2ee/connector/download.html。这意味着文件访问是可能的,但要复杂得多。该规范甚至允许您创建某种称为 Work 的“线程”。
回答by mP.
If your instance is not clustered or one can guarantee that all instances can use a network drive then its not really a problem to use the File apis to read/write files. However care must be taken to get paths right and clean up as appropriate. Often there are not any real needs to write files so think about it again. The main reason most people give is that in a cluster different servers wont see the same file bcause paths change and so on. In the end most many small apps dont run in such a cluster...
如果您的实例没有集群或者可以保证所有实例都可以使用网络驱动器,那么使用 File apis 读取/写入文件并不是真正的问题。但是,必须注意使路径正确并进行适当的清理。通常没有任何真正的需要写文件,所以再考虑一下。大多数人给出的主要原因是在一个集群中不同的服务器不会看到相同的文件,因为路径改变等等。最后,大多数小应用程序都不会在这样的集群中运行......
回答by Aaron Digulla
Because the Java EE spec doesn't offer an API to write files. Of course, you can just use the normal Java IO API to create files but you must make sure that this code is thread safe, that someone cleans up the files, that the file name is passed on to the next bean, that the file is migrated when your bean is moved to another node in the cluster, etc.
因为 Java EE 规范不提供用于写入文件的 API。当然,您可以只使用普通的 Java IO API 来创建文件,但您必须确保此代码是线程安全的,有人清理文件,文件名传递给下一个 bean,文件是当您的 bean 移动到集群中的另一个节点时迁移,等等。
So while you can do it, in practice, you'll encounter lots of small problems which make handling files in Java EE really hard.
因此,尽管您可以做到,但在实践中,您会遇到许多小问题,这使得在 Java EE 中处理文件变得非常困难。
回答by Angelo
In order to inter-operate with a legacy non-j2ee systems, you occasionally have to do "bad things" like socket i/o, writing to files, etc. In the best of all worlds the j2ee spec would be followed strictly, but people get away with non-j2ee stuff all the time for the sake of expediency and getting the job done. There are ways to pull these things off safely and thoughtfully.
为了与遗留的非 j2ee 系统进行互操作,您有时必须做“坏事”,例如 socket i/o、写入文件等。在最好的情况下,将严格遵循 j2ee 规范,但是为了方便和完成工作,人们一直在使用非 j2ee 的东西。有一些方法可以安全、周到地解决这些问题。

