Java学习随笔3:遍历文件夹及文件的读取和写入

import java.io.File;

/**
 * 遍历文件夹
 */
public class ScannerFile {
    public static void main(String[] args) {
        // 1代表缩进,以便更好的显示
        printFiles(new File("E:\\eclipse32_workspace\\test"),1);
    }
    
    public static void printFiles(File dir,int tab){
        // 判断是否是文件夹
        if(dir.isDirectory()){
            // 将文件夹下的文件存入next
            File[] next = dir.listFiles();
            for(File file : next){
                // 分级显示
                for(int i = 0 ; i<tab-1;i++){
                    System.out.print("|--");
                }
                System.out.println(file.getName());
                
                // 嵌套显示
                if(file.isDirectory()){
                    printFiles(file,tab+1);
                }
            }
        }
    }
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

public class ReadFile {
    public static void main(String[] args) {
        // 文件读取
        File file = new File("test.txt");
        if(file.exists()){
            System.out.println("文件存在");
            
            try {
                FileInputStream fis = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
                BufferedReader br = new BufferedReader(isr);
                
                String line;
                while((line = br.readLine()) != null){
                    System.out.println(line);
                }
                br.close();
                isr.close();
                fis.close();
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch(UnsupportedEncodingException e){
                e.printStackTrace();
            } catch(IOException e){
                e.printStackTrace();
            }
        }
    
        // 文件写入(具有覆盖性)
        File newFile = new File("newtest.txt");
        try {
            FileOutputStream fos = new FileOutputStream(newFile);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            BufferedWriter bw = new BufferedWriter(osw);
            
            bw.write("长歌行 汉乐府");
            bw.write("青青园中葵,朝露待日晞\n");
            bw.write("阳春布德泽,万物生光辉\n");
            bw.write("常恐秋节至,焜黄华叶衰\n");
            bw.write("百川东到海,何时复西归\n");
            bw.write("少壮不努力,老大徒伤悲");
            
            bw.close();
            osw.close();
            fos.close();
            
            System.out.println("写入完成");
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch(UnsupportedEncodingException e){
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        }
        
    }
}

 

posted @ 2015-10-30 20:23  mengrennwpu  阅读(519)  评论(0编辑  收藏  举报