Java:在新文件()中传递字符串变量;
我正在开发一个桌面应用程序,它使用XPath读取特定的XML元素,并将它们显示在Java:在新文件()中传递字符串变量;,java,string,file,new-operator,Java,String,File,New Operator,我正在开发一个桌面应用程序,它使用XPath读取特定的XML元素,并将它们显示在JFrame中的文本字段中 到目前为止,程序运行顺利,直到我决定在文件类中传递字符串变量 public void openNewFile(String filePath) { //file path C:\\Documents and Settings\\tanzim.hasan\\my documents\\xcbl.XML //is passed as a string from another
JFrame
中的文本字段中
到目前为止,程序运行顺利,直到我决定在文件
类中传递字符串
变量
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
传递,则不会打印预期结果,而不是编写静态路径
我在谷歌上搜索了一下,但没有找到任何具体的东西 只需使用内置对象方法:
System.out.println("file = "+xmlFile.toString());
您还可以使用:
System.out.println("f = " + f.getAbsolutePath());
此外,如果您遇到文件不存在的问题,请先检查,然后继续:
File file = new File (aPath);
if(file.exists()) {
//Do your work
}
如果像这样使用path.replaceAll(“\\”,“/”
)删除反斜杠,它将失败,因为replaceAll()
方法需要一个正则表达式作为第一个参数,而单个反斜杠(编码为“\\”
)是一个无效的正则表达式。要使用replaceAll()
,需要对反斜杠进行双转义(一次用于字符串,另一次用于正则表达式),如下path.replaceAll(“\\\\”,“/”)
但是,您不需要正则表达式!相反,请使用以下基于纯文本的方法:
path.replace("\\", "/")
请注意,名称“replace”和“replaceAll”具有误导性:“replace”仍然会替换所有出现的内容。。。决定命名为“replaceAll”的白痴应该选择“replaceRegex”或类似的名称
编辑
尝试:
现在回答这个问题已经太晚了,但是…从我的配置文件中删除“”会有所帮助 我是说 不是这个
pathVariable="c:\\some\\path\\here"
aPath
变量的值是多少?你希望它打印什么?它打印什么。没有静态和动态字符串。字符串就是字符串。重要的是字符串包含的字符。@jbniset-value of-aPath='C:\\Documents and Settings\\tanzim.hasan\\my Documents\\xcbl.XML'它是在另一个类中使用file.getAbsolutePath()捕获的;然后使用“path.replaceAll()”添加反斜杠转义字符。我期待文档doc=docBuilder.parse(xmlFile);接收有效的文件路径,以便使用DocumentBuilderFactory解析文件。但是,只有手动键入路径-File xmlFile=new File(“C:\\Documents and Settings\\tanzim.hasan\\my Documents\\xcbl.XML”)@所有-我发现了一个可能的错误,只有手动键入文件路径,该错误才会再次生效。Document doc=docBuilder.parse(xmlFile);如果xmlFile被替换为实际的文件路径,它就可以工作。但是,当它从xmlFile更改为aPath时,再次失败。仅供参考,失败不会弹出任何错误消息,它只是在需要数据的地方显示一个空屏幕。字符串中不应该有双反斜杠。因为反斜杠必须转义,所以只需要字符串文本中的斜杠,但字符串本身应该只包含一个斜杠。@JBNizet感谢您的澄清,您所说的是有意义的。然而(遗憾的是)它仍然不起作用。我删除了前一个类中的path.replaceAll(),因此现在该字符串不再是字符串文字,而是包含单个斜杠文件路径。另外,我修改了Document=docBuilder.parse(xmlFile)处的行;包括System.out.println(“@DOC”+DOC”);输出为-@DOC[#document:null]。有什么想法吗?嗨,杰森,谢谢你的信息。但是,我只插入println语句来检查局部变量接收到的实际值。我遇到的主要问题是Document=docBuilder.parse(xmlFile);没有接收到要分析的有效文件路径(或者我假设是这样)。有什么想法吗?先检查路径是否存在。为答案添加示例。1秒钟。嗨,杰森,不幸没有运气。还是和上面一样的问题。我知道该文件确实存在于文件路径中,否则catch语句会弹出一条JOptionPane错误消息。谢谢你的帮助非常感谢你的解释。我已从path.replaceAll()更改为path.replace(“\\”,“/”)。到达System.out.println(“Try&Catch之前的文件路径:+filePath”)时的输出;是-Try&Catch之前的文件路径:C:/Documents and Settings/tanzim.hasan/my Documents/xcbl.XML,这很完美,因为文件就在这里。但是,程序无法在JFrame上打印结果。我无法理解写入文件xmlFile=new文件(“C:\\Documents and Settings\\tanzim.hasan\\my Documents\\xcbl.XML”)之间有什么不同;写入文件xmlFile=新文件(aPath);两者都包含一个字符串,用于标识名为xcbl.xml的文件。啊!
pathVariable=c:\\some\\path\\here
pathVariable="c:\\some\\path\\here"