當前位置:首頁 » 文件管理 » 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流會一直存在,直到程序運行結束。

熱點內容
編譯原理視頻教程推薦 發布:2024-10-12 10:26:33 瀏覽:522
我的世界神奇寶貝伺服器怎麼穿牆 發布:2024-10-12 10:06:08 瀏覽:251
phpsession數組 發布:2024-10-12 10:02:25 瀏覽:105
重建sqlserver2008 發布:2024-10-12 09:31:30 瀏覽:816
android配置環境變數 發布:2024-10-12 09:22:01 瀏覽:734
安卓手機如何連接大眾車導航 發布:2024-10-12 08:58:17 瀏覽:734
linuxandroidsdk配置 發布:2024-10-12 08:49:34 瀏覽:560
爬蟲項目實戰python 發布:2024-10-12 08:33:59 瀏覽:108
網站架設多伺服器ip 發布:2024-10-12 07:42:15 瀏覽:190
linuxjdbc 發布:2024-10-12 07:38:10 瀏覽:201