File路径问题

lgm277531070 2010-05-24 04:42:45
File file = new File(http://xxxxxx);
为什么最后输出file的路径为http:\xxxxx
File file = new File(http:////xxxxxx) 输出的路径还是http:\xxxx导致无法访问指定文件。
为什么,输出的总是“\”呢,有什么办法解决么,小弟在此先谢过大家了!

...全文
160 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wasaia 2010-05-24
  • 打赏
  • 举报
回复
public File(URI uri)通过将给定的 file: URI 转换成一个抽象路径名来创建一个新的 File 实例。
file: URI 的具体形式与系统有关,因此,由此构造方法执行的转换也与系统有关。

对于某个给定抽象路径名 f,可以保证:

new File( f.toURI()).equals( f.getAbsoluteFile())
只要原始抽象路径名、URI 和新的抽象路径名都是在同一 Java 虚拟机(或者它的不同调用)中创建的。但是,当在某一操作系统上的虚拟机中创建的 file: URI 在不同操作系统上的虚拟机中被转换成抽象路径名时,这种关系通常是不成立的。
龙四 2010-05-24
  • 打赏
  • 举报
回复
用这个构造方法


File(URI uri)
wasaia 2010-05-24
  • 打赏
  • 举报
回复
是说构造,构建File不能http schema吧(水平有限,个人理解)io当然任何url都可以了
crazylaa 2010-05-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wasaia 的回复:]

我看了一下api,File貌似只支持file的schema
[/Quote]
。。。


package test;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadFile {

public static void getSong(String _path, String _savePath) {
String savePath = _savePath;
String path = _path;
int BYTE_SIZE = 1;
int SAVE_SIZE = 1024;
byte[] buff = new byte[BYTE_SIZE]; // 每次读的缓存
byte[] save = new byte[SAVE_SIZE]; // 保存前缓存
BufferedInputStream bf = null;
FileOutputStream file;
URL url = null;
HttpURLConnection httpUrl;
try {
url = new URL(path);
httpUrl = (HttpURLConnection) url.openConnection();
System.out.println("已经打开连接....");
bf = new BufferedInputStream(httpUrl.getInputStream());
System.out.println("已经获取资源......");
file = new FileOutputStream(savePath);
System.out.println("准备保存到:" + savePath);

System.out.println("开始读入......");
int i = 0;
while (bf.read(buff) != -1) { // 一个字节一个字节读
save[i] = buff[0];
if (i == SAVE_SIZE - 1) { // 达到保存长度时开始保存
file.write(save, 0, SAVE_SIZE);
save = new byte[SAVE_SIZE];
i = 0;
} else {
i++;
}
}
// 最后这段如果没达到保存长度,需要把前面的保存下来
if (i > 0) {
file.write(save, 0, i - 1);
}
System.out.println("下载成功!!!");
httpUrl.disconnect();
file.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {

bf.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
try {
DownloadFile.getSong(
"http://wap.sonyericsson.com/UAprof/K700cR201.xml",
"D://1.xml");
DownloadFile.getSong(
"http://nds1.nds.nokia.com/uaprof/NN78-1r100.xml",
"D://2.xml");
DownloadFile.getSong("http://www.sohu.com", "D://3.xml");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
Arthur0088 2010-05-24
  • 打赏
  • 举报
回复
不会出现这样的问题吧,可以找到的,你可以用双斜杠
wasaia 2010-05-24
  • 打赏
  • 举报
回复
我看了一下api,File貌似只支持file的schema
zz250121244 2010-05-24
  • 打赏
  • 举报
回复
打错了 是C盘.不是D盘.
zz250121244 2010-05-24
  • 打赏
  • 举报
回复
比如我D盘下有一个hello.txt文件.
File file = new File("c:/hello.txt");
这样就可以访问了. 你的http://xxxxxx 不需要用 " " 引起来?
dongyangmoney 2010-05-24
  • 打赏
  • 举报
回复
http:////xxxxxx ??????

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧