如何从 Java 读取 Windows 和 Linux 上的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25307720/
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
How to read file on Windows and Linux from Java
提问by user2771655
I have a xml file located in D:\XML\RequestXML
and I am reading xml file in this folder from a FileReader
. In my program I hard coded the file path /XML/RequestXML/
. This works fine with the windows
environment. In windows JBoss
is in D:\jbossdistrib\jboss
.
我有一个 xml 文件D:\XML\RequestXML
,我正在从 .xml 文件中读取此文件夹中的 xml 文件FileReader
。在我的程序中,我对文件路径进行了硬编码/XML/RequestXML/
。这适用于windows
环境。在 windowsJBoss
中D:\jbossdistrib\jboss
。
I created the folder structure in linux
/usr/XML/RequestXML/
. And add the xml in to RequestXML folder. JBoss
is in /usr/jbossdistrib/jboss/
path.
我在linux
/usr/XML/RequestXML/
. 并将 xml 添加到 RequestXML 文件夹中。 JBoss
在/usr/jbossdistrib/jboss/
路径中。
But my application can not find the file specified in /XML/RequestXML/ in linux environment.
但是我的应用程序在 linux 环境中找不到 /XML/RequestXML/ 中指定的文件。
If I change the file path as /usr/XML/RequestXML/
it works in linux.
如果我更改文件路径,因为/usr/XML/RequestXML/
它在 linux 中工作。
How can I use the consistent file path in linux and windows both?
如何在 linux 和 windows 中使用一致的文件路径?
public class Controller extends HttpServlet {
private String filePath = "/XML/RequestXML/";
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String file = request.getParameter("fileName");
xml = readFile(filePath + file);
}
private String readFile(String file) {
StringBuffer fileData = new StringBuffer();
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
char[] buf = new char[1024];
int numRead=0;
while((numRead=reader.read(buf)) != -1){
String readData = String.valueOf(buf, 0, numRead);
fileData.append(readData);
buf = new char[1024];
}
reader.close();
}
catch (FileNotFoundException e) {
logger.fatal("File not found in specifid path "+ file);
}
catch (IOException e) {
logger.fatal("Error while reading the xml file");
}
return fileData.toString();
}
}
Update
更新
My question is how to set the file path without /usr/
which works fine in Windows.
If this is not possible, then do I need to use the path as /usr/XML/RequestXML/
in windows environment as well? so I have to create a folder structure like D:\usr\XML\RequestXML
in windows.
我的问题是如何设置文件路径,没有/usr/
它在 Windows 中工作正常。如果这是不可能的,那么我是否也需要使用/usr/XML/RequestXML/
Windows 环境中的路径?所以我必须像D:\usr\XML\RequestXML
在 Windows 中一样创建一个文件夹结构。
回答by Conffusion
If you know the current working directory (test it with:
如果您知道当前的工作目录(使用以下命令进行测试:
System.out.println(new File(".").getAbsolutePath());
you can hardcode a relative directory like ../../XML/RequestXML
你可以硬编码一个相对目录,比如 ../../XML/RequestXML
For the record: although this may help, I still believe you should try to solve this with a configuration parameter or by loading it as a resource available in the classpath.
记录:尽管这可能会有所帮助,但我仍然相信您应该尝试使用配置参数或将其作为类路径中可用的资源加载来解决此问题。
回答by SparkOn
I don't want two paths
I don't want two paths
ok then put your the file in the resources folder of your application and try reading it this way
好的,然后将您的文件放在应用程序的资源文件夹中并尝试以这种方式阅读
private String filePath = className.getClass()
.getResource("yourFileName").getPath();
BufferedReader reader = new BufferedReader(new FileReader(filePath));
回答by Joop Eggen
First a bit of bad news: FileReader is a utility class that as default uses the platform encoding: non-portable. As the encoding is defined in the XML source itself, you might keep to InputStream if possible.
首先是一个坏消息: FileReader 是一个实用程序类,默认情况下使用平台编码:non-portable。由于编码是在 XML 源本身中定义的,如果可能,您可以保留 InputStream。
You could keep the XML as read-only resource insidethe war/ear.
你可以保留XML作为只读资源中的War/耳朵。
Or as read-only resource in the jboss directories, outsidethe application. Using as java resource via the system ClassLoader of jboss.
或者作为应用程序外部jboss 目录中的只读资源。通过jboss的系统ClassLoader作为java资源使用。
Or as file, where the path is configured as above. Maybe in an existing properties/xml configuration file. You could also use the jboss admin console to configure a path.
或作为文件,其中路径配置如上。也许在现有的 properties/xml 配置文件中。您还可以使用 jboss 管理控制台来配置路径。
Maybe of interest:
也许感兴趣:
System.getProperty("file.encoding"); // The default encoding
System.getProperty("user.name"); // Under which user are we running
System.getProperty("user.home"); // The user's home
System.getProperty("user.dir"); // The applications working dir
JBoss also defines a couple of things; but that would be non-portable.
JBoss 还定义了一些东西;但这将是不可移植的。
回答by DavisTasar
The issue isn't easy to solve because of the fundamental differences in the file systems. (Edit: Ignore me, I'm clearly on Cough Medicine. As Djon pointed out below).
由于文件系统的根本差异,这个问题并不容易解决。(编辑:忽略我,我显然在服用咳嗽药。正如 Djon 在下面指出的那样)。
Windows\Uses\Folder\Structures\Like\This.txt
Windows\Uses\Folder\Structures\Like\This.txt
Linux/Uses/Folder/Structures/Like/This.txt
Linux/Uses/Folder/Structures/Like/This.txt
So, the only way to handle this accordingly is to detect the operating system it runs on first, and then build your file paths accordingly.
因此,相应地处理此问题的唯一方法是首先检测它运行的操作系统,然后相应地构建文件路径。
See this question for more details:
有关更多详细信息,请参阅此问题:
How do I programmatically determine operating system in Java?
回答by fmcato
Hardcoding file paths is not a recommended practice. You may find a way to build the file path programmatically and use File.separator, which returns the correct one depending on the system ("\" for Windows, and "/" for UNIX/Linux/Macintosh).
不推荐使用硬编码文件路径。您可能会找到一种以编程方式构建文件路径的方法并使用 File.separator,它会根据系统返回正确的路径(“\”代表 Windows,“/”代表 UNIX/Linux/Macintosh)。