08-01
18

去空行程序

import java.io.*;
import java.util.*;
import java.util.regex.*;

public class FileChooser {
    private String filePath;

    private String newFilePath;

    private File[] k;

    BufferedReader br;

    BufferedWriter bw;

    Pattern pattern;

    Matcher matcher;

    public FileChooser() {
        pattern = Pattern.compile("[' ']*");
        getFilePath();
        getAllJavaFiles();
        createFileFolder();
        createNewFiles();
    }

    public void createNewFiles() {
        System.out.println("开始创建新文件...");
        try {
            String s = null;
            for (int i = 0; i < k.length; i++) {
                br = new BufferedReader(new FileReader(
                        new File(k[i].toString())));
                bw = new BufferedWriter(new FileWriter(new File(newFilePath
                        + k[i].toString().substring(
                                k[i].toString().lastIndexOf("\\")))));
                while ((s = br.readLine()) != null) {
                    matcher = pattern.matcher(s);
                    if (!matcher.matches()) {
                        // System.out.println("yaowei"+s+"yaowei");
                        bw.append(s);
                        bw.newLine();
                    }
                }
                br.close();
                bw.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("新文件已经全部创建");
    }

    public void getAllJavaFiles() {
        File path = new File(filePath);
        myFileFilter f = new myFileFilter();
        k = path.listFiles(f);
    }

    public void createFileFolder() {
        File path = new File(filePath);
        if (path.isDirectory()) {
            System.out.println("正在当前目录下创建名为new的文件夹...");
            if (filePath.endsWith("/"))
                newFilePath = filePath + "new/";
            else
                newFilePath = filePath + "/new/";
            path = new File(newFilePath);
            if (!path.isDirectory()) {
                path.mkdir();
            } else {
                System.out.println("此文件夹已经存在,不能创建!");
                System.exit(0);
            }
            System.out.println("文件夹创建完毕");
        } else {
            System.out.println("输入路径非法,退出...");
            System.exit(0);
        }
    }

    public void getFilePath() {
        System.out.println("请输入要转换的文件夹路径");
        Scanner s = new Scanner(new BufferedInputStream(System.in));
        filePath = s.nextLine();
        System.out.println(filePath);
        s.close();

    }
}


import java.io.File;
import java.io.FileFilter;
public class ReadFile implements FileFilter {
public ReadFile() {
}

public boolean accept(File f) {
if(String.valueOf(f).endsWith(".java"))
return true;
return false;
}
public String getDescription() {
return "java Files";
}


文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 去空行 去回行
相关日志:
评论: 0 | 引用: 0 | 查看次数: 723
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 1000 字 | UBB代码 开启 | [img]标签 关闭