好贷网好贷款

遍历文件目录

发布时间:2016-12-5 8:34:16 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"遍历文件目录",主要涉及到遍历文件目录方面的内容,对于遍历文件目录感兴趣的同学可以参考一下。

遍历文件目录 以及读取字符串的三种方式 import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; //Read String from the screen (two valid methods) public class TraverseDirectory { public static void main(String[] args) throws Exception{ String FileName; System.out.println("input directory name:"); //method1 ---ok /* Scanner sc = new Scanner(System.in); System.out.println("input directory name:"); FileName = sc.nextLine();*/ //method2 bad way /* byte byt1[] = new byte[MAXLINELEN]; int len = System.in.read(byt1);//***NO 包括换行和回车符都读入了 System.out.println("byt1.length=" + byt1.length);// =120 //FileName = new String(byt1,0,byt1.length); No wrong should be len FileName = new String(byt1,0,len);*/ //method3 --ok BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); FileName = br.readLine(); br.close(); System.out.println("FileName:" + FileName); File f = new File(FileName); System.out.println("FileName len = " + FileName.length()); //File f = new File("F:/Movie");//********preferences******** //File f = new File("F:\\Movie");//***********ok**** Dir(f); } static int MAXLINELEN = 120; static void Dir(File f){//recursive call --> traverse the directory System.out.println("dir: " + f.getName()); File fs[] = f.listFiles(); if(f.isDirectory()){ for(File temp:fs){ if(temp.isFile()){ System.out.println("file: " + temp.getName()); } else{ Dir(temp); } } } } } 运行结果: input directory name: F:/Movie FileName:F:/Movie FileName len = 8 dir: Movie file: 02acbe2430358bfe4938b90e074cc7b598fa4f14_1_152F9490A5F03874AA8A61697A08ECEEA1811A1C.xv file: adf9efc09f10de8a194dbed77b3f51ab35b5bf09_1_363A67449032E4D2E3B58F57F29C5A82FC8B2AD6.xv dir: baidu player file: 一路X西BD国语版.rmvb file: ec7cc545bfbb38eec6535cb27c95e6b9e1b0422c_1_34043BCDD673FD230AD6E19978A5A9C5BFC3512B.xv file: How to Win Friends and Influence People.doc file: How to Win Friends and Influence People.pdf dir: VaCache file: xm_xvs.cfg dir: 《人性的弱点》MP3版9集完 file: 人性的弱点(上)01集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(上)02集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(上)03集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(上)04集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(下)01集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(下)02集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(下)03集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(下)04集{依依听书网www.yjytuan.com}.mp3 file: 人性的弱点(下)05集{依依听书网www.yjytuan.com}.mp3 file: 《人性的弱点》MP3版9集完.zip file: 【Lost in Thailand】.rmvb file: 人性的弱点英文版MP3+书B.rar file: 先王实录校注.pdf dir: 新建文件夹 file: 101973_20131205.zip.dl file: 101973_20131205.zip.dl.cfg dir: 新建文件夹 file: Thumbs.db file: 涓浗绾㈠鑱旂洘瀛︾敓缁凚缁勬笚閫忔暀绋_rar file: 说明.txt

上一篇:Wince 运行一段时间后同步出错的问题
下一篇:Unity 重力感应

相关文章

关键词: 遍历文件目录

相关评论