文件名中如果有空格,如何判断该文件在硬盘上是否存在

yoken 2003-07-07 09:56:06
文件名中有空格时用java.io.File.isFile()或java.io.File.exists()检测结果不存在
...全文
779 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
XKP 2003-07-07
  • 打赏
  • 举报
回复
不知道为什么你的判断不了
不过下面这个程序在我这里是
返回
true的


import java.io.*;

public class Test {
public static void main (String args[]) {
File file = new File( "abc c.txt" );
System.out.println( file.exists() );
}
}
hxzhappy 2003-07-07
  • 打赏
  • 举报
回复
看看这个,很全面了,我测了没问题:

package jsf;

import java.util.*;
import java.io.*;

/**
* 一些常用的方法
* 创建日期:(2002-1-18 13:24:35)
* @author:yb
*/
public class FileUtil {
/**
* Replace 构造子注解。
*/
public FileUtil() {
super();
}

/**
* 文件拷贝(单个)
* 创建日期:(2002-1-24 9:52:47)
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean copy(String from, String to) {
try {
to = replace(to, "\\", "/");
String toPath = to.substring(0, to.lastIndexOf("/"));
File f = new File(toPath);
if (!f.exists())
f.mkdirs();

BufferedInputStream bin = new BufferedInputStream(new FileInputStream(
from));
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(
to));
int c;
while ( (c = bin.read()) != -1)
bout.write(c);
bin.close();
bout.close();
return true;
}
catch (Exception e) {

e.printStackTrace();
return false;
}

}

/**
* 判断文件是否存在
* 创建日期:(2002-1-24 9:19:48)
* @return boolean
* @param fileName java.lang.String
*/
public boolean exists(String fileName) {
try {
FileReader f = new FileReader(new File(fileName));
return true;
}
catch (FileNotFoundException e) {
return false;
}

}

/**
* 此处插入方法描述。
* 创建日期:(2002-1-18 13:26:53)
* @param args java.lang.String[]
*/
public static void main(String[] args) {

FileUtil u = new FileUtil();
boolean bflag;
bflag=u.exists("d:\\aa a.txt");
System.out.println(bflag);
//u.xcopy("c:\\test", "C:\\bb\\gggggggg");
}

/**
* 替换函数,性能比较强
* 创建日期:(2002-1-18 13:25:21)
* @return java.lang.String
* @param ss java.lang.String
*/
public String replace(String srcStr, String oldStr, String newStr) {
int i = srcStr.indexOf(oldStr);
StringBuffer sb = new StringBuffer();
if (i == -1)
return srcStr;
sb.append(srcStr.substring(0, i) + newStr);
if (i + oldStr.length() < srcStr.length())
sb.append(replace(srcStr.substring(i + oldStr.length(), srcStr.length()),
oldStr, newStr));
return sb.toString();
}

/**
* 目录拷贝
* 创建日期:(2002-1-24 10:10:46)
* @return boolean
* @param from java.lang.String
* @param to java.lang.String
*/
public boolean xcopy(String from, String to) {
from = replace(from, "\\", "/");
to = replace(to, "\\", "/");

if (!from.endsWith("/"))
from = from + "/";
if (!to.endsWith("/"))
to = to + "/";

File tt = new File(to);
if (!tt.exists())
tt.mkdirs();
String ss = "";
File ff = new File(from);
if (ff.isDirectory()) {
File f[] = ff.listFiles();
for (int i = 0; i < f.length; i++) {
String temp = f[i].getName();
if (f[i].isDirectory()) {

File g = new File(to + temp);
if (!g.exists())
g.mkdirs();
}
else
copy(from + temp, to + temp);
xcopy(from + temp, to + temp);
}

}

return true;
}
}
SailorK 2003-07-07
  • 打赏
  • 举报
回复
楼主
对你的问题我已经检测过了
文件名中有空格时用java.io.File.isFile()或java.io.File.exists()检测结果
是存在的!!!
可能是楼主把":"写成了";"
yoken 2003-07-07
  • 打赏
  • 举报
回复
问题解决了
我的new File(pathname)中的pathname的一部分是由数据库中读出的,
我对pathname做了encoding转换
pathname = new String(pathname.getBytes("GB2312"), "GBK");
然后用File的isFile()检测,没问题
XKP 2003-07-07
  • 打赏
  • 举报
回复
刚才又测试了一下,就是用你说的
中文里面的shift & -
我的测试结果是true···········
真不知道说什么好了

import java.io.*;
import javax.swing.*;

public class Test {
public static void main (String args[]) {
File file = new File( "abc_c.txt" );
System.out.println( file.exists() );
File file2 = new File( "efg—g.txt" );
System.out.println( file2.exists() );
}
}

JDK142 win2000
yefc 2003-07-07
  • 打赏
  • 举报
回复
你的问题是中文文件名或者是中文String如何在java源文件中表示的问题,
解决办法之一:
把中文字串放到property file中,用的时候用this.getClass().getResources()调上来
之二:
把中文字串放到.txt file中, 自己读上来
之三:
字串用\uxxxx unicode 表示



yoken 2003-07-07
  • 打赏
  • 举报
回复
——是中文输入法里shift & - 得到的
yoken 2003-07-07
  • 打赏
  • 举报
回复
sorry,问题不在空格,而在文件名含有——
yoken 2003-07-07
  • 打赏
  • 举报
回复
那个exists方法还是解决不了我的问题

62,614

社区成员

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

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