从Windows和Linux读取文件 [英] read file from windows and linux from java

查看:118
本文介绍了从Windows和Linux读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个位于 D:\XML\RequestXML 中的xml文件,我正在从 FileReader 。在我的程序中,我硬编码了文件路径 / XML / RequestXML / 。这对 windows 环境可以正常工作。在windows JBoss D:\ jbossdistrib \jboss



我在 linux / usr / XML / RequestXML / 中创建了文件夹结构。并将xml添加到RequestXML文件夹中。 JBoss 位于 / usr / jbossdistrib / jboss / 路径中。



如果我将文件路径更改为,那么我们的应用程序就无法在/ XML / RequestXML / / usr / XML / RequestXML / 它可以在linux中运行。



如何在Linux和Windows中使用一致的文件路径?公共类控制器扩展HttpServlet {

私有字符串filePath =/ XML / RequestXML /; $ p

  
$ b保护void doPost(HttpServletRequest请求,
HttpServletResponse响应)抛出ServletException,IOException {

String file = request.getParameter(fileName);

xml = readFile(filePath + file);


$ b 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; ((numRead = reader.read(buf))!= -1){
String readData = String.valueOf(buf,0,numRead);
$ b $
fileData.append(readData);
buf = new char [1024];
}
reader.close();

$ b catch(FileNotFoundException e){
logger.fatal(在指定路径中找不到文件+文件);

catch(IOException e){
logger.fatal(读取xml文件时出错);
}
return fileData.toString();






更新



我的问题是如何在没有 / usr / 的情况下设置文件路径,这在Windows中可以正常工作。
如果这是不可能的,那么我是否需要在Windows环境中使用路径 / usr / XML / RequestXML / ?所以我必须在Windows中创建一个像 D:\usr\XML\RequestXML 的文件夹结构。

解决方案

如果您知道当前的工作目录(测试它:

  System.out .println(new File(。)。getAbsolutePath()); 

目录像../../ XML / RequestXML



记录:虽然这可能有帮助,但我仍然相信你应该试着用配置参数来解决这个问题,加载它作为类路径中的一个资源。


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.

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.

But my application can not find the file specified in /XML/RequestXML/ in linux environment.

If I change the file path as /usr/XML/RequestXML/ it works in linux.

How can I use the consistent file path in linux and windows both?

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.

解决方案

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

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.

这篇关于从Windows和Linux读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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