• java学习-文件IO练习


    1.移除注释

    设计一个方法,用于移除Java文件中的注释

    效果图:

     1 package IOTest;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileReader;
     6 import java.io.FileWriter;
     7 import java.io.IOException;
     8 import java.io.PrintWriter;
     9 
    10 public class test {
    11 
    12     public static void printFile(File newF) {// 逐行打印传进来的文件对象
    13         try (FileReader fr = new FileReader(newF); BufferedReader br = new BufferedReader(fr)) {
    14             while (true) {
    15                 String str = br.readLine();
    16                 if (str == null)
    17                     break;
    18                 System.out.println(str);
    19             }
    20         } catch (IOException e) {
    21             // TODO Auto-generated catch block
    22             e.printStackTrace();
    23         }
    24     }
    25 
    26     public static void main(String[] args) {
    27 
    28         File f = new File("F:\project\javastudy\src\HelloWorld.java");// 原文件f
    29         File newF = new File("F:\project\javastudy\src\HelloWorld2.java");// 删掉含有“//”那一行之后的新建立的文件newF
    30 
    31         System.out.println("原文件内容是:");
    32         printFile(f);// 打印原文件内容
    33         try (FileReader fr = new FileReader(f);
    34                 FileWriter fw = new FileWriter(newF);
    35 
    36                 BufferedReader br = new BufferedReader(fr); // 读取每一行要用BufferedReader
    37                 PrintWriter pw = new PrintWriter(fw);) {// 写入每一行要用PrintWriter
    38             // 原文件f、fr、br;新文件newF、fw、pw
    39             while (true) {
    40                 String str = br.readLine();
    41                 if (str == null)
    42                     break;
    43                 if (!str.contains("//")) {// 含有注释,则不写入,即把含有注释的那行”删了“
    44                     pw.println(str);
    45                 }
    46 
    47             }
    48         } catch (IOException e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         }
    52         System.out.println("新文件内容是:");
    53         printFile(newF);// 打印文件内容
    54     }
    55 }

    2.自动创建类

    自动创建有一个属性的类文件。
    通过控制台,获取类名,属性名称,属性类型,根据一个模板文件,自动创建这个类文件,并且为属性提供setter和getter

    已有模板文件module.txt,内容为:

    public class @class@ {
        public @type@ @property@;
        public @class@() {
        }
        public void set@Uproperty@(@type@  @property@){
            this.@property@ = @property@;
        }
          
        public @type@  get@Uproperty@(){
            return this.@property@;
        }
    }

    注:@中间的内容是要替换的字符串@

    要求自动生成module.java,其内容应该是(举个例子):

    public class Dog {
        public String name;
        public Dog() {
        }
        public void setName(String  name){
            this.name = name;
        }
          
        public String  getName(){
            return this.name;
        }
    }

     1 package IOTest;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileReader;
     6 import java.io.FileWriter;
     7 import java.io.IOException;
     8 import java.io.PrintWriter;
     9 import java.util.Scanner;
    10 
    11 public class test6 {
    12     public static void func(String className, String attributeClass, String attributeName) {
    13         // 下面这句看似复杂,其实就是把attributeName的首字母大写,其他不变而已,(替换后settter和getter看起来顺眼一点)
    14         String upAttributeName = attributeName.substring(0, 1).toUpperCase()
    15                 + attributeName.substring(1, attributeName.length());
    16 
    17         File f = new File("d:/module.txt");
    18         File newF = new File("d:/module.java");
    19 
    20         // 逐行替换
    21         try (FileReader fr = new FileReader(f);
    22                 BufferedReader br = new BufferedReader(fr);
    23 
    24                 FileWriter fw = new FileWriter(newF);
    25                 PrintWriter pw = new PrintWriter(fw);) {
    26             while (true) {
    27                 String line = br.readLine();
    28                 if (line == null)
    29                     break;
    30                 line = line.replace("@class@", className);
    31                 line = line.replace("@type@", attributeClass);
    32                 line = line.replace("@property@", attributeName);
    33                 line = line.replace("@Uproperty@", upAttributeName);
    34                 pw.println(line);
    35             }
    36         } catch (IOException e) {
    37             // TODO Auto-generated catch block
    38             e.printStackTrace();
    39         }
    40     }
    41 
    42     public static void main(String[] args) {
    43         String className = null;
    44         String attributeClass = null;
    45         String attributeName = "";
    46 
    47         Scanner s1 = new Scanner(System.in);
    48         System.out.println("请输入类的名称:");
    49         className = s1.nextLine();
    50         System.out.println("请输入属性的类型:");
    51         attributeClass = s1.nextLine();
    52         System.out.println("请输入属性的名称:");
    53         attributeName = s1.nextLine();
    54 
    55         // 进行替换
    56         func(className, attributeClass, attributeName);
    57 
    58     }
    59 }

    3.复制文件夹

    把源文件夹下所有的文件复制到目标文件夹下(包括子文件夹)

    难点:怎么处理子文件夹->递归;怎么处理命名的问题

     1 package IOTest;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileInputStream;
     6 import java.io.FileNotFoundException;
     7 import java.io.FileReader;
     8 import java.io.FileWriter;
     9 import java.io.IOException;
    10 import java.io.PrintWriter;
    11 
    12 import javax.annotation.processing.Filer;
    13 import javax.imageio.stream.FileImageOutputStream;
    14 
    15 public class test7 {
    16     // 使用缓存流进行文件复制
    17     public static void copyFile(String srcFile, String destFile) {
    18         File f = new File(srcFile);
    19         File newF = new File(destFile);
    20 
    21         try (FileReader fr = new FileReader(f);
    22                 BufferedReader br = new BufferedReader(fr);
    23                 FileWriter fw = new FileWriter(newF);
    24                 PrintWriter pw = new PrintWriter(fw);) {
    25             // 读一行,写一行
    26             while (true) {
    27                 String line = br.readLine();
    28                 if (line == null)
    29                     break;
    30                 pw.println(line);
    31             }
    32         } catch (IOException e) {
    33             // TODO Auto-generated catch block
    34             e.printStackTrace();
    35         }
    36     }
    37 
    38     public static void copyFolder(String srcFolder, String destFolder) {
    39         File f1 = new File(srcFolder);
    40         File f2 = new File(destFolder);
    41 
    42         File[] fs = f1.listFiles();
    43         for (File f : fs) {
    44             //若f是文件夹,要先在destFolder先创建同名空文件夹,再递归调用copyFolder
    45             if (f.isDirectory()) {
    46                 File tmp = new File(destFolder + File.separator + f.getName());
    47                 tmp.mkdirs();
    48                 copyFolder(f.getAbsolutePath(), tmp.getAbsolutePath()); // 难点,递归
    49             }
    50             if (f.isFile()) {
    51                 copyFile(f.getAbsolutePath(), destFolder + File.separator + f.getName());
    52             }
    53         }
    54     }
    55 
    56     public static void main(String[] args) {
    57         File f1 = new File("D:/test");
    58         File f2 = new File("D:/test1");
    59         // 把f1文件夹下的内容全部复制到f2文件夹下
    60         copyFolder(f1.getAbsolutePath(), f2.getAbsolutePath());
    61 
    62     }
    63 }

    测试成功!

    4.查找文件内容

    遍历这个目录下所有的java文件(包括子文件夹),找出文件内容包括 Magic的那些文件,并打印出来。

    有了上面的经验,这题好做多了。

     1 package IOTest;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.File;
     5 import java.io.FileReader;
     6 import java.io.IOException;
     7 
     8 public class test8 {
     9     public static void func(String path){
    10         File f=new File(path);
    11         if(f.exists()){
    12             File []fs=f.listFiles();
    13             for(File x:fs){
    14                 if(x.isDirectory()){
    15                     func(x.getAbsolutePath());//也是递归
    16                 }
    17                 //对所有.java文件进行查找,打开每个文件查找字符串"Magic"是否在其中
    18                 if(x.isFile() && x.getName().contains(".java")){
    19                     try(
    20                             FileReader fr=new FileReader(x);
    21                             BufferedReader br=new BufferedReader(fr);
    22                             )
    23                     {
    24                         while(true){
    25                             String line=br.readLine();
    26                             if(line==null)
    27                                 break;
    28                             if(line.contains("Magic")){
    29                                 System.out.printf("找到目标字符串"Magic",在文件%s%n",x.getAbsoluteFile());
    30                             }
    31                         }
    32                     } catch (IOException e) {
    33                         // TODO Auto-generated catch block
    34                         e.printStackTrace();
    35                     }                    
    36                 }
    37             }
    38         }
    39     }
    40     public static void main(String[] args) {
    41         String path="F:\project\javastudy";
    42         func(path);
    43     }
    44 }

    效果图:

  • 相关阅读:
    迭代器设计模式
    python中的turtle库绘制图形
    Tesseract-OCR4.0识别中文与训练字库实例
    java图片处理
    Tess4J OCR简单使用教程
    C++ 排列最优解算法思想
    Swing使用JavaFXweb组件
    java专业术语
    java的分数类
    System Rules 更好的测试
  • 原文地址:https://www.cnblogs.com/gilgamesh-hjb/p/12193977.html
Copyright © 2020-2023  润新知