0

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.

jww
  • 97,681
  • 90
  • 411
  • 885
user2771655
  • 1,052
  • 3
  • 20
  • 38
  • 2
    If the file is in `/usr/XML/RequestXML` and you're trying to read it from `/XML/RequestXML`, then it will obviously not find the file, since the two locations are not the same... Why are you hard-coding the path? – Jesper Aug 14 '14 at 12:12
  • If the paths are different you cannot hard code it for both systems. – Djon Aug 14 '14 at 12:12
  • I don't want two paths. I'm saying if I use /XML/RequetXML/ path. this does not work in linux. Linux wants /usr/XML/RequestXML/ path to work. I just want only one path which is working in both linux and windows. How can I do that? – user2771655 Aug 14 '14 at 12:14
  • post the relevant code you tried will help us to give solution of the exact scenario – SparkOn Aug 14 '14 at 12:17
  • 1
    You either need the path to be identical for both the systems or you need to tell your code that he's running one or the other. Also in `/XML/RequestXML/`, `/` is the root directory, you might have another problem here. – Djon Aug 14 '14 at 12:21

5 Answers5

2

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.

Conffusion
  • 4,335
  • 2
  • 16
  • 28
1

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));
SparkOn
  • 8,806
  • 4
  • 29
  • 34
  • Even I use it in resource file, still I have to change it when upload to linux. Because linux path starts from /usr/ But in windows there nothing like that. It use the relative path from D drive. – user2771655 Aug 14 '14 at 12:26
1

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.

You could keep the XML as read-only resource inside the war/ear.

Or as read-only resource in the jboss directories, outside the application. Using as java resource via the system ClassLoader of jboss.

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.

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • No there can be many xml files in this folder which user selects and submit. So I can not put them inside the war or ear. Also user would change the xml values time to time. – user2771655 Aug 14 '14 at 12:37
  • As Linux can have soft links (`link -s`), you have some freedom to define some common path, in Linux. – Joop Eggen Aug 14 '14 at 12:46
0

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).

Windows\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?

Community
  • 1
  • 1
DavisTasar
  • 245
  • 1
  • 4
  • 14
  • Well I'll be damned. Every time I've done something similar it's always had an issue. Just tried it in Explorer, Command Prompt, and a small script, and it worked. Thanks Djon! – DavisTasar Aug 14 '14 at 12:24
  • Yes, This is not about '/' This is about /usr/ folder matter. How to read the file relatively. In windows, the path is taking from D drive onwards. But in linux it requre /usr/ additionally. My issue is how to resolve this. – user2771655 Aug 14 '14 at 12:24
  • Uh well, I was talking about Java, but I also tested it and in fact you're right, so I've learn something today... – Djon Aug 14 '14 at 12:25
0

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).

fmcato
  • 172
  • 1
  • 12
  • 2
    Not necessarily, Java will take care of that and using `/` on windows works perfectly. – Djon Aug 14 '14 at 12:23