日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
Java讀取大文件方法

需求:實(shí)際開發(fā)中讀取文本文件的需求還是很多,如讀取兩個(gè)系統(tǒng)之間FTP發(fā)送文件,讀取后保存到數(shù)據(jù)庫中或日志文件的數(shù)據(jù)庫中保存等。

目前成都創(chuàng)新互聯(lián)已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、武定網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

為了測(cè)試首先利用數(shù)據(jù)庫SQL生成大數(shù)據(jù)文件。

規(guī)則是 編號(hào)|姓名|手機(jī)號(hào),如 10|張10|13900000010

利用下面語句可以生成10,000,000條數(shù)據(jù)。

SELECT LEVEL||'|'||'張'||LEVEL||'|'||(13900000000+LEVEL)  FROM DUAL CONNECT BY LEVEL < 1000000;

實(shí)現(xiàn)如下:

  
 
  1. package com.test.common.util; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileNotFoundException; 
  7. import java.io.FileReader; 
  8. import java.io.IOException; 
  9. import java.util.Scanner; 
  10.  
  11. import org.apache.commons.io.FileUtils; 
  12. import org.apache.commons.io.LineIterator; 
  13.  
  14. public class HandleTextFile { 
  15.      
  16.     // 使用commons-io.jar包的FileUtils的類進(jìn)行讀取 
  17.     public static void readTxtFileByFileUtils(String fileName) { 
  18.         File file = new File(fileName); 
  19.         try { 
  20.             LineIterator lineIterator = FileUtils.lineIterator(file, "UTF-8"); 
  21.             while (lineIterator.hasNext()) { 
  22.                 String line = lineIterator.nextLine(); 
  23.                 System.out.println(line); 
  24.             } 
  25.         } catch (IOException e) { 
  26.             e.printStackTrace(); 
  27.         } 
  28.     } 
  29.      
  30.     // 使用Scanner進(jìn)行讀取 
  31.     public static void readTxtByScanner(String fileName) { 
  32.         FileInputStream fileInputStream = null;  
  33.         Scanner scanner = null; 
  34.          
  35.         try { 
  36.             fileInputStream = new FileInputStream(fileName); 
  37.             scanner = new Scanner(fileInputStream, "UTF-8"); 
  38.             while (scanner.hasNext()) { 
  39.                 String line = scanner.nextLine(); 
  40.                 System.out.println(line); 
  41.             } 
  42.         } catch (FileNotFoundException e) { 
  43.             e.printStackTrace(); 
  44.         } finally { 
  45.             if (fileInputStream != null) { 
  46.                 try { 
  47.                     fileInputStream.close(); 
  48.                 } catch (IOException e) { 
  49.                     e.printStackTrace(); 
  50.                 } 
  51.             } 
  52.             if (scanner != null) { 
  53.                 scanner.close(); 
  54.             } 
  55.         } 
  56.          
  57.     } 
  58.  
  59.     // 使用cache進(jìn)行讀取 
  60.     public static void readTxtByStringBuffer(String fileName) throws IOException { 
  61.         File file = new File(fileName); 
  62.          
  63.         BufferedReader reader = null; 
  64.          
  65.         try { 
  66.             reader = new BufferedReader(new FileReader(file), 10 * 1024 * 1024); 
  67.             String stringMsg = null; 
  68.             while ((stringMsg = reader.readLine()) != null) { 
  69.                 System.out.println(stringMsg); 
  70.             } 
  71.             reader.close(); 
  72.         } catch (FileNotFoundException e) { 
  73.             e.printStackTrace(); 
  74.         }  
  75.     } 
  76.      
  77.     public static void main(String[] args) { 
  78.         try { 
  79.             HandleTextFile.readTxtByStringBuffer("D:\\test\\customer_info.txt"); 
  80.         } catch (IOException e) { 
  81.             e.printStackTrace(); 
  82.         } 
  83.     } 

 參考文件:讀取大文件性能測(cè)試


當(dāng)前文章:Java讀取大文件方法
瀏覽路徑:http://www.5511xx.com/article/djjjsio.html