자바 가 져 오기 CSV 파일 내 보 내기

3138 단어 Java
필드:CSV 파일 내 보 내기 가 져 오기
1.CSVutils 클래스
package com.delta.gddx;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class CSVUtils {
	/**
     *   
     * 
     * @param file csv  (  +   ),csv          
     * @param dataList   
     * @return
     */
    public static boolean exportCsv(File file, List dataList){
        boolean isSucess=false;
        FileOutputStream out=null;
        OutputStreamWriter osw=null;
        BufferedWriter bw=null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out);
            bw =new BufferedWriter(osw);
            if(dataList!=null && !dataList.isEmpty()){
                for(String data : dataList){
                    bw.append(data).append("\r");
                }
            }
            isSucess=true;
        } catch (Exception e) {
            isSucess=false;
        }finally{
            if(bw!=null){
                try {
                    bw.close();
                    bw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
            if(osw!=null){
                try {
                    osw.close();
                    osw=null;
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
            if(out!=null){
                try {
                    out.close();
                    out=null;
                } catch (IOException e) {
                    e.printStackTrace();
                } 
            }
        }
        
        return isSucess;
    }
    
    /**
     *   
     * 
     * @param file csv  (  +  )
     * @return
     */
    public static List importCsv(File file){
        List dataList=new ArrayList();
        
        BufferedReader br=null;
        try { 
            br = new BufferedReader(new FileReader(file));
            String line = ""; 
            while ((line = br.readLine()) != null) { 
                dataList.add(line);
            }
        }catch (Exception e) {
        }finally{
            if(br!=null){
                try {
                    br.close();
                    br=null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
 
        return dataList;
    }
}

2,FeatureHandle 테스트 클래스:
package com.delta.gddx;

import java.io.File;
import java.util.List;

public class FeatureHandle {
	public static void main(String args[]){
		
		File file =new File("D:\\test.csv");
		List lstrs= CSVUtils.importCsv(file);
		for(String str:lstrs){
			System.out.println(str);
		}
		
		File ofile=new File("D:\\testo.csv");
		CSVUtils.exportCsv(ofile, lstrs);
	}
}

좋은 웹페이지 즐겨찾기