Java: передача строковой переменной в new File();

Я разрабатываю настольное приложение, которое считывает определенные элементы XML с помощью XPath и отображает их в текстовых полях в JFrame.

До сих пор программа работала гладко, пока я не решил передать переменную Stringв класс File.

public void openNewFile(String filePath) {
    //file path C:\\Documents and Settings\\tanzim.hasan\\my documents\\xcbl.XML 
    //is passed as a string from another class.
    String aPath = filePath;

    //Path is printed on screen before entering the try & catch.
    System.out.println("File Path Before Try & Catch: "+filePath);

    try {
        //The following statement works if the file path is manually written. 
        // File xmlFile = new File ("C:\\Documents and Settings\\tanzim.hasan\\my documents\\xcbl.XML");

        //The following statement prints the actual path  
        File xmlFile = new File(aPath);
        System.out.println("file =" + xmlFile);

        //From here the document does not print the expected results. 
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        docFactory.setNamespaceAware(true);

        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(xmlFile);
        doc.getDocumentElement().normalize();

        XPath srcPath = XPathFactory.newInstance().newXPath();
        XPathShipToAddress shipToPath = new XPathShipToAddress(srcPath, doc);
        XPathBuyerPartyAddress buyerPartyPath = new XPathBuyerPartyAddress(srcPath, doc);
    } catch (Exception e) {
        //
    }
}

Если я определяю xmlFileсо статическим путем (т. е. записываю его вручную), то программа работает так, как ожидалось. Однако вместо записи статического пути, если я передам путь как строковую переменную aPath, он не выведет ожидаемых результатов.

Я немного погуглил, но не нашел ничего конкретного.

6
задан Tanzim H. 17 April 2012 в 06:05
поделиться