Reading file from chunk

I want to read a file in parts. The file is divided into several parts that are stored on different media. Currently, I call every single fragment of the file and then combine it with the source file.

The problem is that I need to wait until all the pieces appear before I can play / open the file. Is it possible to read the pieces as they arrive, and not wait for their arrival?

I am working on a media file (movie file).

+5
source share
3 answers

what you want is a source data string . This is ideal for when your data is too large to hold in memory immediately, so you can start playing until you get the whole file. Or if the file never ends.

see the tutorial for the original data row here

http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html#read

I would use this FileInputSteam

+2
source

See InputSteram.read (byte []) for reading bytes at a time.

Code example:

try { File file = new File("myFile"); FileInputStream is = new FileInputStream(file); byte[] chunk = new byte[1024]; int chunkLen = 0; while ((chunkLen = is.read(chunk)) != -1) { // your code.. } } catch (FileNotFoundException fnfE) { // file not found, handle case } catch (IOException ioE) { // problem reading, handle case } 
+12
source

Instead of the old io, you can try nio to read a file fragment by fragment in memory, and not by the full file (as suggested in the answers above). You can use the feed to get data from multiple sources.

 RandomAccessFile aFile = new RandomAccessFile( "test.txt","r"); FileChannel inChannel = aFile.getChannel(); long fileSize = inChannel.size(); ByteBuffer buffer = ByteBuffer.allocate((int) fileSize); inChannel.read(buffer); //buffer.rewind(); buffer.flip(); for (int i = 0; i < fileSize; i++) { System.out.print((char) buffer.get()); } inChannel.close(); aFile.close(); 
0
source

All Articles