设计源于生活。java文本读写以及设计思路-入门实战

前言:前几天在工作中,因为帮忙测试音频,而配置需要将每一个音频名字写到Txt文本里,大家都很机械一个一个的去复制粘贴文件名,于是我就趁工作之余写了这个小程序,代码很简单,希望能帮助到入门的同学。

我需要一个可以读取指定路径的下文件的操作

首先我们的目标是读取路径下的文件,所以有了初步的这份代码

      //获取文件名
   public static void getFliesName(String path){
        File file=new File(path);
         File[] files=file.listFiles();
         if (files == null){
          System.out.println("当前路径下没有文件");
          return;
         }
         for(File file2 : files){
            fliesNameList.add(file2.getAbsolutePath());
        }    
   }

但是这个代码有个问题,这个是能获取对应目录下的文件,而不是对应路径全部文件,如果我需要的是全部文件呢?

      //获取文件名
   public static void getFliesName(String path){
        File file=new File(path);
         File[] files=file.listFiles();
         if (files == null){
          System.out.println("当前路径下没有文件");
          return;
         }
         for(File file2 : files){
            fliesNameList.add(file2.getAbsolutePath());
          if (!file2.isFile()) {
            getFliesName(file2.getAbsolutePath());
    	 }
      }
   }

那么如果我又想只获取获取当前路径的文件呢?又或者我只想获取当前文件的文件名,而不是绝对路径呢?那么

我需要配置文件

 //配置基类
 static class  Options{
  //读取路径下的所有文件 0 读取路径当前层级的目录 1
  String readFile;
  //0 只获取文件名 示例 Test.java 
  //1 获取包含路径的文件名 示例 E:\work_speace\text_speace\Test2\src\Test.java
  String isUsePath;
 }

无关了说一句,名字是真滴难取,我看了很多标准书,但是还是不清楚怎么取名,所以英语还是重要。
好,有了这个2个变量的控制,我们就可以对读取时候进行自己想要的控制。
于是代码变成了这样

   //输入文本列表
      static List<String> fliesNameList = new ArrayList<String>();
      //获取文件名
   public static void getFliesName(String path){
    if (path == null || path.equals("")) {
   path = System.getProperty("user.dir");
  }
         File file=new File(path);
         File[] files=file.listFiles();
         if (files == null){
          System.out.println("当前路径下没有文件");
          return;
         }
         for(File file2 : files){
          if (options.isUsePath!=null&&options.isUsePath.equals("1")) {
            fliesNameList.add(file2.getAbsolutePath());
    }else{
           fliesNameList.add(file2.getName());
    }
           if (!file2.isFile() && options.readFile != null &&options.readFile.equals("0")) {
            getFliesName(file2.getAbsolutePath());
    }
         }
    
   }
   

那么读取文件部分就算完美解决了。最后我们在配置里面添加readPath,这样我们就可以更好的控制我们读取的路径,以及添加了fliePrefixes,用于在文本面前添加前缀

 //配置基类
 static class  Options{
 //读取文件的路径,默认为当前路径
  String readPath;
  //读取路径下的所有文件 0 读取路径当前层级的目录 1
  String readFile;
  //0 只获取文件名 示例 Test.java 
  //1 获取包含路径的文件名 示例 E:\work_speace\text_speace\Test2\src\Test.java
  String isUsePath;
  //给每个文件添加前缀
  String fliePrefixes;
 }

看到这里有朋友肯定要吐槽,根本没有涉及到文本读写。你这边标题党。
我们不慌,我这篇更重要的是写设计思路。
有了配置,我们不能每次在代码里面写,这样不方便,也不好用对吧。于是

我需要灵活的配置更改

于是就有了配置文档options.txt,文档内容如下

#读取文件的路径,默认为当前路径
readPath= 
#读取路径下的所有文件 0 读取路径当前层级的目录 1
readFile=0
# 0 只获取文件名 示例 Test.java 
# 1 获取包含路径的文件名 示例 E:\work_speace\text_speace\Test2\src\Test.java
isUsePath=0
# 给每个文件添加前缀
fliePrefixes= 

有了文档,我们就应该去读取,于是就产生了如下代码

 //获取配置
 private static Options getOptions(String path){
  Options options = new Options();
  File file = null;
  InputStreamReader inputReader = null;
  BufferedReader br = null;
  try {
   file = new File(path + "/options.txt");
   inputReader = new InputStreamReader(new FileInputStream(file),"utf-8");
   br = new BufferedReader(inputReader);
   String str;
   while ((str = br.readLine()) != null) {
    System.out.println(str);
    if (str.startsWith("#")) {
     continue;
    }else if(str.contains("=")){
                    int index = str.indexOf("=");
                    if(str.substring(0, index).trim().equals("readPath")){
                     options.readPath = str.substring(index + 1).trim();
                    }else if(str.substring(0, index).trim().equals("readFile")){
                     options.readFile = str.substring(index + 1).trim();
                    }else if(str.substring(0, index).trim().equals("isUsePath")){
                     options.isUsePath = str.substring(index + 1).trim();
                    }else if(str.substring(0, index).trim().equals("fliePrefixes")){
                     options.fliePrefixes = str.substring(index + 1).trim();
                    }
                    
    }
   }
  } catch (Exception e) {
   e.printStackTrace();
   System.out.println("获取配置失败");
  }finally{
   
    try {
     if (inputReader != null) {
      inputReader.close();
     }
     if (br != null) {
      br.close();
     }
    } catch (IOException e) {
     e.printStackTrace();
    }
  }
  
  return options;
 }

那么最后是不是应该输出一份结果?
那么

我需要一份输出文档

于是这样的代码就产生了

   //写入文本
 public static boolean writeTxt(List<String> list){
  String path = System.getProperty("user.dir");
  File file=new File(path + "/readResult.txt");
   try {
    if (file.exists()) {
     file.delete();
     file.createNewFile();
    }else{
     file.createNewFile();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    System.out.println("文本创建失败");
    return false;
   }
  FileWriter fw = null;
  try {
   fw = new FileWriter(file,true);
   for (int i = 0; i < fliesNameList.size(); i++) {
    String fliePrefixes = "";
    if (options.fliePrefixes != null) {
     fliePrefixes = options.fliePrefixes;
    }
    fw.write(fliePrefixes + fliesNameList.get(i) + "\r\n");
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   return false;
  } finally{
   if (fw != null) {
    try {
     fw.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  return true;
   }

代码部分就这么多了。很简单,后续可以打出jar,然后用批处理进行就行了,如果想要更好的体验也可以添加界面打出exe文件。都是比较简单的方式,不会的可以百度一下。

后续我打算从设计的产生,到设计模式跟大家分享下,我希望的是不仅仅只是程序员可以看的文章,而是更加通用的文章。如果大家有什么意见可以在评论区给与,感谢大家。

结语:作为程序员,我们应该少做机械的动作,把机械的动作更多的程序化,效率化。设计源于生活,代码源于设计。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值