java多线程读写文件实例

Java多线程读文件:

[java]  view plain  copy
  1. package com.myjava;  
  2. import java.io.BufferedReader;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8. public class ThreadReadDemo {  
  9.   
  10.     /**Java多线程读大文件 
  11.      * @param args 
  12.      */  
  13.     public static void main(String[] args) {  
  14.         Thread t1=new Thread(new MultiThread(),"A");  
  15.         Thread t2=new Thread(new MultiThread(),"B");  
  16.         t1.start();  
  17.         t2.start();  
  18.     }  
  19.   
  20. }  
  21.   
  22.   
  23.  class MultiThread implements Runnable{   
  24.     private static BufferedReader br = null;  
  25.     private List<String> list;  
  26.       
  27.     static{  
  28.         try {  
  29.             br = new BufferedReader(new FileReader("F://ThreadDemo.txt"),10);  
  30.         } catch (FileNotFoundException e) {  
  31.             // TODO Auto-generated catch block  
  32.             e.printStackTrace();  
  33.         }  
  34.     }  
  35.       
  36.     public void run() {  
  37.         String line = null;  
  38.         int count = 0;  
  39.         while(true) {  
  40.             //System.out.println(Thread.currentThread().getName());  
  41.             this.list = new ArrayList<String>();  
  42.             synchronized(br) {  
  43.                 try {  
  44.                     while((line = br.readLine()) != null) {  
  45.                         if(count<15) {  
  46.                             list.add(line);  
  47.                             count++;  
  48.                         }else {  
  49.                             list.add(line);  
  50.                             count = 0;  
  51.                             break;  
  52.                         }  
  53.                     }  
  54.                 } catch (IOException e) {  
  55.                     e.printStackTrace();  
  56.                 }  
  57.             }  
  58.             try {  
  59.                 Thread.sleep(1);  
  60.                 display(this.list);  
  61.             } catch (InterruptedException e) {  
  62.                 e.printStackTrace();  
  63.             }  
  64.             if(line == null)  
  65.                 break;  
  66.         }  
  67.           
  68.           
  69.     }  
  70.       
  71.     public void display(List<String> list) {  
  72.         for(String str:list) {  
  73.             System.out.println(str);  
  74.         }  
  75.         System.out.println(list.size());  
  76.     }  
  77.       
  78. }  

java多线程写文件:

[java]  view plain  copy
  1. package com.myjava;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7. import java.util.concurrent.ConcurrentLinkedQueue;  
  8.   
  9. /** 
  10.  * 多线程下写文件 
  11.  *  
  12.  * @author Administrator 
  13.  *  
  14.  */  
  15. public class ThreadDemo {  
  16.   
  17.     public static void main(String[] args) {  
  18.         File file=new File("F:"+File.separator+"ThreadDemo.txt");  
  19.         try {  
  20.             FileOutputStream out=new FileOutputStream(file, true);  
  21.             ConcurrentLinkedQueue<String> queue=new ConcurrentLinkedQueue<String>();  
  22.             for(int i=0;i<10;i++){  
  23.                 new Thread(new MyThread(queue,"线程"+i+",")).start();//多线程往队列中写入数据  
  24.             }  
  25.             new Thread(new DealFile(out,queue)).start();//监听线程,不断从queue中读数据写入到文件中  
  26.             try {  
  27.                 Thread.sleep(3000);  
  28.                 if(!Thread.currentThread().isAlive()){  
  29.                     System.out.println("线程已结束");  
  30.                 }  
  31.             } catch (InterruptedException e) {  
  32.                 // TODO Auto-generated catch block  
  33.                 e.printStackTrace();  
  34.             }  
  35.               
  36.         } catch (FileNotFoundException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.           
  40.     }  
  41. }  
  42.   
  43. /** 
  44.  * 将要写入文件的数据存入队列中 
  45.  *  
  46.  * @author Administrator 
  47.  *  
  48.  */  
  49. class MyThread implements Runnable {  
  50.     private ConcurrentLinkedQueue<String> queue;  
  51.     private String contents;  
  52.   
  53.     public MyThread() {  
  54.     }  
  55.   
  56.     public MyThread(ConcurrentLinkedQueue<String> queue, String contents) {  
  57.         this.queue = queue;  
  58.         this.contents = contents;  
  59.     }  
  60.   
  61.     @Override  
  62.     public void run() {  
  63.         try {  
  64.             Thread.sleep(100);  
  65.         } catch (InterruptedException e) {  
  66.             // TODO Auto-generated catch block  
  67.             e.printStackTrace();  
  68.         }  
  69.         queue.add(contents);  
  70.     }  
  71. }  
  72. /** 
  73.  * 将队列中的数据写入到文件 
  74.  * @author Administrator 
  75.  * 
  76.  */  
  77. class DealFile implements Runnable {  
  78.     private FileOutputStream out;  
  79.     private ConcurrentLinkedQueue<String> queue;  
  80.   
  81.     public DealFile() {  
  82.     }  
  83.   
  84.     public DealFile(FileOutputStream out, ConcurrentLinkedQueue<String> queue) {  
  85.         this.out = out;  
  86.         this.queue = queue;  
  87.     }  
  88.   
  89.     @Override  
  90.     public void run() {  
  91.         synchronized (queue) {  
  92.             while (true) {  
  93.                 if (!queue.isEmpty()) {  
  94.                     try {  
  95.                         out.write(queue.poll().getBytes("UTF-8"));  
  96.                     } catch (IOException e) {  
  97.                         // TODO Auto-generated catch block  
  98.                         e.printStackTrace();  
  99.                     }  
  100.                 }  
  101.                 try {  
  102.                     Thread.sleep(100);  
  103.                 } catch (InterruptedException e) {  
  104.                     e.printStackTrace();  
  105.                 }  
  106.             }  
  107.         }  
  108.   
  109.     }  
  110.   
  111. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值