• 2022-06-08
    下列程序运行后的结果可能是________。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: 如果文件不存在 , 则创建该文件
  • D

    举一反三

    内容

    • 0

      如果File对象对应的文件或目录不存在,则调用delete()方法返回false

    • 1

      若在程序中以追加写的方式打开文件file,则执行下列哪条语句?file若不存在则创建。 A: open("./file",O_RDONLY|O_CREAT|O_TRUNC) B: open("./file",O_WRONLY|O_CREAT|O_APPEND) C: open("./file"O_WRONLY|O_APPEND) D: open("./file",O_RDWR|O_CREAT|O_TRUNC)

    • 2

      File file=new file(“a.txt”);其中 a.txt这个文件必须存在。 A: 正确 B: 错误

    • 3

      下列程序的功能是显示用户在命令行方式下指定的任意驱动器目录,请补充程序。 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( ); } = =

    • 4

      中国大学MOOC: File file=new file(“a.txt”);其中 a.txt这个文件必须存在。( )