[Java] 如果是目录,显示该目录下的所以文件名称;如果是文件,显示文件的内容 →→→→→进入此内容的聊天室

来自 , 2019-09-21, 写在 Java, 查看 103 次.
URL http://www.code666.cn/view/2f37d101
  1.  import java.io.File;
  2.  import java.io.FileInputStream;
  3.  import java.io.IOException;
  4.  import java.io.FileNotFoundException;
  5.  import java.io.BufferedInputStream;
  6.  public class FileWork{
  7.      public static void main(String[] args){
  8.                  //File f = new File("d:"+File.separator+"FileWork.java");
  9.            File f = new File("d:"+File.separator+"Java_work");
  10.          if(f.isDirectory()){          //判断是否是文件的一个目录(文件夹)
  11.              System.out.println("路径是目录!");
  12.              String str[] = f.list();
  13.              for(int i=0;i<str.length;i++){
  14.                  System.out.println(str[i]);
  15.              }
  16.          }else if(f.isFile()){    //判断是否是标准文件
  17.              int b = 0;
  18.              FileInputStream fis =null;
  19.              BufferedInputStream bis= null;
  20.              try{
  21.                  fis = new FileInputStream(f);
  22.                  bis = new BufferedInputStream(fis);
  23.                  while((b=bis.read())!=-1){
  24.                      System.out.print((char)b);
  25.                  }
  26.              }catch(FileNotFoundException e){
  27.                  e.printStackTrace();
  28.              }
  29.              catch(IOException e){
  30.                  e.printStackTrace();
  31.              }finally{
  32.                  try{
  33.                      if(bis!=null){
  34.                          bis.close();
  35.                      }    
  36.                  }catch(IOException e){
  37.                      e.printStackTrace();
  38.                  }
  39.              }
  40.          }
  41.      }
  42.  }

回复 "如果是目录,显示该目录下的所以文件名称;如果是文件,显示文件的内容"

这儿你可以回复上面这条便签

captcha