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
6
votes
4 answers

Prevent OutOfMemory when using java.nio.MappedByteBuffer

Consider application, which create 5-6 threads, each thread in cycle allocate MappedByteBuffer for 5mb page size. MappedByteBuffer b = ch.map(FileChannel.MapMode.READ_ONLY, r, 1024*1024*5); Sooner or later, when application works with big files,…
user12384512
  • 3,362
  • 10
  • 61
  • 97
6
votes
4 answers

Reading Objects from Random Access File

I wrote a file using Java's FileChannel class that uses RandomAccessFiles. I wrote objects at various locations in the file. The objects were of variable sizes but all of the same class. I wrote the objects using the following idea…
AnkurVj
  • 7,958
  • 10
  • 43
  • 55
6
votes
4 answers

Java NIO MappedByteBuffer OutOfMemoryException

I am really in trouble: I want to read HUGE files over several GB using FileChannels and MappedByteBuffers - all the documentation I found implies it's rather simple to map a file using the FileChannel.map() method. Of course there is a limit at 2GB…
Zordid
  • 10,451
  • 11
  • 42
  • 58
5
votes
6 answers

Reading an ASCII file with FileChannel and ByteArrays

I have the following code: String inputFile = "somefile.txt"; FileInputStream in = new FileInputStream(inputFile); FileChannel ch = in.getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(BUFSIZE); // BUFSIZE =…
Jake
  • 15,007
  • 22
  • 70
  • 86
5
votes
1 answer

How to use java.nio.channels.FileChannel to read to ByteBuffer achieve similiar behavior like BufferedReader#readLine()

I want to use java.nio.channels.FileChannel to read from a file, but I want to read line per line like BufferedReader#readLine() does. The reason why I need to use java.nio.channels.FileChannel instead of java.io is because I need to put a lock on a…
Thang Pham
  • 38,125
  • 75
  • 201
  • 285
5
votes
1 answer

Read scattered data from multiple files in java

I'm working on a reader/writer for DNG/TIFF files. As there are several options to work with files in general (FileInputStream, FileChannel, RandomAccessFile), I'm wondering which strategy would fit my needs. A DNG/TIFF file is a composition…
5
votes
1 answer

FileUtils.copyFile() VS FileChannel.transferTo()

As I found, underlying OS call for the copyToFile() is Libcore.os.read(fd, bytes, byteOffset, byteCount), while transferTo() is based on memory mapped file: MemoryBlock.mmap(fd, alignment, size + offset, mapMode); ... buffer = map(MapMode.READ_ONLY,…
MobileX
  • 409
  • 4
  • 13
5
votes
2 answers

Need to convert AssetInputStream to FileInputStream

I have implemented a data structure which is working on my computer and now I am trying to port it into my android application. I open a raw .dat resource and get a InputStream but I need to get a FileInputStream: FileInputStream fip =…
Joop
  • 3,706
  • 34
  • 55
5
votes
1 answer

RandomAccessFile Vs NIO Channel

I am trying to understand the following behavior. My older code, String path = "C:/temp/sample.txt"; String mode= "rw"; FileChannel channel = new RandomAccessFile(path, mode).getChannel(); // some code to write to this file // finally delete File…
Victor
  • 1,207
  • 2
  • 13
  • 21
5
votes
3 answers

Java 7 filechannel not closing properly after calling a map method

I'm working on a sc2replay parsing tool. I build it on top of MPQLIB http://code.google.com/p/mpqlib/ Unfortunately the tool uses filechannels to read through the bzip files, and uses map(MapMode.READ_ONLY, hashtablePosition, hashTableSize); After…
user1717817
4
votes
1 answer

Why does Java FileChannel.truncate fail predictably on Windows only?

I have a web application running in Wildfly, and part of the application uses a temporary file via: File.createTempFile(...). That temporary file is then used as the destination file for a read-write java.io.RandomAccessFile stream. The…
DDoomUs
  • 183
  • 9
4
votes
2 answers

Writing contents of ByteArrayOutputStream to a file using NIO FileChannel

I have a method that generates a ByteArrayOutputStream and I want to put those contents in a file using FileChannel. With FileOutputStream, I could do this: ByteArrayOutputStream baos = MyClass.myMethod(); // get my stream FileOutputStream out = new…
quantumSoup
  • 27,197
  • 9
  • 43
  • 57
4
votes
1 answer

Not getting file through FileInputStream?

I am trying to check file position so that it would not be overwritten. For this purpose I have to use FileInputStream because it has a method position() that can be use with FileChannel. BufferedReader does not maintain position. My code…
user3664724
  • 425
  • 1
  • 6
  • 18
4
votes
5 answers

Who is better in performance filechannel or RandomAccessFile for reading and writing?

I recently came across FileChannel, I am a big fan of RandomAccessFile. But I am wondering why would I pick FileChannel over RandomAccessFile for reading from a file and writing that content to another. Is there any specific performance reason? I…
fscore
  • 2,567
  • 7
  • 40
  • 74
4
votes
2 answers

Appending to a file with Java 7 AsynchronousFileChannel

I am trying the AsynchronousFileChannel JAVA 7 API to write a file in an async manner, however I could not find an easy way to append to the file. The API description states that AsynchronousFileChannel doesn't maintain the file position and you…
bsam
  • 1,838
  • 3
  • 20
  • 26
1
2
3
12 13