導航:首頁 > 文件教程 > 多線程寫不同文件夾

多線程寫不同文件夾

發布時間:2023-04-05 12:12:16

java大數據 多線程寫文件

1、採用public static的變數存儲這一數值,每個線程都往這一共有靜態變數里寫入已復制大內小。 2、採用Callable方式實現多線程,容將結果作為返回值返回到主線程。這一方法只能在所有子線程都完成之後才能通過future獲取。

Ⅱ C++多線程操作文件遇到的問題!

句柄和指針都可以有多個,讀的時候可以同時讀,但是寫的時候要注意互斥,一次只能有一個進行寫操作,並且寫的時候不能讀。這些可以使用mutex實現。

Ⅲ 求多線程讀取一個文件,然後寫到另外一個文件中的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)+"毫秒");
}
}

Ⅳ writefile 多線程寫多文件 該如何做

下面的程序,編譯之後,你可以運行很多個實例,目前我將文件寫在了D:\1.txt,每個程序寫1000行數據,這些值你可以自己更改(比如 寫在C:,每個程序寫10000行等),等程序都寫完後,你可以去文件中查看寫文件的結果。補充一下,我是在VC6.0環境中寫的,所以windows.h,如果你不是在這個環境中的話,可能需要修改一些定義,比如DWORD等。其他的API都是windows平台提供的API;
#include <stdio.h>
#include "windows.h"
int main()
{
//獲取進程ID,因為你希望是多個進程運行同時寫一個文件,所以,我們列印出進程ID
DWORD dwProcessID = GetCurrentProcessId();

//初始化我們要寫入文件中的內容,及該內容長度;
char szContent[100] = {0};
sprintf(szContent,"process[%u] write file\r\n",dwProcessID);
DWORD dwContentLen = strlen(szContent);

//創建互斥量,這樣可以進行進程間的互斥,當然用這個也可以做線程間的互斥
HANDLE hMutex = CreateMutex(NULL,FALSE,"MyFileMutex");
if (NULL == hMutex)
{
printf("[%u]Create/Open Mutex error!\r\n",dwProcessID);
return 1;
}

//創建或打開文件
HANDLE hFile = CreateFile("D:\\1.txt",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_WRITE | FILE_SHARE_READ,NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_ARCHIVE,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
printf("[%u]Creat/Open file error!\r\n",dwProcessID);
return 1;
}

//循環寫入文件
for(int i = 0; i < 1000 ; i++)
{
//等待臨界資源,即鎖定文件
WaitForSingleObject(hMutex,INFINITE);
printf("Process[%u] Get the signal\r\n",dwProcessID);
DWORD len = 0;

//因為是共享寫文件,即多個程序寫一個文件,所以一定要將文件指針偏移到尾部
SetFilePointer(hFile,0,NULL,FILE_END);

//寫入文件
BOOL rnt = WriteFile(hFile,szContent,dwContentLen,&len,NULL);
if (rnt == FALSE)
{
printf("Process[%u] Fail to write file\r\n",dwProcessID);
}

//釋放互斥量,解除鎖定
ReleaseMutex(hMutex);

//加個Sleep便於我們中間觀察結果
Sleep(30);
}
CloseHandle(hMutex);
CloseHandle(hFile);
return 0;
}

應你要求,我把AIP中的宏定義解釋如下:
HANDLE hFile = CreateFile("D:\\1.txt",
GENERIC_READ | GENERIC_WRITE,//表示程序對該文件有讀和寫的許可權
FILE_SHARE_WRITE | FILE_SHARE_READ,//表示可以多個程序共享讀和寫的許可權
NULL,
OPEN_ALWAYS,//表示打開該文件,如果該文件不存在,則創建該文件
FILE_ATTRIBUTE_ARCHIVE,//文件的屬性為存檔
NULL);

WaitForSingleObject(hMutex,INFINITE);
//INFINITE表示永遠等待,直到hMutex有信號為止

SetFilePointer(hFile,0,NULL,FILE_END);
//FILE_END表示從文件尾部開始偏移;實際此舉就是將文件指針偏移到文件尾部;

Ⅳ 怎麼避免多線程同時讀寫文件

Java中不同的線程是可以同時操作一個文件的,只不過有時候因為進程執行的快慢,會出現數據讀取不同步的問題,例子如下:Public class Readfile implements Runnable{ public void run(){ FileInputStream inputStream = new FileInputStream(file);//讀數據 byte[] buffer = new byte[1024]; int size; while ((size = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, size);//寫數據 } inputStream.close();// outputStream.close(); } public satatic void main(String args []) throws InterruptedException{ Readfile rf = new Readfile(); Thread t1 = new Thread(rf);//開啟一個線程 Thread t2 = new Thread(rf);//開啟第二個線程 t1.start(); t2.start(); }}

Ⅵ c語言文件讀寫 多線程

主線程讀的是A文件,次線程寫的是B文件,兩者不沖突。4K的buffer已經算很小了。重點是主次線程共享的數據需要做同步,所以才造成了要等待的現象。你說的類似消費者和生產者模型。

Ⅶ 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() {
@
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();
}
}

Ⅷ qt 怎麼使用多線程遍歷文件夾

一、Qt遍歷文件夾下一層的文件:
方式1:
void ImageTree::addFolderImages(QString path)
{
//判斷路徑是否存在
QDir dir(path);
if(!dir.exists())
{
return;
}
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
int file_count = list.count();
if(file_count <= 0)
{
return;
}
QStringList string_list;
for(int i=0; i
{
QFileInfo file_info = list.at(i);
QString suffix = file_info.suffix();
if(QString::compare(suffix, QString("png"), Qt::CaseInsensitive) == 0)
{
QString absolute_file_path = file_info.absoluteFilePath();
string_list.append(absolute_file_path);
}
}
}
分析:遍歷文件的下一層,對於系統而言包括:文件夾、文件、快捷方式,使用setFilter即可過濾。通過entryInfoList則可以獲取過濾後所得到的文件夾下的文件信息列表,遍歷文件通過操作QFileInfo可得到所需的文件詳細信息(大小、類型、後綴等)。

閱讀全文

與多線程寫不同文件夾相關的資料

熱點內容
產品在網站優化多少錢 瀏覽:992
亂碼文件夾 瀏覽:480
mc編程後怎麼模擬加工 瀏覽:153
如何恢復刷機後的數據 瀏覽:243
重裝系統win81教程 瀏覽:317
nero10安裝教程 瀏覽:182
handJoy游戲大廳安卓版 瀏覽:663
wow的配置文件怎麼重置 瀏覽:921
css代碼在線編輯 瀏覽:383
哪個狼人殺app可以觀戰 瀏覽:797
你懂的免費qq空間 瀏覽:858
電影曲面是在哪裡拍的app 瀏覽:137
ipadwps怎麼改文件名 瀏覽:162
怎麼將結果顯示在jsp 瀏覽:819
word文檔解析度 瀏覽:108
如何在網站主頁中插入圖像 瀏覽:258
特斯拉數據需要多少伺服器 瀏覽:828
手機百度雲無法看種子文件 瀏覽:690
都有哪些街拍網站 瀏覽:482
賣家鄉特產要什麼網站賣呢 瀏覽:102

友情鏈接