문제

How do you read files with metacharacters in the file name? For example:

  private void readDoc (String path) { 
   try {  
       SAXReader reader = new SAXReader();   
       this.doc = reader.read("_40208#1159SOV.xml");
 } catch(Exception e) {
       this.print(e.getMessage()); } }

I tried using:

reader.read(Pattern.quote("_40208#1159SOV.xml");

but it doesn't work. Would like a solution that does not require renaming the files. Thank you.

도움이 되었습니까?

해결책

read treats the system id as if it were a URL. # denotes an anchor in a URL so the method looks for a file named _40208 which doesnt exist. You can encode the argument

Document doc = reader.read(URLEncoder.encode("_40208#1159SOV.xml", "UTF-8"));

path to file's is getting from UI dialog (JFileChooser) which returns string

In this case the solution is even simpler: SAXReader provides an overloaded method which uses a File reference

Document doc = reader.read(filechooser.getSelectedFile());

다른 팁

I find more easy solution for this problem in my case reader.read(new File(path)); works perfect with out any URLEncoder.Hope it would help someone

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top