多種java讀寫文字檔實現程式

來源:互聯網
上載者:User

先看最簡單的 java按行讀取文字檔?代碼如下

 代碼如下 複製代碼

 FileReader reader = new FileReader("D:\www.111cn.net\feisanWeb\src\265_url.txt");
  BufferedReader br = new BufferedReader(reader);
  String s1 = null;
  while((s1 = br.readLine()) != null) {
  }
 br.close();
 reader.close();


下面結合資料庫來操作

然而匯入時怎麼也無法匯入,查看之後才發現,全部資料形式不帶表名

 代碼如下 複製代碼

INSERT INTO (id, stat_date, referrer, func, catid, user_type, action_stat, channel) VALUES (240092, "2012-3-6", www.111cn.net, -10, 11611, -10, 65:4.0;57:1.0;52:1.0", "-10");

INSERT INTO (id, stat_date, referrer, func, catid, user_type, action_stat, channel) VALUES(240093, "2012-3-6", "baidu.com", -1, 10587, -10, "65:273.0", "-10");


總共6萬多條;如果一個個加表名要多久呀,立即想到用程式處理一下,我們只要VALUES的值,使用sql批量插入就可以實現;

根據sql指令碼的形式,我們只需要將VALUES後面括弧後的“;”換成逗號,最終形式為

 代碼如下 複製代碼

(240092, "2012-3-6", "renren.com", -10, 11611, -10, "65:4.0;57:1.0;52:1.0", "-10"),
  (240093, "2012-3-6", "baidu.com", -1, 10587, -10, "65:273.0", "-10"),
  (240094, "2012-3-6", "baidu.com", -1, 10589, -10, "65:1040.0;57:6.0;52:10.0", "-10"),
  (240095, "2012-3-6", "qq.com", -10, 11913, -10, "65:1.0", "channelId_846"),
  (240096, "2012-3-6", "baidu.com", -1, 10581, -10, "65:1423.0;57:5.0;52:5.0", "-10"),
  (240097, "2012-3-6", "baidu.com", -1, 10583, -10, "57:20.0;52:23.0;65:2823.0", "-10"),

這樣我們只需要在轉換後的指令檔開頭加上

 代碼如下 複製代碼

INSERT INTO t_xxx (id, stat_date, referrer, func, catid, user_type, action_stat, channel)
VALUES


即可以批量插入資料庫;下面是JavaIO處理常式;

   

 代碼如下 複製代碼
public static void main(String[] args) throws Exception{
        BufferedReader in =new BufferedReader(new FileReader("D:\sql.sql"));//要讀取的文字檔
        BufferedWriter br=new BufferedWriter(new FileWriter("D:\aaa.sql"));//輸出的結果檔案
         String s = "";
         Pattern pattern = Pattern.compile(".*[)][;]");//正則匹配sql結尾
         Matcher m = null;
         int i = 1;
         while((s = in.readLine()) != null){
             m = pattern.matcher(s); 
             if(m.matches()){
                 System.out.println(i++);
                 s = s.replace(");", "),");//替換
             }
             if(!s.matches(" *")){//剔除空白行
                 br.write(s);
                 br.write("n");
             }
         }
         //關閉
        in.close();
        br.flush();
        br.close();
    }


執行速度很快,大約不到2秒;


/*
 * 簡單的讀/寫文字檔的樣本
 * 這裡包含了三個例子,即
 * 1.
 * 2.
 * 3. 將一個檔案的內容讀出來寫入另一個檔案中
 *    同時也展示了如果從輸入資料流中讀出來內容寫入輸出資料流中(僅限文字資料流)
 * 三個例子可以獨立存在,所以根據需要只看其中一個就行了。
 */

 代碼如下 複製代碼

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;


將檔案讀入到記憶體(這裡是StringBuffer)的例子
public final class AccessTextFile {

    /**
     * 1. 示範將流中的文本讀入一個 StringBuffer 中
     * @throws IOException
     */
    public void readToBuffer(StringBuffer buffer, InputStream is)
        throws IOException {
        String line;        // 用來儲存每行讀取的內容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine();       // 讀取第一行
        while (line != null) {          // 如果 line 為空白說明讀完了
            buffer.append(line);        // 將讀到的內容添加到 buffer 中
            buffer.append("n");        // 添加分行符號
            line = reader.readLine();   // 讀取下一行
        }
    }

    /**
     * 2. 示範將 StringBuffer 中的內容讀出到流中
     */


    public void writeFromBuffer(StringBuffer buffer, OutputStream os) {
        // 用 PrintStream 可以方便的把內容輸出到輸出資料流中
        // 其對象的用法和 System.out 一樣
        // (System.out 本身就是 PrintStream 對象)
        PrintStream ps = new PrintStream(os);  
        ps.print(buffer.toString());
    }


    /**
     * 3*. 從輸入資料流中拷貝內容到輸入資料流中
     * @throws IOException
     */


    public void copyStream(InputStream is, OutputStream os) throws IOException {
        // 這個讀過過程可以參閱 readToBuffer 中的注釋
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        PrintWriter writer = new PrintWriter(new OutputStreamWriter(os));
        line = reader.readLine();
        while (line != null) {
            writer.println(line);
            line = reader.readLine();
        }
        writer.flush();   
                           
    }

文字檔的操作就這些了,有需要瞭解的朋友可以參考一下下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.