各们DGDJ,救急!!!! JAVA读带空格的目录里的文件时出错了!
JAVA读带空格的目录里的文件时出错了!
我把这个文件放到不带空格的目录中就没问题,请高手指教如何解决!
问题点数:50、回复次数:6Top
1 楼alps014_1()回复于 2006-03-04 20:49:30 得分 5
用trim()函数,是去除左右空格的一个有用的函数,用法:
abc=abc.trim();
这样abc就变成左右都没有空格的了Top
2 楼doway(john)回复于 2006-03-04 21:12:37 得分 1
楼主给一点点代码出来。
Top
3 楼725137(2006年不会菜)回复于 2006-03-04 21:20:50 得分 20
import java.io.*;
class Test
{
static void getDir(String strPath) throws Exception
{
try
{
File f=new File(strPath);
if(f.isDirectory())
{
File[] fList=f.listFiles();
for(int j=0;j<fList.length;j++)
{
if(fList[j].isDirectory())
{
System.out.println(fList[j].getPath());
getDir(fList[j].getPath()); //在getDir函数里面又调用了getDir函数本身
}
}
for(int j=0;j<fList.length;j++)
{
if(fList[j].isFile())
{
System.out.println(fList[j].getPath());
}
}
}
}
catch(Exception e)
{
System.out.println("Error: " + e);
}
}
public static void main(String[] args)
{
String strPath="d:\\aaa";
System.out.println(strPath);
try
{
getDir(strPath);
}
catch(Exception e)
{
}
}
}
我这个程序读空格的没有问题。你对照到看下Top
4 楼Wathking(无知者)回复于 2006-03-05 11:01:04 得分 0
比如像这样的目录
C:\Program Files\Apache Software Foundation\
读文件时就会提示找不到文件了Top
5 楼doway(john)回复于 2006-03-05 11:17:22 得分 24
import java.io.*;
public class Fio {
public static void main(String[] args) {
File f = new File("D:\\Program Files\\ComPlus Applications\\fio test.txt");
try {
BufferedReader reader = new BufferedReader(
new FileReader(f));
String line = null;
while ((line = reader.readLine()) != null)
System.out.println(line);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
这段代码经过测试了。
Top
6 楼Wathking(无知者)回复于 2006-03-05 11:27:44 得分 0
先谢过楼上两位Top




