黑马程序员技术交流社区
标题:
文件流IO问题
[打印本页]
作者:
陈欢
时间:
2012-7-19 12:50
标题:
文件流IO问题
本帖最后由 陈欢 于 2012-7-20 00:43 编辑
import java.io.*;
public class Test {
public static void main(String args[]){
if(args.length==0)args=new String[]{".."
};
try
{
File pathName =new File(args[0]);
String[] fileName =pathName.list();
for(int i=0;i<fileName.length;i++){
File f=new File (pathName.getPath(),fileName
);
if(f.isDirectory())
{
System.out.println(f.getCanonicalPath());
main(new String[]{ f.getPath()});//
这句话就是简单的获取文件路径吗,还有其他的含义吗,不太懂
}
}
}catch(IOException e)
{
e.printStackTrace();
}
}}
作者:
冯心程
时间:
2012-7-19 13:05
getPath()
将此抽象路径名转换为一个路径名字符串。
看看JDK就知道了
作者:
黄锐
时间:
2012-7-19 14:23
main方法只是个普通的成员静态方法。
if(f.isDirectory()) 这是递归调用的方法,,这种递归调用就是解决这种树状数据结构数据遍历的解决方案。上面的for循环找到一个目录下的所有文件和目录,如果遇到了一个目录,则进入这个目录中,在调用相同的方法继续遍历这个目录中的所有成员。 你可以再看看斐波那契数列的案例。比较详细。
作者:
rslheima
时间:
2012-7-19 15:44
f.getPath()
API:将此抽象路径名转换为一个路径名字符串
不过是根据你输入路径的相对路径
而getAbsolutePath()是绝对路径
作者:
王宝康
时间:
2012-7-19 16:23
首先给楼主整理了下代码
package IOStudy;
import java.io.*;
public class Test {
public static void main(String args[]){
if(args.length==0)
args=new String[]{".."};
try
{
File pathName =new File(args[0]);
String[] fileName =pathName.list();
for(int i=0;i<fileName.length;i++){
File f=new File(pathName.getPath(),fileName[i]);//这儿调用的是File的public File(File parent, String child)构造方法
//根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。
if(f.isDirectory())
{
System.out.println(f.getCanonicalPath());
main(new String[]{f.getPath()});// public String getPath()将此抽象路径名转换为一个路径名字符串。
//所得到的字符串使用默认名称分隔符来分隔名称序列中的名称。 返回: 此抽象路径名的字符串形式
}
}
}catch(IOException e)
{
e.printStackTrace();
}
}
}
复制代码
函数中调用静态方法main(),其中传入一个标识文件路径的字符串,通过for循环递归遍历目录下的文件和目录
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2