栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java读取某个文件夹下的所有文件实例代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java读取某个文件夹下的所有文件实例代码

java读取某个文件夹下的所有文件

         学习java基础知识,读取文件,读取某个文件夹,需要读取所有文件,如何实现呢,看下实例代码。

实例代码:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;

public class ReadFile {
    public ReadFile() {
    }
    
    public static boolean readfile(String filepath) throws FileNotFoundException, IOException {
 try {

     File file = new File(filepath);
     if (!file.isDirectory()) {
  System.out.println("文件");
  System.out.println("path=" + file.getPath());
  System.out.println("absolutepath=" + file.getAbsolutePath());
  System.out.println("name=" + file.getName());

     } else if (file.isDirectory()) {
  System.out.println("文件夹");
  String[] filelist = file.list();
  for (int i = 0; i < filelist.length; i++) {
      File readfile = new File(filepath + "\" + filelist[i]);
      if (!readfile.isDirectory()) {
   System.out.println("path=" + readfile.getPath());
   System.out.println("absolutepath="
    + readfile.getAbsolutePath());
   System.out.println("name=" + readfile.getName());

      } else if (readfile.isDirectory()) {
   readfile(filepath + "\" + filelist[i]);
      }
  }

     }

 } catch (FileNotFoundException e) {
     System.out.println("readfile()  Exception:" + e.getMessage());
 }
 return true;
    }

    
    
    
    
    
    public static void main(String[] args) {
 try {
     readfile("e:/videos");
     // deletefile("D:/file");
 } catch (FileNotFoundException ex) {
 } catch (IOException ex) {
 }
 System.out.println("ok");
    }

}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/147238.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号