java中文件读写使用多线程_java多线程读写文件实例

Java多线程读文件:

package com.myjava;

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

public class ThreadReadDemo {

/**Java多线程读大文件

* @param args

*/

public static void main(String[] args) {

Thread t1=new Thread(new MultiThread(),"A");

Thread t2=new Thread(new MultiThread(),"B");

t1.start();

t2.start();

}

}

class MultiThread implements Runnable{

private static BufferedReader br = null;

private Listlist;

static{

try {

br = new BufferedReader(new FileReader("F://ThreadDemo.txt"),10);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void run() {

String line = null;

int count = 0;

while(true) {

//System.out.println(Thread.currentThread().getName());

this.list = new ArrayList();

synchronized(br) {

try {

while((line = br.readLine()) != null) {

if(count<15) {

list.add(line);

count++;

}else {

list.add(line);

count = 0;

break;

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

try {

Thread.sleep(1);

display(this.list);

} catch (InterruptedException e) {

e.printStackTrace();

}

if(line == null)

break;

}

}

public void display(Listlist) {

for(String str:list) {

System.out.println(str);

}

System.out.println(list.size());

}

}

java多线程写文件:

package com.myjava;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.concurrent.ConcurrentLinkedQueue;

/**

* 多线程下写文件

*

* @author Administrator

*

*/

public class ThreadDemo {

public static void main(String[] args) {

File file=new File("F:"+File.separator+"ThreadDemo.txt");

try {

FileOutputStream out=new FileOutputStream(file, true);

ConcurrentLinkedQueuequeue=new ConcurrentLinkedQueue();

for(int i=0;i<10;i++){

new Thread(new MyThread(queue,"线程"+i+",")).start();//多线程往队列中写入数据

}

new Thread(new DealFile(out,queue)).start();//监听线程,不断从queue中读数据写入到文件中

try {

Thread.sleep(3000);

if(!Thread.currentThread().isAlive()){

System.out.println("线程已结束");

}

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

}

/**

* 将要写入文件的数据存入队列中

*

* @author Administrator

*

*/

class MyThread implements Runnable {

private ConcurrentLinkedQueuequeue;

private String contents;

public MyThread() {

}

public MyThread(ConcurrentLinkedQueuequeue, String contents) {

this.queue = queue;

this.contents = contents;

}

@Override

public void run() {

try {

Thread.sleep(100);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

queue.add(contents);

}

}

/**

* 将队列中的数据写入到文件

* @author Administrator

*

*/

class DealFile implements Runnable {

private FileOutputStream out;

private ConcurrentLinkedQueuequeue;

public DealFile() {

}

public DealFile(FileOutputStream out, ConcurrentLinkedQueuequeue) {

this.out = out;

this.queue = queue;

}

@Override

public void run() {

synchronized (queue) {

while (true) {

if (!queue.isEmpty()) {

try {

out.write(queue.poll().getBytes("UTF-8"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值