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

How wait and get lock on file

I want wait till other program releases lock on particular file, then I want to proceed to open that unlocked file. I came across many solutions, but none are useful, here is my code - File file = new File("c:\\somelockedfile.txt"); FileChannel…
nullptr
  • 3,320
  • 7
  • 35
  • 68
3
votes
1 answer

Java: obtain the filename from a opened RandomAccessFile instance

How can I obtain the filename from a opened RandomAccessFile instance? I can find only the following methods related to the file itself: getFD() : which returns a FileDescriptor object getChannel() : which returns a FileChannel object I would like…
alexroat
  • 1,687
  • 3
  • 23
  • 34
3
votes
2 answers

Java Possible FileChannel.map Bug

So I'm trying to read in a very large file using a mapped FileChannel. The file exceeds 2GB. A snippet of code is: long fileSize = 0x8FFFFFFFL; FileChannel fc = new RandomAccessFile("blah.huge",…
poy
  • 10,063
  • 9
  • 49
  • 74
3
votes
1 answer

FileChannel.write on Linux produces lots of garbage, but not on Mac

I am trying to limit the amount of garbage produced by my log library, so I coded a test to show me how much memory is FileChannel.write creating. The code below allocates ZERO memory on my Mac, but creates tons of garbage on my Linux box (Ubuntu…
TraderJoeChicago
  • 6,205
  • 8
  • 50
  • 54
3
votes
1 answer

Apache Flume stuck after ChannelFullException is occured 500 times

I have flume configuration with rabbitmq source, file channel and solr sink. Sometimes sink becomes so busy and file channel is filling up. At that time ChannelFullException is being thrown by file channel. After 500 number of ChannelFullException…
Gürcan Kavakçı
  • 562
  • 1
  • 11
  • 24
3
votes
3 answers

FileInput/OutputStream versus FileChannels -- which gives better performance

I am writing a program that has to copy a sizeable, but not huge amount of data from folder to folder (in the range of several dozen photos at once). Originally I was using java.io.FileOutputStream to simply read to buffer and write out, but then I…
donnyton
  • 5,874
  • 9
  • 42
  • 60
3
votes
1 answer

Fastest way to write a FloatBuffer or Float(float) array to a file in Java

I've got a bunch of float data in a FloatBuffer which needs to be written to a file (without it taking three minutes). Currently a DataOutputStream is used to write the FloatBuffer element by element to a file. This is slow. Preferably, I'd like to…
user8709
  • 1,342
  • 13
  • 31
3
votes
1 answer

What method is more efficient for concatenating large files in Java using FileChannels

I want to find out what method is better of two that I have come up with for concatenating my text files in Java. If someone has some insight they can share about what goes on at the kernel level that explains the difference between these methods of…
GLaDOS
  • 683
  • 1
  • 14
  • 29
3
votes
1 answer

Are transferFrom and transferTo transfer all bytes if invoked with FileChannel?

Some internet documents show me examples look like, FileChannel target; FileChannel source; target.trasferFrom(source, 0, source.size()); // done! or FileChannel source; FileChannel target; source.transferTo(0, source.size(), target); // done! But…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
3
votes
1 answer

How can I break from a FileChannel#transferFrom loop?

I'm writing a utility class for FileChannels. Following method looks it may work. // tries to transfer as many bytes as specified public static long transferTo(final FileChannel src, long position, long count, final…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
3
votes
2 answers

Insert brackets at specific spots in file

I'm trying to write a script that, given a file, will search through it and every time it encounters a require call will add brackets, so that, for instance, var x = require("name") becomes var x = require(["name"]) Normally for something like…
3
votes
6 answers

How to avoid OutOfMemoryError when using Bytebuffers and NIO?

I'm using ByteBuffers and FileChannels to write binary data to a file. When doing that for big files or successively for multiple files, I get an OutOfMemoryError exception. I've read elsewhere that using Bytebuffers with NIO is broken and should be…
jumar
  • 5,360
  • 8
  • 46
  • 42
3
votes
1 answer

FileChannel.transferFrom fails for larger files with Out of memory Error

FileChannel.transferFrom(source, 0, source.size()) gives the following OutOfMemory exception, when trying to copy a file of size around 2GB. I understand the memory issue due to larger files. Can we solve it by coping the file in small chunks, in a…
Mayur More
  • 951
  • 2
  • 15
  • 37
3
votes
1 answer

File Channel reads/adds wrong data

I am using a filechannel with a byte buffer to send packets over the network. My problem is that when the filechannel reads the last few bytes it appends the last bit of data from previous bytes read even though I am clearing the byte buffer after I…
user3339242
  • 631
  • 2
  • 15
  • 32
3
votes
1 answer

Getting a wrong FileChannel size

I'm trying to get the size of a file contained in assets. I'm using a FileChannel because I need a FileChannel later. The file myfile.txt contains 7 bytes. Here is my code: AssetManager amgr; AssetFileDescriptor afd; FileChannel…
Vincent
  • 1,013
  • 14
  • 33
1 2
3
12 13