Java路径问题解决方案:[1]基础篇

 时间:2024-11-20 15:03:16

解决JAVA开发中遇到的路径问题,从而快速的从事其他工作任务

工具/原料

JAVA JDK版本jdk1.7.0_51

Eclipse加部分应用插件

windows 7 操作系统

Java路径特殊字符

1、import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class URLTest { public static void main(String[] args) { String urlPath = URLTest.class.getResource("/").getPath(); System.out.println(urlPath); urlPath = URLTest.class.getResource("/").getFile(); System.out.println(urlPath); urlPath = URLTest.class.getClassLoader().getResource("").getPath(); System.out.println(urlPath); urlPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); System.out.println(urlPath); try { FileWriter writer = new FileWriter("D:/test.txt"); writer.write("success\n"); writer.flush(); writer.close(); FileReader read = new FileReader("D:/test.txt"); int i = 1; while((i = read.read())!=-1) { System.out.print((char)i); } read.close(); /**下面读写报错 **/ read = new FileReader(urlPath); writer = new FileWriter(urlPath); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}上述获取路径的方式不能被FileReader()和FileWriter()直接应用因为URL对空格、特殊字符(%,#,[]等)以及中文进行了编码处理;必须以"/"开头来获取路径:URLTest.class.getResource("/").getPath();不用加"/"可以来获取路径:URLTest.class.getClassLoader().getResource("").getPath();控制台输出如下图所示:

Java路径问题解决方案:[1]基础篇

2、import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class URLTest { public static void main(String[] args) { String urlPath = URLTest.class.getResource("/").getPath(); System.out.println(urlPath); try { urlPath = urlPath.replaceAll("%20"," ")+ "URLTest.class"; FileReader read = new FileReader(urlPath); int i = 1; while((i = read.read())!=-1) { System.out.print((char)i); } read.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}1、repaceAll("%20",' ')替换,只能解决空格问题,路径中包含其他特殊字符或者中文是不能够解决问题的;2、URLDecoder.decode(str,"UTF-8")解码只能解决部分部分;如果路径中含有“+”是不能解决的,URL不是完全支持URLEncoder.encode(str,"UTF-8")编码的,如果转码“+”会变成"空格";3、URLTest.class.getClassLoader().getResource("").toURI().getPath();可以解决目前存在的问题,但需处理URISyntaxException异常。控制台输出如下图所示(虽然都是乱码):

Java路径问题解决方案:[1]基础篇

3、import java.net.MalformedURLException;import java.荏鱿胫协net.URI;import java.net.URISyntaxException;import java.net.URL;public class URLTest { public static void main(String[] args) { try { URL url = new URL("file://E:/test.txt"); System.out.println(url); URI uri = new URI("file://E:/test.txt"); System.out.println(uri); String uriPath = Thread.currentThread().getContextClassLoader() .getResource("").toURI().getPath(); System.out.println("以“空格”的形式显示" + uriPath); uriPath = Thread.currentThread().getContextClassLoader() .getResource("").toURI().toString(); System.out.println("以“%20”的形式显示" + uriPath); } catch (MalformedURLException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } }}new URL();的参数可以为正确的URI,或者为URI格式的字符串;若字符串是非完整的URI格式,则创建失败;java.net.URI以“%20”的形式显示Thread.currentThread().getContextClassLoader().getResource("").toURI().toString();以“空格”的形式显示Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath();java.net.URL返回的一切路径中的空格都是以“%20”的形式出现。URL/URI返回的路径分隔符都是“/”(控制台输出"/")控制台输出如下图所示:

Java路径问题解决方案:[1]基础篇

4、import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.net.URI;public class URLTest { public static void main(String[] args) { File file = new File("D:/test.txt"); System.out.println(file.toURI()); System.out.println(file.toURI().getPath()); try { FileReader reader = new FileReader(file); System.out.println(reader); /**接受正确URI格式的参数和带“空格”(非%20)的正确相对/绝对字符串路径**/ URI uri = file.toURI(); file = new File(uri.getPath()); reader = new FileReader(file); System.out.println(reader); } catch (FileNotFoundException e) { e.printStackTrace(); } }}new File(String filePath);接受正确URI格式的参数和带“空格”(非%20)的正确相对/绝对字符串路径,否则会报找不到文件异常;new File(String filePath);返回的路径都是以“\”分隔的,空格还是以"空格"形式显示;new File(String filePath);是空格则getPath()返回的仍是空格,是“%20”的仍是“%20”,不会存在改变的问题;new File(String filePath).toURI();会将file的路径名中的“空格”转化为“%20”,然后在路径前加“file:/”,new File(String filePath)..toURI().getPath();则会取掉“file:”。控制台输出如下图所示:

Java路径问题解决方案:[1]基础篇
  • mysql安装后修改相关配置
  • 商务谈判时如何把握客户心理
  • 怎么设置电脑回收站的最大容量?
  • HTML div嵌套 margin不起作用
  • 玫瑰花如何用电脑画图软件画
  • 热门搜索
    什么是可转债 订单号是什么 本科毕业是什么学位 软件打不开是什么原因 id是什么软件 睡觉做梦是什么原因 临官是什么意思 happy是什么意思 fl是什么意思 ean是什么意思