Questions tagged [filechannel]

In Java FileChannel is an abstract class for reading, writing, mapping, and manipulating a file

In Java FileChannel is an abstract class for reading, writing, mapping, and manipulating a file.

A file channel is a SeekableByteChannel that is connected to a file.

It has a current position within its file which can be both queried and modified.

The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried.

The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is truncated.

The file may also have some associated metadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access.

More info

193 questions
1
vote
1 answer

How can I lock a file if it exists, don't create it?

I want to test to see if I can lock file X. If the file doesn't exist or can't be locked, fail. It sounds simple, but I keep running into dead-ends. A short-cut to the answer would be to provide a way to get a FileChannel I can make an exclusive…
Richard Brightwell
  • 3,012
  • 2
  • 20
  • 22
1
vote
1 answer

NIO GZIP compression and copying of files

I was using FileChannel and FileInputStream to do a simple file copying from File fromFile to File toFile. The code is basically like this: source = new FileInputStream(fromFile).getChannel(); destination = new FileOutputStream(toFile,…
bozeng
  • 245
  • 1
  • 2
  • 10
1
vote
1 answer

FileChannel.write incomplete

I'm using FileChannel to wrtie 2MB data to a file. private void write(int numEntries, int entrySize) throws Exception { File dir = new File(iotestDir); dir.mkdirs(); File file = new File(dir, "data00001.dat"); …
zjk
  • 2,043
  • 3
  • 28
  • 45
1
vote
2 answers

Java FileChannel bigger than its content

I am creating a fileChannel to perform memory mapped writing. This fileChannel has a size of 100 bytes. I only write 80 bytes to it. So when I read from that file later on, it adds 5 "0" to the and. Is there any way to set the size or get the size…
1
vote
2 answers

How to set fileChannel position

I am reading in a file 510 bytes at a time. The bytes are in a byte buffer and I am reading them using a fileChannel. Once I change the position, and it checks the case inside the while loop again but then jumps out the while loop. The total number…
user3339242
  • 631
  • 2
  • 15
  • 32
1
vote
2 answers

write from multiple threads on same file without locking, Java

I am making a download manager and I want multiple threads downloading different segments of file to write to file at different places at a time. Just for every one's clarification I dont want file to lock because it will kill the purpose of…
1
vote
1 answer

Missing bytes while transferring file over SocketChannel

I am transferring large file between Client and Server using nio ServerSocketChannel and SocketChannel.The problem is when I transfer file of size 6060064 bytes from the sender to the receiver, the receiver receives only 6059040 bytes missing some…
rns
  • 1,047
  • 10
  • 25
1
vote
0 answers

Hadoop Mapreduce using FileChannel, FileInputStream

I want to use FileChannel in Hadoop Reducer for fast retrieval the data from file. With it, I can use MappedByteBuffer to map parts of file to memory. It is faster than other APIs when reading data (At least much faster than DataInputStream).…
user3677630
  • 635
  • 6
  • 14
1
vote
0 answers

Copy a file: FileChannel vs Input/OutputStream

Is using a FileChannel the newer and better way to copy a file in Android (as opposed to making an InputStream and OutputStream)? And if so, how do I specify a File in assets to copy to any other location?
Suragch
  • 935
  • 3
  • 9
  • 15
1
vote
0 answers

I cannot memory map large files. Map Failed. OutOfMemoryError

Here is my code: FileChannel fc = new RandomAccessFile(afile.getAbsolutePath(), "r").getChannel(); ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); Here are the errors: java.io.IOException: Map failed Caused by:…
Danny Rancher
  • 1,923
  • 3
  • 24
  • 43
1
vote
1 answer

Write a large text file with NIO

I write a large, approximately 1200 (+/- 200) mb , csv file for an offline report in my application. (A thread performs this job.) The data count may run around 50 million, so the query is run for every 50k rows. The query loop runs till empty fetch…
vvra
  • 2,832
  • 5
  • 38
  • 82
1
vote
1 answer

FileChannel transferFrom's comments explanation

I've read the comment on FileChannel's transferFrom *

This method is potentially much more efficient than a simple loop * that reads from the source channel and writes to this channel. Many * operating systems can transfer bytes directly…

WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
1
vote
1 answer

Get FileChannel from AssetManager Android

I want to get a FileChannel from the AssetManager in Android, is there any way to do this? I want to map some raw custom binary files to some buffers. (If you have a better solution than using the AssetManager, please feel free to mention.
Ryan
  • 2,755
  • 16
  • 30
1
vote
1 answer

Is there a way to have FileChannels close automatically?

I am currently developing an application that requires random access to many (60k-100k) relatively large files. Since opening and closing streams is a rather costly operation, I'd prefer to keep the FileChannels for the largest files open until…
Jan Kebernik
  • 154
  • 6
1
vote
0 answers

java FileChannel writing to a file and adding \n at the end

i'm trying to make log using the FileChannel. for some reason it doesn't get the \n character in my file. my function: private void serverLog(String status, String message) { currentTime = new Date().getTime(); String…
Asaf Nevo
  • 11,338
  • 23
  • 79
  • 154