首页 \ 问答 \ Java中的文件线程(Threading in Java for files)

Java中的文件线程(Threading in Java for files)

我正在阅读Java中的文件内容。 我有大约8000个文件来读取内容并将其放在HashMap中,如(路径,内容)。 我认为使用线程可以做到这一点,以加快这个过程。 从我所知的所有8000个文件在不同线程中读取其内容是不可能的(我们可能想限制线程),对它的任何评论? 另外,我对Java线程化是新手,有谁可以帮助您开始使用Java吗?

到目前为止,我认为这个代码是:

    public class ThreadingTest extends Thread {

public HashMap<String, String > contents = new HashMap<String, String>();


public ThreadingTest(ArrayList<String> paths)
{
    for(String s : paths)
    {
      // paths is paths to files.
      // Have threading here for each path going to get contents from a        
      //  file  
        //Not sure how to limit and start threads here
        readFile(s);
        Thread t = new Thread();
        t.start();
    }
}


public String readFile(String path) throws IOException
{
    FileReader reader = new FileReader(path);
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(reader);
    String line;
    while ( (line=br.readLine()) != null) {
      sb.append(line);
    }

    return textOnly;

}

 }

任何帮助完成线程过程。 谢谢


I am looking to read the contents of a file in Java. I have about 8000 files to read the contents and have it in HashMap like (path,contents). I think using Threads would be a option for doing this to speed up the process. From what I know having all 8000 files to read their contents in different threads is not possible(we may want to limit the threads),Any comments on it? Also I am new to threading in Java, can any one help on how to get started on this one?

so far I thought this pesudo code, :

    public class ThreadingTest extends Thread {

public HashMap<String, String > contents = new HashMap<String, String>();


public ThreadingTest(ArrayList<String> paths)
{
    for(String s : paths)
    {
      // paths is paths to files.
      // Have threading here for each path going to get contents from a        
      //  file  
        //Not sure how to limit and start threads here
        readFile(s);
        Thread t = new Thread();
        t.start();
    }
}


public String readFile(String path) throws IOException
{
    FileReader reader = new FileReader(path);
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(reader);
    String line;
    while ( (line=br.readLine()) != null) {
      sb.append(line);
    }

    return textOnly;

}

 }

Any help in completing the threading process. Thanks


原文:https://stackoverflow.com/questions/5106197
更新时间:2024-03-18 16:03

最满意答案

您需要在收到数据时连接数据。 查看NSMutableData对象。 数据完成后,将逻辑放在connectionDidFinishLoading委托方法中。

使用此示例,其中receivedData是您在开始下载之前初始化的属性。

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"BuckBunny.mp4"];

    [receivedData writeToFile:filePath atomically:YES];
    [connection release];
}

You need to concatenate the data as you receive it. Look at the NSMutableData object. Once the data is complete, put logic to proceed in the connectionDidFinishLoading delegate method.

Use this example, where receivedData is a property that you init before you begin the download.

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [receivedData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"BuckBunny.mp4"];

    [receivedData writeToFile:filePath atomically:YES];
    [connection release];
}

相关问答

更多
  • 我有同样的问题。 对我来说,复制opencv_ffmpeg242.dll解决了它,所以感谢你的建议! 如果同样不适合你,我可以建议: 确保它是正确的位数。 确保在运行时将其复制到必要的文件夹(例如\ bin \ debug) 其他DLL也可能是必需的。 添加所有DLL可能是一个很好的测试。 I had the same problem. For me copying opencv_ffmpeg242.dll solved it, so thanks for that suggestion! If the s ...
  • 您需要在收到数据时连接数据。 查看NSMutableData对象。 数据完成后,将逻辑放在connectionDidFinishLoading委托方法中。 使用此示例,其中receivedData是您在开始下载之前初始化的属性。 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [receivedData appendData:data]; } - (void)connectionDid ...
  • 正如您在问题中提到的那样, AVAudioPlayer是相当高的水平,并没有给你很多选择 我认为你必须使用AudioToolbox AudioFileStream, AudioQueue is the way to go. Implemented it the hard way. The link below helped me out :) lastFM source
  • 您必须以二进制模式打开文件,以确保文件正确保存到磁盘。 $file = fopen($destination, "wb"); 也可以使用fwrite而不是fputs fwrite($file, $data); 检查video.mp4是否正确地从浏览器下载。 也许是重定向? 如果是这样,添加此选项。 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 如果仍然无效,则转储此信息并发布。 var_dump(curl_getinfo($ch)); You must o ...
  • 这是WAS版本的已知问题/限制。 最好的解决方案是从IHS提供mp4文件。 It's a known issue/limitation of that version of WAS. The best solution is to serve mp4 files from IHS.
  • 第一个header("Content-type: application/octet-stream"); 不需要下载文件。 资源 第二 改变你的标题, header('Content-type: video/mp4'); header('Content-type: video/mpeg'); 这应该工作。 First header("Content-type: application/octet-stream"); is not needed to download file. Source Second ...
  • 原来,Xuggler有时会遇到64位JRE的问题。 我切换到一个32位的JRE,它解决了这个问题。 在Eclipse中切换JRE:使用Eclipse我右键单击我的项目并转至Properties-> Java Build Path-> Libraries。 然后我选择了“JRE System Library”并点击“编辑...”。 从那里我选择了“Alternate JRE:”并选择了“jre7-32-bit”,然后点击“完成”,然后点击“确定”。 如果您还没有安装32位JRE,您必须先执行此操作。 Turn ...
  • 你可以查看扩展名或文件签名( 幻数 ) http://www.garykessler.net/library/file_sigs.html http://en.wikipedia.org/wiki/List_of_file_signatures 00 00 00 18 66 74 79 70 33 67 70 35 .... ftyp 3gp5 MPEG-4视频文件 或者,如果您使用的是Unix / Linux,则可以从程序中解析文件(1)的输出。 编辑: 您不需要扫描整个文件来识别它,签名就可以了,但是, ...
  • 使用值video/mp4作为Content-Type标头。 Use the value video/mp4 for the Content-Type header.
  • 检查此链接它显示的东西.. (1)。 添加背景-音乐对Android的应用程序 (2)。 玩,前景和背景音乐 并通过视频播放音频,你可以去... (3)。 如何播放视频和音频功能的Android check this link it shows something .. (1). Adding-Background-Music-to-Android-App (2). play-foreground-and-background-music and to play audio with video you c ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。