---
title: "URI文件路径-空格问题"
author: "just2learn"
date: "2014-09-16T09:51:29Z"
tags: []
categories: ["非技术"]
slug: "uri"
draft: false
lastmod: "2014-09-16T09:51:29Z"
id: 2116874
url: "https://just2learn.iteye.com/blog/2116874"
---

如C:\\Documents and Settings路径包含空格，直接传入到URI中会出错。

 

正确的做法：通过API将普通文件对象转换为URI路径。

而且，会自动给文件路径前面加上协议：file:/

 

URI文件路径与File之间的转换，将日志放到**用户目录**下：

```
public static void main(String[] args) {
	String home = System.getProperty("user.home");
	File file = new File(home, "logs");
	URI uri = file.toURI();
	String filePath = uri.toString();
	System.out.println(filePath);
	//---file:/C:/Documents%20and%20Settings/user001/logs
}
```