Java 文件操作

package com.file.test;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @date 2015-3-17 7:52:06
 * @author y
 * @desc 
 */
public class FileTest {
    
    /**
     * 通过 BufferedReader 读取文本文件
     * 使用此种方式进进行读取文件,将会忽略回车换行等字符
     * 读取中文推荐此种方式
     * @param fileName 
     */
    public static void readFileByBufferedReader(String fileName){
        FileReader fr = null;
        BufferedReader br = null;
        StringBuilder sb = new StringBuilder();
        
        try {
            fr = new FileReader(fileName);
            br = new BufferedReader(fr);
            
            String line;
            while((line=br.readLine())!=null){
                sb.append(line);
            }
            
            System.out.println("str:"+sb.toString());
            System.out.println("str.length:"+sb.length());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                if(null!=br){
                    br.close();
                }
                if(null!=fr){
                    fr.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /**
     * 通过 BufferedInputStream 读取文件
     * 此种方式存在中文乱码问题,关键点在这里:
     * byte buffer[]=new byte[1024];  有可能中文被读取了半个字节
     * 使用ByteArrayOutputStream解决中文乱码问题
     * @param fileName 
     */
    public static void readFileByBufferedInputStream(String fileName){
        File file = new File(fileName);
        
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream baos = null;
        
        StringBuilder sb = new StringBuilder();
        
        try {
            fis = new FileInputStream(file);
            bis = new BufferedInputStream(fis);
            baos = new ByteArrayOutputStream();
            
            
            byte buffer[]=new byte[2];
            int b;
            while((b=bis.read(buffer))>0){
                baos.write(buffer, 0, b);
            }
            
            sb.append(baos.toString());
            
            System.out.println("str:"+sb.toString());
            System.out.println("str.length:"+sb.length());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
            try {
                if(null!=baos){
                    baos.close();
                }
                if(null!=bis){
                    bis.close();
                }
                if(null!=fis){
                    fis.close();
                }
            } catch (IOException ex) {
                Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
    /**
     * 创建文件
     * @param fileName 
     */
    public static void createFile(String fileName){
        try {
            File file = new File(fileName);
            
            if(!file.exists()){
                file.createNewFile();
                
                System.out.println("文件创建成功");
            }else{
                System.out.println("文件已经存在");
            }
        } catch (IOException ex) {
            Logger.getLogger(FileTest.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

 

posted @ 2015-03-17 09:23  yshy  阅读(257)  评论(0编辑  收藏  举报