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
0
votes
3 answers

Read/Write using FileChannel and FileInput/OutputStream-Java

I am new to file handling. I tried reading a file using fileinputstream and file channel. I could not find out the bug in the following code. It runs successfully but file has not been transferred. New file is created with zero bytes. Please have a…
Struse
  • 75
  • 3
  • 13
0
votes
1 answer

FileChannel.read() fail to throw ClosedByInterruptException

There is a simple situation like: Thread A: try to read from a FileChannel like length = tun.read(tunPacket.buffer); This operation is block the this A thread. Thread B: wait for a user input, (Stop button). When this event happens, calls A's…
SPYFF
  • 133
  • 1
  • 6
0
votes
0 answers

MappedByteBuffer with multiple files

When it comes to Java NIO MappedByteBuffer, is it possible to map part of different files into the buffer? Something like this: buffer = infilechannel1.map(FileChannel.MapMode.READ_WRITE, infilechannel1.position(), infilechannel1.size()); Then…
0
votes
0 answers

Fastest way to write file with multiple threads:FileChannel vs multiple RandomAccessFiles

I have a file(0 to N) bytes long, that i want to download and need to write it into file. What is faster writing into FileChannel with multiple threads or writing into multiple RandomAccessFiles at the same time from each thread that is reading…
Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
0
votes
0 answers

If block get executed while condition is false

I am trying to copy a file from one directory to another, everything works perfect except when i try to run Copy.exe am getting an exception. What wrong with the code? why the code withing if statement is executed while the condition is…
Denis
  • 390
  • 3
  • 14
0
votes
1 answer

MappedByteBuffer throwing a java.nio.BufferOverflowException

I am learning java nio and I am using MappedByteBuffer and ExecutorService to copy files asynchronously. My problem is the method MappedByteBuffer.put() is throwing java.nio.BufferOverflowException. But in my debug I am not copying to over position…
Felipe
  • 7,013
  • 8
  • 44
  • 102
0
votes
1 answer

Why does position() method of FileChannel return zero always?

My app reads text file line by line and record offset of each line until the end of file. But position() always returns 0. What is wrong with my code? String buffer; long offset; RandomAccessFile raf = new RandomAccessFile("data.txt",…
user3152056
  • 169
  • 2
  • 10
0
votes
1 answer

Fork/Join combine with FileChannel to copy file

Recently I am making an exercise using Java 7 FORK/JOIN framework and FileChannel to copy a file. Here is my code (Test.java): import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import…
user2758327
  • 41
  • 1
  • 1
  • 4
0
votes
1 answer

Reading certain number of bytes into ByteBuffer

I have a binary file of 10MB. I need to read it in chunks of different size (e.g 300, 273 bytes). For reading I use FileChannel and ByteBuffer. Right now for each iteration of reading I allocate new ByteBuffer of size, that I need to read. Is there…
Sparrow_ua
  • 664
  • 1
  • 11
  • 28
0
votes
2 answers

Java doc specification for Filechannel transferTO

As per Java doc, ClosedByInterruptException is thrown when: @throws ClosedByInterruptException If another thread interrupts the current thread while the transfer is in progress, thereby closing both channels and setting…
fscore
  • 2,567
  • 7
  • 40
  • 74
0
votes
0 answers

java filechannel cpu usage growing with time

I am using java nio filechannel transferFrom function along with Apache httpclient to download files from internet. Its starts normally but cpu usage suddenly grows after some time. And download speed decreases and eventually becomes zero. try…
0
votes
1 answer

What happens with written data if Filechannel.force(true) throws an exception?

What happens, when calling filechannel.force and throws an exception? When filechannel.write was successful and we were able to write the whole buffer content into the filechannel i want to be sure if everything was flushed to disk. I'll call…
0
votes
1 answer

Preventing Unicode Byte Order Mark to be written in the middle of a file

This code writes two strings in a file channel final byte[] title = "Title: ".getBytes("UTF-16"); final byte[] body = "This is a string.".getBytes("UTF-16"); ByteBuffer titlebuf = ByteBuffer.wrap(title); ByteBuffer bodybuf =…
mins
  • 6,478
  • 12
  • 56
  • 75
0
votes
1 answer

How do I create an OutputStream that writes to a FileChannel

I have a FileChannel object (which I need so I can apply a FileLock to the file), and I want to write to the file it references using a simple OututStream. How do I create an OutputStream that writes to a FileChannel? This should be in the Java API…
Raedwald
  • 46,613
  • 43
  • 151
  • 237
0
votes
3 answers

Weird ByteBuffer Outputs

I was working with ByteBuffers and IntBuffers when I came across this weird problem. Here's the code - public static void main(String[] args) { File file = null; FileOutputStream fos = null; FileChannel fc = null; ByteBuffer bb =…
Ryotsu
  • 786
  • 6
  • 16