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
最满意答案
您需要在收到数据时连接数据。 查看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]; }
相关问答
更多-
EmguCV和MP4文件(EmguCV and MP4 files)[2022-03-12]
我有同样的问题。 对我来说,复制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 ...
-
有没有办法在iOS下载时播放MP3文件?(Is there a way to start playing an MP3 file while downloading it in iOS?)[2022-07-26]
正如您在问题中提到的那样, 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 ...
-
检测mp4文件(Detect mp4 files)[2022-10-01]
你可以查看扩展名或文件签名( 幻数 ) 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)的输出。 编辑: 您不需要扫描整个文件来识别它,签名就可以了,但是, ... -
iPhone和iPad无法播放没有.mp4文件扩展名的mp4视频(iPhone & iPad can't playback mp4 videos having no .mp4 file extension)[2022-05-16]
使用值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 ...