08-01
18
去空行程序
作者:Java伴侣 日期:2008-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.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";
}
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";
}
评论: 0 | 引用: 0 | 查看次数: 724
发表评论