请大家帮我找个程序,要求对一个文件夹内所有.java文件进行去空行操作,之后把改后的文件全部储存在名为“new”的文件夹里,文件名不变。。。。。。。急 谢谢!!
应该还能用,两个文件
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 myFileFilter implements FileFilter {
public myFileFilter() {
}
public boolean accept(File f) {
if(String.valueOf(f).endsWith(".java"))
return true;
return false;
}
public String getDescription() {
return "java Files";
}
}
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 myFileFilter implements FileFilter {
public myFileFilter() {
}
public boolean accept(File f) {
if(String.valueOf(f).endsWith(".java"))
return true;
return false;
}
public String getDescription() {
return "java Files";
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2007-10-30
java.io.File file = new java.io.File("文件夹路径");
if(file.isDirectory())
{
java.io.File[] javas = file.listFiles();
for(int i=0; i<javas.length; i++)
{
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(javas[i]));
java.io.File outPut = new java.io.File("new文件夹\\"+javas[i].getName());
java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(outPut));
String text = null;
while((text = reader.readLine()) != null)
{
writer.write(text);
}
reader.close();
writer.close();
}
}
if(file.isDirectory())
{
java.io.File[] javas = file.listFiles();
for(int i=0; i<javas.length; i++)
{
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(javas[i]));
java.io.File outPut = new java.io.File("new文件夹\\"+javas[i].getName());
java.io.BufferedWriter writer = new java.io.BufferedWriter(new java.io.FileWriter(outPut));
String text = null;
while((text = reader.readLine()) != null)
{
writer.write(text);
}
reader.close();
writer.close();
}
}