找回密码
 立即注册

java获取所有文件夹名称,文件名称,文件内容

2022-7-23 22:36:10 · 站长社区
  1. //输出代码
  2. public void priviewCode(String codePath) {
  3.     List<String> paths= traverseFolder1(codePath);
  4.     for(String path:paths){
  5.         File file=new File(path);
  6.         if(file.isFile()){
  7.             try {//读取指定文件路径下的文件内容
  8.                 String fileDatas = readFile(file);
  9.                 System.out.println(fileDatas);
  10. }
  11. public static List<String> traverseFolder1(String path) {
  12.     List<String> paths=new ArrayList<>();
  13.     int fileNum = 0, folderNum = 0;
  14.     File file = new File(path);
  15.     if (file.exists()) {
  16.         LinkedList<File> list = new LinkedList<File>();
  17.         File[] files = file.listFiles();
  18.         for (File file2 : files) {
  19.             if (file2.isDirectory()) {
  20.                 System.out.println("文件夹:" + file2.getAbsolutePath());
  21. //                    paths.add(file2.getAbsolutePath());
  22.                 list.add(file2);
  23.                 folderNum++;
  24.             } else {
  25.                 System.out.println("文件:" + file2.getAbsolutePath());
  26.                 paths.add(file2.getAbsolutePath());
  27.                 fileNum++;
  28.             }
  29.         }
  30.         File temp_file;
  31.         while (!list.isEmpty()) {
  32.             temp_file = list.removeFirst();
  33.             files = temp_file.listFiles();
  34.             for (File file2 : files) {
  35.                 if (file2.isDirectory()) {
  36.                     System.out.println("文件夹:" + file2.getAbsolutePath());
  37. //                        paths.add(file2.getAbsolutePath());
  38.                     list.add(file2);
  39.                     folderNum++;
  40.                 } else {
  41.                     System.out.println("文件:" + file2.getAbsolutePath());
  42.                     paths.add(file2.getAbsolutePath());
  43.                     fileNum++;
  44.                 }
  45.             }
  46.         }
  47.     } else {
  48.         System.out.println("文件不存在!");
  49.     }
  50.     System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);
  51. return paths;
  52. }
  53. /**
  54. * 获取某文件夹下的文件名和文件内容,存入map集合中
  55. * @param filePath 需要获取的文件的 路径
  56. * @return 返回存储文件名和文件内容的map集合
  57. */
  58. public static Map<String, String> getFilesDatas(String filePath){
  59.     Map<String, String> files = new HashMap<>();
  60.     File file = new File(filePath); //需要获取的文件的路径
  61.     String[] fileNameLists = file.list(); //存储文件名的String数组
  62.     File[] filePathLists = file.listFiles(); //存储文件路径的String数组
  63.     for(int i=0;i<filePathLists.length;i++){
  64.         if(filePathLists[i].isFile()){
  65.             try {//读取指定文件路径下的文件内容
  66.                 String fileDatas = readFile(filePathLists[i]);
  67.                 //把文件名作为key,文件内容为value 存储在map中
  68.                 files.put(fileNameLists[i], fileDatas);
  69.             } catch (IOException e) {
  70.                 e.printStackTrace();
  71.             }
  72.         }
  73.     }
  74.     return files;
  75. }
  76. /**
  77. * 读取指定目录下的文件
  78. * @param path 文件的路径
  79. * @return 文件内容
  80. * @throws IOException
  81. */
  82. public static String readFile(File path) throws IOException{
  83.     //创建一个输入流对象
  84.     InputStream is = new FileInputStream(path);
  85.     //定义一个缓冲区
  86.     byte[] bytes = new byte[1024];// 1kb
  87.     //通过输入流使用read方法读取数据
  88.     int len = is.read(bytes);
  89.     //System.out.println("字节数:"+len);
  90.     String str ;
  91.     StringBuilder stringBuilder = new StringBuilder();
  92.     while(len!=-1){
  93.         //把数据转换为字符串
  94.         str = new String(bytes, 0, len);
  95.         stringBuilder.append(str);
  96. //            System.out.println(str);
  97.         //继续进行读取
  98.         len = is.read(bytes);
  99.     }
  100.     //释放资源
  101.     is.close();
  102.     return stringBuilder.toString();
  103. }
复制代码


全部评论 0

  1. //输出代码
  2. public void priviewCode(String codePath) {
  3.     List<String> paths= traverseFolder1(codePath);
  4.     for(String path:paths){
  5.         File file=new File(path);
  6.         if(file.isFile()){
  7.             try {//读取指定文件路径下的文件内容
  8.                 String fileDatas = readFile(file);
  9.                 System.out.println(fileDatas);
  10. }
  11. public static List<String> traverseFolder1(String path) {
  12.     List<String> paths=new ArrayList<>();
  13.     int fileNum = 0, folderNum = 0;
  14.     File file = new File(path);
  15.     if (file.exists()) {
  16.         LinkedList<File> list = new LinkedList<File>();
  17.         File[] files = file.listFiles();
  18.         for (File file2 : files) {
  19.             if (file2.isDirectory()) {
  20.                 System.out.println("文件夹:" + file2.getAbsolutePath());
  21. //                    paths.add(file2.getAbsolutePath());
  22.                 list.add(file2);
  23.                 folderNum++;
  24.             } else {
  25.                 System.out.println("文件:" + file2.getAbsolutePath());
  26.                 paths.add(file2.getAbsolutePath());
  27.                 fileNum++;
  28.             }
  29.         }
  30.         File temp_file;
  31.         while (!list.isEmpty()) {
  32.             temp_file = list.removeFirst();
  33.             files = temp_file.listFiles();
  34.             for (File file2 : files) {
  35.                 if (file2.isDirectory()) {
  36.                     System.out.println("文件夹:" + file2.getAbsolutePath());
  37. //                        paths.add(file2.getAbsolutePath());
  38.                     list.add(file2);
  39.                     folderNum++;
  40.                 } else {
  41.                     System.out.println("文件:" + file2.getAbsolutePath());
  42.                     paths.add(file2.getAbsolutePath());
  43.                     fileNum++;
  44.                 }
  45.             }
  46.         }
  47.     } else {
  48.         System.out.println("文件不存在!");
  49.     }
  50.     System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);
  51. return paths;
  52. }
  53. /**
  54. * 获取某文件夹下的文件名和文件内容,存入map集合中
  55. * @param filePath 需要获取的文件的 路径
  56. * @return 返回存储文件名和文件内容的map集合
  57. */
  58. public static Map<String, String> getFilesDatas(String filePath){
  59.     Map<String, String> files = new HashMap<>();
  60.     File file = new File(filePath); //需要获取的文件的路径
  61.     String[] fileNameLists = file.list(); //存储文件名的String数组
  62.     File[] filePathLists = file.listFiles(); //存储文件路径的String数组
  63.     for(int i=0;i<filePathLists.length;i++){
  64.         if(filePathLists[i].isFile()){
  65.             try {//读取指定文件路径下的文件内容
  66.                 String fileDatas = readFile(filePathLists[i]);
  67.                 //把文件名作为key,文件内容为value 存储在map中
  68.                 files.put(fileNameLists[i], fileDatas);
  69.             } catch (IOException e) {
  70.                 e.printStackTrace();
  71.             }
  72.         }
  73.     }
  74.     return files;
  75. }
  76. /**
  77. * 读取指定目录下的文件
  78. * @param path 文件的路径
  79. * @return 文件内容
  80. * @throws IOException
  81. */
  82. public static String readFile(File path) throws IOException{
  83.     //创建一个输入流对象
  84.     InputStream is = new FileInputStream(path);
  85.     //定义一个缓冲区
  86.     byte[] bytes = new byte[1024];// 1kb
  87.     //通过输入流使用read方法读取数据
  88.     int len = is.read(bytes);
  89.     //System.out.println("字节数:"+len);
  90.     String str ;
  91.     StringBuilder stringBuilder = new StringBuilder();
  92.     while(len!=-1){
  93.         //把数据转换为字符串
  94.         str = new String(bytes, 0, len);
  95.         stringBuilder.append(str);
  96. //            System.out.println(str);
  97.         //继续进行读取
  98.         len = is.read(bytes);
  99.     }
  100.     //释放资源
  101.     is.close();
  102.     return stringBuilder.toString();
  103. }
复制代码


热门推荐
您需要登录后才可以回帖 立即登录
说说你的想法......
0
0
0
返回顶部