java實現將資源檔轉化成sql語句匯入資料庫

來源:互聯網
上載者:User

標籤:

文檔結構

注意事項:匯入文件字元格式設定需轉化成utf—8格式再進行匯出,否則匯出資料中文顯示亂碼。

java代碼

 1 package test; 2 import java.io.BufferedReader; 3 import java.io.BufferedWriter; 4 import java.io.File; 5 import java.io.FileOutputStream; 6 import java.io.FileReader; 7 import java.io.FileWriter; 8 import java.io.InputStream; 9 import java.io.OutputStreamWriter;10 public class InsertTest {11     public static void main(String[] args) throws Exception{12         FileOutputStream writerStream = new FileOutputStream("result.txt");//sql語句寫入資料檔案13         BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(14                                writerStream, "UTF-8")); 15         String[] fileArr = new String[]{"games.txt","nickLib.txt"};//讀取資源資料檔案16         BufferedReader reader = null;17         for(int k = 0;k <fileArr.length;k++){18             File file = new File(fileArr[k]);19             FileReader re = new FileReader(file); 20             reader = new BufferedReader(re);21             String str = null;22             while((str = reader.readLine())!=null){23                 String[] arr = str.split("//");24                 if(!arr[0].equals("")){25                     String type = null;26                     for(int i = 0; i<arr.length;i++){27                         arr[i] = arr[i].replaceAll("\\s*", "");28                     }29                     if(arr[0].endsWith("jsp")){30                         type = "100";31                     }32                     if(arr[0].endsWith("action")){33                         type = "101";34                     }35                     String sout = "insert into t_resource (ResourceTypeId,url,Title) values ("+type+","+"‘"+arr[0]+"‘"+","+"‘"+arr[1]+"‘"+");";36                     bw.write(sout+"\r\n");37                 }38                 39             }40             bw.write("#"+fileArr[k]+"\r\n");41             reader.close();42         }43         bw.close();44     }45 46 }

 

java實現將資源檔轉化成sql語句匯入資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.