getResourceAsStream(" Words.txt")和FileInputStream(&。/ src / package / Words.txt")之间有什么区别? [英] What is the difference between getResourceAsStream("Words.txt") and FileInputStream("./src/package/Words.txt")?

查看:105
本文介绍了getResourceAsStream(" Words.txt")和FileInputStream(&。/ src / package / Words.txt")之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个基于servlet的应用程序(客户端)。我试图在代码所在的同一个包中获取一个文本文件。我遇到的所有方法都使用 MyClass.class.getResourceAsStream(Words.txt) classLoader.getResourceAsStream(Words。 txt)获取文本文件(例如: SO1 SO2 )。但我尝试过 FileInputStream(./ src / package / Words.txt),文本文件仍然可以成功加载。

I am currently writing a servlet based application (the client side). I tried to get a text file inside the same package where the code is located. All of the methods that I have come across used either MyClass.class.getResourceAsStream("Words.txt") or classLoader.getResourceAsStream("Words.txt") to get the text file (eg: SO1, SO2). But I have tried FileInputStream("./src/package/Words.txt") and the text file can still be successfully loaded.

有什么区别?为什么鼓励方法 getResourceAsStream

What are the differences? And why is the method getResourceAsStream encouraged?

推荐答案

目前,您在开发人员工作站上,可能正在从IDE运行您的应用程序。 Tomcat碰巧是从IDE项目根目录启动的,因此使用

At the moment, you're on your developer workstation, and are probably running your app from your IDE. Tomcat happens to be started from the IDE project root directory, and thus using

new FileInputStream("./src/package/Words.txt")

允许读取项目中存储的文件 src 目录。

allows reading the file stored in your project src directory.

但这不是项目将如何在生产中运行。在生产中,您将使用shell脚本从完全不同的目录启动Tomcat服务器。并且生产服务器根本没有源项目。它只有Tomcat,war文件构成了从项目构建的工件。

But that's not how the project will be run in production. In production, you'll have a Tomcat server started from a totally different directory, using a shell script. And the production server won't have the source project at all. All it will have is Tomcat, and the war file constituting the artifact built from the project.

因此根本没有 src 目录,文件 Words.txt 甚至不在文件系统的任何地方。它只会输入war文件(实际上是一个zip文件),位于 WEB-INF / classes / package 下以及 .class 编译器从Java源文件生成的文件。

So there will be no src directory at all, and the file Words.txt won't even be anywhere on the file system. It will only be en entry of the war file (which is in fact a zip file), located under WEB-INF/classes/package along with the .class files produced by the compiler from your Java source files.

因此,为了能够读取该文件,您无法使用文件IO:文件系统中不存在文件。您需要使用将在war文件中找到文件的ClassLoader并从那里加载它。

So, in order to be able to read that "file", you can't use file IO: the "file" doesn't exist in the file system. You need to use the ClassLoader that will locate the "file" inside the war file and load it from there.

在开发期间,当应用程序是从爆炸的war结构中运行:类加载器将在IDE使用的目标目录下找到类来存储类文件和资源文件。

That will also go fine during development, when the app is run from an exploded war structure: the class loader will find the class under the target directory used by your IDE to store the class files and resource files.

注意你是什么需要加载该资源,如果它在包 com.foo 并且MyClass在同一个包中,则

Note that what you need to load that resource, if it's in the package com.foo and MyClass is in that same package, is

MyClass.class.getResourceAsStream("Words.txt") 

AnyOtherOfYourClassesWhateverThePackageIs.class.getResourceAsStream("/com/foo/Words.txt") 

classLoader.getResourceAsStream("com/foo/Words.txt")

这篇关于getResourceAsStream(" Words.txt")和FileInputStream(&。/ src / package / Words.txt")之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆