当前位置:首页 » 文件管理 » java多线程写文件夹

java多线程写文件夹

发布时间: 2022-09-04 04:41:35

java多线程写文件,同步问题。

说下我的想法吧:编写一个类,这个类的功能是用多线程将多个数据流按顺序缓冲排列合并到缓存中,另外启动一个线程负责将缓存数据写入指定的文件中。

Ⅱ java 多线程操作文件

多个线程同时操作一个文档一定要加synchronized进行同步,不然会有线程安全问题的,加synchronized同步后 其他线程才会等待该线程处理完毕后在对这个文件进行操作。

Ⅲ java大数据 多线程写文件

1、采用public static的变量存储这一数值,每个线程都往这一共有静态变量里写入已复制大小。 2、采用Callable方式实现多线程,将结果作为返回值返回到主线程。这一方法只能在所有子线程都完成之后才能通过future获取。

Ⅳ java多线程读写文件

public static void main(String[] args) {
File data = new File("data.txt");
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(
data), "UTF-8");
final BufferedReader bufferedReader = new BufferedReader(read);
for (int i = 0; i < 5; i++) {
new Thread(new Runnable() {
@Override
public void run() {
String lineTXT = null;
synchronized (bufferedReader) {
try {
while ((lineTXT = bufferedReader.readLine()) != null) {
System.out.println(Thread.currentThread()+":"+lineTXT);
bufferedReader.notify();
bufferedReader.wait();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
bufferedReader.notifyAll();
}
}
}
}).start();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

Ⅳ 求多线程读取一个文件,然后写到另外一个文件中的Java实现。

这个是我写的三个类,用于多线程操作读取文件内容和写入文件内容,不知道是不是你合你味口。
________________第一个类______读取内容__写入内容____________________
package pro;

import java.io.*;
public class ReadFileToWriteOtherFile {

private File oldFile;
private File newFile;
private BufferedReader br;
private BufferedWriter bw;
private String totalString="";
private Boolean flag=true; //用于标记文件名是否存在 true表示存在

public ReadFileToWriteOtherFile()
{
oldFile=null;
newFile=null;
br=null;
bw=null;
System.out.println("初始化成功");
}
public void readInfoFromFile(String fileName)
{

System.out.println("开始读取");
try
{

oldFile=new File(fileName);
if(oldFile.exists()) //如果文件存在
{
System.out.println("存在");
br=new BufferedReader(new FileReader(oldFile));
String info=br.readLine(); //读取一行
while(info!=null)
{
totalString+=info; //将读取到的一行添加到totalString中
info=br.readLine(); //再读取下一行
//System.out.println(totalString);
}
System.out.println("读取完成,准备写入…………");
}
else //如果文件不存在
{
System.out.println("文件不存在");
flag=false; //标记该文件不存在
}
// System.out.println("totalString="+totalString);
}
catch(FileNotFoundException e)
{
System.out.println(e);System.out.println("开始读取中1");
}
catch(IOException e)
{System.out.println(e);System.out.println("开始读取中2");}

}
public void writeInfoToFile(String fileName)
{
if(!flag) //如果标记前面的文件不存在,则return
{
flag=true; //改回原来的文件标记符
return;
}
try
{
newFile=new File(fileName);
if(newFile.exists()) //如果存在,不用创建新文件
{
System.out.println("文件存在,可以写入!");
}
else //如果不存在,则创建一个新文件
{
System.out.println("文件不存在,准备创建新文件");
newFile.createNewFile();
System.out.println("文件创建成功,可以写入");
}
bw=new BufferedWriter(new FileWriter(newFile,true));
// System.out.println("totalString="+totalString);
bw.write(totalString,0,totalString.length());
bw.flush(); //刷新缓冲区
System.out.println("写入完成");
totalString="\r\t"; //清空原来的字符串
}
catch(FileNotFoundException e)
{System.out.println(e);}
catch(IOException e)
{System.out.println(e);}

}
}
________________第二个类______一个自定义的线程类____________________
package pro;

import java.lang.Thread;
public class MyThread extends Thread
{
private int index; //用于数组的位置
private String[] fileNames; //定义一个字符串数组
ReadFileToWriteOtherFile bftwo=new ReadFileToWriteOtherFile(); //定义前面的自定义类
public MyThread(String[] fileNames,int index) //index表示数组位置标号
{
this.index=index;
this.fileNames=fileNames;
}
public void run()
{

bftwo.readInfoFromFile(fileNames[index]);//传入数组中的字符串参数
bftwo.writeInfoToFile("b.txt"); //传入写入的目的地文件
//index++; //数组位置加1
System.out.println("==============");//分隔线

}
}
________________第三个类______主程序____________________
package pro;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.support.;
import java.io.*;
public class BeanRunApp {

/**
* Method main
*
*
* @param args
*
*/
public static void main(String[] args)
{
/* ApplicationContext apc=new ("beans.xml");
ClassRoom croom=(ClassRoom)apc.getBean("classRoom");
croom.out();
System.out.println("over");
*/
long startTime=System.currentTimeMillis();
String[] a={"a.txt","c.txt","d.txt","e.txt"}; //用一个符品数组保存文件名

for(int i=0;i<a.length;i++) //用数组的长度来作为循环条件
{ //把这个数组和i的值作为构造函数传入线程类
MyThread myth=new MyThread(a,i);
System.out.println("--------------------------------");
myth.start(); //执行
System.out.println("当前的线程是:"+myth.getName());
}
long endTime=System.currentTimeMillis();
System.out.println("耗时:"+(endTime-startTime)+"毫秒");
}
}

Ⅵ java如何实现多线程读写一个文件

线程保护

Ⅶ Java:关于多线程写文件流的问题

不会的 放心 每个线程都各自引用着自己的流 每个流都各自保存着各自的状态 不会混淆的,记得每个线程完的时候关闭流

Ⅷ 求java多线程遍历目录的完整代码,能运行的那种

目录结构为树型结构,用多线程不大好做,线程最多在前几层进行分割,比如每个目录下有两个目录,共5层,那么root目录下就能启用2个线程分别进行遍历,所以第二层就启动了2个线程进行遍历,加上主线程共三个线程,虽然这样做是可以做,但是要更具实际情况进行线程的规划,否则容易线程过多导致cpu超负荷,或者假死,再提一点,遍历目录不建议用递归来写,因为目录较多容易栈溢出。
随手写了个,会有点bug就是关闭线程池的时候,还有就是有可能目录太多进入拒绝策略,这个东西 可以考虑使用令牌桶算法,或者计数器算法来做。这里提供个简单的例子。
public class TraverseUtil {
public static BlockingQueue blockingQueue = new LinkedBlockingQueue(100);
public static ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100,100,10, TimeUnit.SECONDS,blockingQueue);
public static void traverseFolder2(String path) {
File file = new File(path);
if (file.exists()) {
File[] files = file.listFiles();
if (null == files || files.length == 0) {
System.out.println("文件夹是空的!");
return;
} else {
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath());
threadPoolExecutor.execute(new Runnable() {
@Override
public void run() {
traverseFolder2(file2.getAbsolutePath());
}
});
} else {
System.out.println("文件:" + file2.getAbsolutePath());
}
}
}
} else {
System.out.println("文件不存在!");
}
}
public static void main(String[] args) throws InterruptedException {
traverseFolder2("C:\\Users\\a8932\\Desktop\\md");
}
}

Ⅸ java多线程同时读取一个文件,这个方法可行吗

不可行。每次读取文件都需要创建缓存文件流,很占内存,而且多次读取实际上也是一个文件,还不如直接读取文件,之后通过条件多次获取需要的内容来的实际。
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容,此处可以添加条件进行不同的处理
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。

热点内容
android配置环境变量 发布:2024-10-12 09:22:01 浏览:733
安卓手机如何连接大众车导航 发布:2024-10-12 08:58:17 浏览:733
linuxandroidsdk配置 发布:2024-10-12 08:49:34 浏览:559
爬虫项目实战python 发布:2024-10-12 08:33:59 浏览:107
网站架设多服务器ip 发布:2024-10-12 07:42:15 浏览:188
linuxjdbc 发布:2024-10-12 07:38:10 浏览:199
pythonip正则表达式 发布:2024-10-12 07:30:24 浏览:178
xp怎么认安卓手机 发布:2024-10-12 07:30:20 浏览:879
pythonmac开发工具 发布:2024-10-12 07:29:01 浏览:269
android字符数组 发布:2024-10-12 07:16:32 浏览:307