随笔 - 25, 文章 - 1, 评论 - 0, 引用 - 0
数据加载中……

[20100427]-[源码]-[Java]-[读取文件并输出]

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Main {

    
/**
     * 
@param args
     
*/

    
public static void main(String[] args) {
        
// TODO Auto-generated method stub
        Main
                .ListFilesInDirectory(
"E://WorkShop/source");
    }


    
static void ListFilesInDirectory(String path) {
        File file 
= new File(path);
        File[] files 
= file.listFiles();
        
for (File fl : files) {
            
if (fl.isDirectory())
                ListFilesInDirectory(fl.toString());
            
else {
                
if (fl.getName().endsWith("java")) {
                    System.out.println(fl.getName());

                    
try {
                        Main.ReadFile(fl);
                    }
 catch (Exception e) {
                        
// TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }

            }

        }


    }


    
public static void ReadFile(File f) throws Exception {
        BufferedReader br 
= new BufferedReader(new InputStreamReader(
                
new FileInputStream(f)));
        BufferedWriter bw 
= new BufferedWriter(new OutputStreamWriter(
                
new FileOutputStream(new File("d://output1.txt"), true)));
        String tmp 
= new String();
        
while ((tmp = br.readLine()) != null{
            
if (tmp.matches("\\s+"|| tmp.length() == 0)
                
continue;
            
if (tmp.matches("^\\s*(/\\*).*"|| tmp.matches("^\\s*\\*.*")||tmp.matches("^\\s*//.*")
                    
|| tmp.matches("^\\s+\\*/.*")){
                
continue;
            }

            bw.write(tmp);
            bw.newLine();
        }

        br.close();
        bw.close();
    }

}

posted on 2010-05-06 23:08 至尊贝贝 阅读(159) 评论(0)  编辑  收藏 所属分类: 代码_Java


只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问