Java: passing a string variable to a new file ();

I am developing a desktop application that reads certain XML elements using XPath and displays them in text fields in a JFrame .

While the program went smoothly, while I decided to pass the String variable in the File class.

 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) { // } } 

If I define an xmlFile with a static path (i.e. manually write it), then the program works as expected. However, instead of writing a static path, if I pass the path as an aPath string variable, it does not print the expected results.

I did a bit of searching, but did not find anything specific.

+6
source share
3 answers

Just use the built-in methods of the object:

 System.out.println("file = "+xmlFile.toString()); 

You can also use:

 System.out.println("f = " + f.getAbsolutePath()); 

Also, if you have a problem with a file that does not exist, first check, then run:

 File file = new File (aPath); if(file.exists()) { //Do your work } 
+1
source

If you use replaceAll() , like this path.replaceAll("\\", "/") to remove backslashes, it will fail because the replaceAll() method expects regex as the first parameter, and one backslash (encoded as "\\" ) is an invalid regular expression. To make it work using replaceAll() , you will need a double escape backslash (once for a string, again for a regular expression), like this path.replaceAll("\\\\", "/") .

However, you do not need regex! Instead, use the plaintext replace() method:

 path.replace("\\", "/") 

Note that the names "replace" and "replaceAll" are misleading: "replace" is still replacing all occurrences ... the moron who defined the name "replaceAll" should have chosen "replaceRegex" or something similar

Edit

Try:

 path = path.replace("\\\\", "/"); 
+1
source

It's too late to answer this, but ... removing "from my configuration file helped I mean

  pathVariable=c:\\some\\path\\here 

not this one

  pathVariable="c:\\some\\path\\here" 
0
source

Source: https://habr.com/ru/post/913136/


All Articles