阅读下面的程序public class Example {public static void main(String[] args) throws Exception {// 创建File对象File file = new File("d:\\cn\\itcast");// 判断File对象对应的目录是否存在if (file.isDirectory ()) {// 获得目录下的所有文件的文件名String[] names = file.____();for (String name : names) {System.out.println(name); // 输出文件名}}}}请填写空白处的代码,使程序打印出itcast文件夹下每个文件或目录的名称。
举一反三
- 假设在E盘下的cn文件夹中有文件abc.txt,则下列代码的运行结果为 class Example { public static void main(String[] args) { File file = new File("E:\cn"); // 这是一个代表目录的File对象 if (file.exists()) { System.out.println(file.delete()); } } }
- 如果在一台 PC 机的c:\source目录运行如下代码: import java.io.*; class Path { public static void main(String[] args) throws Exception { File file = new File('Ran.test'); System.out.println(file.getAbsolutePath()); } } 则,期望的输出结果为?
- 下列程序运行后的结果可能是________。import java. io. *;public class Test { public static void main (String [] args) { File file =new File ( "hello . txt" ) ; if (file. exists () ) { file.delete () ; } else { try { file . createNewFile ( ) ; } catch (Exception e ) { e .printStackTrace ( ) ; } } }} A: 如果文件不存在 ,则程序退出 B: 如果文件已经存在 , 则程序退出 C: 如果文件已经存在,则保留该文件 D: 如果文件不存在 , 则创建该文件
- 下列程序的功能是显示用户在命令行方式下指定的任意驱动器目录,请补充程序。 import java.iO.*; public class FindDirectories { 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;<fileName.length;i+ += { File f=new 【 】 if(f.isDirectory( )) { System.out.println(f.getCanonicalPath( )); main(new String[ ] { f.getPath( ) }); } } = catch(IOException e) { e.printStackTrace( ); } = =
- 能够获得文件file.txt所在目录路径的语句是()。 A: String name=File.getParentName(”file.txt”); B: String name=(new File(”file.txt”)).getParent(); C: String name=(new File(”file.txt”)).getParentName(); D: String name=(new File(”file.txt”)).getParentFile();