Java read file path space and Chinese processing

Source: Internet
Author: User

When the application is deployed, a file read error occurs, a folder name that contains spaces in the deployment path, and the application server location is migrated.

Find the following solutions online:
1, Testurl (). Class.getResource (""). GetPath () or Testurl (). Class.getResource (""). GetFile () The path obtained cannot be FileReader () and FileWriter () direct application.
The reason is that the URL is encoded with spaces, special characters (%,#,[], and so on) and Chinese.
For example: The space becomes% 20.


There is a workaround (1), after replacing with Repaceall ("%20", ""), only the space problem can be resolved. But the path contains% and Chinese is not.
There is a workaround (2), using Urldecoder.decode (str, "UTF-8") decoding, but only part of the solution, if the path contains +, also cannot be resolved, because the URL is not fully used Urlencoder.encode (str, "UTF-8 ") encoded, the + number is decoded, but it becomes a space.
Method (3), you can solve all the problems, with Testurl (). Class.getResource (""). Touri (). GetPath (), but you need to handle the urisyntaxexception exception, which is a bit more troublesome.
Source: http://blog.chinaunix.net/uid-11898547-id-3671219.html

Java read file path space and Chinese processing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.