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.