2
String filepath = E:\TestCode\My Demo File\abc.xml

I am trying to create file using this file path, this file path having spaces.

FileInputStream file = new FileInputStream(new File(filePath));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);

It throws a FileNotFoundException.

Laf
  • 7,965
  • 4
  • 37
  • 52
atish shimpi
  • 4,873
  • 2
  • 32
  • 50

1 Answers1

3

When specifying Windows file paths, you have to escape the '\' character, otherwise the path specified will not be exactly what you expect. The correct way to specify the path would be:

String filepath = "E:\\TestCode\\My Demo File\\abc.xml";

Or, you can use a forward slash as the path separator, the File class will automatically convert it to the correct separator for your platform:

String filepath = "E:/TestCode/My Demo File/abc.xml";

I have added the missing quotes and semicolon that were missing from the code you provided in your original question.

Laf
  • 7,965
  • 4
  • 37
  • 52