从控制台输入项目路径输出java到本地txt

package com.bigdata.financial.xsbassessment.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author 通过控制台输入项目文件路径,实现java文件的复制
 *
 */
public class CopyManyJavaFileToTxt {
     private static ArrayList<String> filelist = new ArrayList<String>();

    public static void main(String[] args) {
        String  pathstr = inputPorjectPath();
        if(checkPath(pathstr)){
            List<String> list= getFiles(filePathProcess(pathstr));
             for(String lis:list){
                    if(BufferedReaderAndBufferedWriter(lis,"D:\\abc.txt")){
                         System.out.println("文件写出成功");
                    }else{
                         System.out.println("文件写出失败");
                    };
                }
        }else{
            inputPorjectPath();
        };

    }

    public static Boolean checkPath(String path){
        boolean flag = false;

        while(!flag){
            System.out.println("确认正确输入Y/y,取消输入N/n");
            String YesOrNo = inputYesOrNo();
            if(YesOrNo.equalsIgnoreCase("y")){
                if(filePathProcess(path).equalsIgnoreCase("no")){
                    flag = false;
                }else{
                    flag = true;                    
                };
            }else if(YesOrNo.equalsIgnoreCase("n")){
                inputPorjectPath();
            }else{
                System.out.println("不支持的操作");
                checkPath(path);
            }
        }
        return flag;
    }

    public static String inputYesOrNo(){
        Scanner input = new Scanner(System.in); 
        return input.nextLine();
    }
    public static String inputPorjectPath(){
        System.out.println("请输入你要copy的Java项目文件路径,并定位到//src目录:形如:E://Workspaces//MyEclipse//bigdata_financial_xsbassessment1.0//src");
        Scanner input = new Scanner(System.in); 
        String str = input.nextLine();
        System.out.println("你输入的路径是:"+str+"请检查输入项目路径正确");
        return str;
    };
    public static String filePathProcess(String pathstr){
         pathstr=pathstr.replaceAll("\\\\","\\//").trim();
         Pattern p = Pattern.compile("(^//.|^/|^[a-zA-Z])?:?/.+(/$)?");

         Matcher m = p.matcher(pathstr);
         if(!m.matches()){
             System.out.println("文件输入内容不正确,请输入N/n进行取消,重新输入");
             return "no";
         }else{
             return pathstr;
         }
    } 
    /**
     * 通过递归得到某一路径下所有的目录及其文件
     */
    public static List<String> getFiles(String filePath){
      File root = new File(filePath);
        File[] files = root.listFiles();
        for(File file:files){     
         if(file.isDirectory()){
              getFiles(file.getAbsolutePath());//递归调用(如果继续是目录的话)
         }else{
              if(file.getAbsolutePath().endsWith(".java")){
                  filelist.add(file.getAbsolutePath());//将文件放入到集合里面
              }
         }     
        }
        return filelist;
     }
    /**
     * 
     * @param inputFilePath
     * @param outputFilePath "D:\\abc.txt"
     * @return
     */
    public static Boolean BufferedReaderAndBufferedWriter(String inputFilePath,String outputFilePath){
        BufferedReader br = null;
        BufferedWriter bw = null;
        File file = null;//输出路径
        try {
            file = new File(outputFilePath);
            if (!file.exists() != false) {  
                try {  
                    file.createNewFile();  

                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }
            //输出路径检查
            br = new BufferedReader(new InputStreamReader(new FileInputStream(inputFilePath), "UTF-8"));//读取文件
            bw = new BufferedWriter(new FileWriter(file,true));//多个文件添加到一个文件不覆盖(FileWriter里面两个参数,第二个 参数设置为true,如果覆盖的话没有第二个参数,或者将第二个参数设置为false);
            String tempString = null;
            while ((tempString = br.readLine()) != null) {
                bw.write(tempString);//输出字符串  
                bw.newLine();//换行  
                bw.flush();  
            }
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(bw!= null){
                try{
                     bw.close();
                }catch(IOException e){
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值