nio實現文件讀取寫入數據庫或文件

1.nio實現讀取大文件,之後分批讀取寫入數據庫

2.nio實現讀取大文件,之後分批寫入指定文件


package com.ally;

import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.sql.*;

/**
 * Created by admin on 2016/6/28.
 * 1.nio分批讀取sql文件並執行插入數據庫
 * 2.讀取一個文件寫入另外文件
 */
public class TestNio {
    public static void main(String args[]) throws Exception {
        System.err.println("begin");
        long start = System.currentTimeMillis();
        int _5M = 1024 * 1024 * 5;
        File fin = new File("D:\\drug_general_info.sql");
        FileChannel fcin = new RandomAccessFile(fin, "r").getChannel();
        ByteBuffer rBuffer = ByteBuffer.allocate(_5M);
        //將文件讀取執行到數據庫
        readFileByLine(_5M, fcin, rBuffer);

        //將文件讀取並寫入另外文件
        File fout = new File("D:\\mm.sql");
        FileChannel fcout = new RandomAccessFile(fout, "rws").getChannel();
        ByteBuffer wBuffer = ByteBuffer.allocateDirect(_5M);
        saveOtherFile( _5M,  fcin, rBuffer,  fcout,  wBuffer);
        System.err.print((System.currentTimeMillis() - start) / 1000);
    }

    /**
     * 將一個文件內容寫入另外一個
     * @param bufSize
     * @param fcin
     * @param rBuffer
     * @param fcout
     * @param wBuffer
     */
    public static void saveOtherFile(int bufSize, FileChannel fcin,
                                     ByteBuffer rBuffer, FileChannel fcout, ByteBuffer wBuffer){
            Statement pst = null;
            String enterStr = "\n";
            try {
                byte[] bs = new byte[bufSize];
                StringBuilder strBuf = new StringBuilder("");
                String tempString = null;
                while (fcin.read(rBuffer) != -1) {
                    int rSize = rBuffer.position();
                    rBuffer.rewind();
                    rBuffer.get(bs);
                    rBuffer.clear();
                    tempString = new String(bs, 0, rSize);
                    int fromIndex = 0;
                    int endIndex = 0;
                    int i = 0;
                    while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
                        String line = tempString.substring(fromIndex, endIndex);
                        line = strBuf.toString() + line;
                        writeFileByLine(fcout, wBuffer, line);
                        strBuf.delete(0, strBuf.length());
                        fromIndex = endIndex + 1;

                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

    }

    /**
     * 讀文件寫入數據庫
     * @param bufSize
     * @param fcin
     * @param rBuffer
     */
    public static void readFileByLine(int bufSize, FileChannel fcin,
                                      ByteBuffer rBuffer) {
        Connection conn = null;
        Statement pst = null;
        String enterStr = "\n";
        try {
            byte[] bs = new byte[bufSize];
            StringBuilder strBuf = new StringBuilder("");
            String tempString = null;
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/doctor?useUnicode=true&characterEncoding=utf-8", "root", "root");
            pst = conn.createStatement();
            while (fcin.read(rBuffer) != -1) {

                int rSize = rBuffer.position();
                rBuffer.rewind();
                rBuffer.get(bs);
                rBuffer.clear();
                tempString = new String(bs, 0, rSize);
                int fromIndex = 0;
                int endIndex = 0;
                int i = 0;
                while ((endIndex = tempString.indexOf(enterStr, fromIndex)) != -1) {
                    String line = tempString.substring(fromIndex, endIndex);
                    line = strBuf.toString() + line;
                    strBuf.delete(0, strBuf.length());
                    fromIndex = endIndex + 1;
                    pst.addBatch(line);
                  /*  System.out.println("-----------------------");
                    System.out.println(line);
                    System.out.println("-----------------------");*/
                    if (i % 100 == 0) {
                        System.out.println("執行了:" + i);
                        if (i == 2700) {
                            System.out.println("導了:" + i);
                        }
                        int[] flag = pst.executeBatch();
                        System.out.println("結果:" + flag[0]);
                    }
                    i += 1;
                }
                // 執行批量更新
                pst.executeBatch();

                if (rSize > tempString.length()) {
                    strBuf.append(tempString.substring(fromIndex,
                            tempString.length()));
                } else {
                    strBuf.append(tempString.substring(fromIndex, rSize));
                }
                //  System.out.println(strBuf.toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void writeFileByLine(FileChannel fcout, ByteBuffer wBuffer,
                                       String line) {
        try {
            fcout.write(wBuffer.wrap(line.getBytes()), fcout.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章