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

Any code tips for speeding up random reads from a Java FileChannel?

I have a large (3Gb) binary file of doubles which I access (more or less) randomly during an iterative algorithm I have written for clustering data. Each iteration does about half a million reads from the file and about 100k writes of new values. I…
Simon
  • 78,655
  • 25
  • 88
  • 118
3
votes
1 answer

Reading Huge Data With Java

Firstly, I'm sorry about my English. I looking for an effective a way to read a Big file in java. I make a log analysis program and I have log files at least from 500 MB to 4 GB. I have tried the Filechannel class (Memory Mapped files), but I could…
Hyunwoo Kim
  • 105
  • 1
  • 1
  • 5
2
votes
2 answers

ByteBuffer and FileChannel reading only the specified number of bytes

I have a situation where in I keep reading with a ByteBuffer as below. ByteBuffer buffer = MappedByteBuffer.allocateDirect(Constants.BUFFER_SIZE); But when the reading reaches the boundary (when the remaining bytes to read is less than…
nobody
  • 1,909
  • 5
  • 31
  • 45
2
votes
0 answers

FileChannel.tryLock works locally but hangs on remote machine, accessed via SSH

Tl;DR code with FileChannel.tryLock works on a local file but hangs indefinitely when run on a remote machine, I had SSHed into. Most of the length of this question is about showing outputs and debugging attempts, but the essential part is in the…
lineage
  • 792
  • 1
  • 8
  • 20
2
votes
1 answer

Loading raw VBO data via MappedByteBuffer into OpenGL (not working)

I've attempted to load raw, uncompressed VBO data via the method presented in a talk Google did at GDC 2011. This method uses a MappedByteBuffer to quickly load the data in a subsequent call to glBufferData. Unfortunately, for me, it's just not…
user8709
  • 1,342
  • 13
  • 31
2
votes
3 answers

Java XML parsing

I have a file that has several XML documents like below in sequence. .........
foobarometer
  • 751
  • 1
  • 9
  • 20
2
votes
2 answers

Why is StandardOpenOption.DELETE_ON_CLOSE not deleting the source file of the FileChannel?

We have underneath method in Java which should delete the source file when its close method is called. private void appendFile(Path destination, Path source) {     try (FileChannel sourceChannel…
2
votes
0 answers

Cannot delete temporary file after writing to it via a MappedByteBuffer

When I run this JUnit test, it fails because file.delete() returns false: @Test public void testDeleteTempFile() throws Exception { File file = Files.createTempFile("_file_del_test", ".txt").toFile(); try (RandomAccessFile f = new…
Michael
  • 4,722
  • 6
  • 37
  • 58
2
votes
1 answer

Transerring files between FileChannel and Socket

I'm writing a multithreaded server in Java. The server transfers files from/to clients. A requirement of the project is to use NIO to handle files. Since the server is multithreaded, I'm not using SocketChannels for the communication, instead I'm…
PoriPiriPuri
  • 43
  • 1
  • 4
2
votes
1 answer

What's the purpose of waiting in FileChannel.lock if OverlappingFileLockException is thrown anyway?

FileChannel.lock allows to create file lock in Java (I use the approach from How can I lock a file using java (if possible) with FileOutputStream in order to avoid NonWritableChannelException): FileOutputStream out = new FileOutputStream(file); try…
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
2
votes
1 answer

why the buffers have not been write into the FileChannel

I'm learning java NIO now, I have found an example to explain the gather operation for FileChannel as below: public class ScattingAndGather { public static void main(String args[]) { gather(); } public static void gather() { …
gesanri
  • 237
  • 1
  • 3
  • 10
2
votes
1 answer

Fastest way to read a line in file

I am using RandomAccessFile to read some informations from a large file. RandomAccessFile has a method seek that points the cursor to a specific part of the file that I want to read the whole line. To read this line I use readLine() method. I read…
Marcelo Machado
  • 1,179
  • 2
  • 13
  • 33
2
votes
2 answers

What is the best way for Files IO in java

I need to do some basic operations on image file in java . My requirements are like : - opening a file. - read bytes in some order. - write the updated byte at the particular offset - seeking at some offset in file. Files can be of any size like 2…
aga
  • 359
  • 2
  • 4
  • 15
2
votes
1 answer

FileChannel.tryLock sometimes throws AccessDeniedException

I've written a small method that is meant to tell me if another instance of the application is already running. I am aware that there are many ways to find out if another instance is running, but I chose this one. I am creating an empty file and…
Klitos Kyriacou
  • 10,634
  • 2
  • 38
  • 70
2
votes
1 answer

SocketChannel and FileChannel.transferFrom/To

I just learnt about the java nio package and channels and now, I tried to write a really simple file transfer program using channels. My aim was to get rid of all this bytewise reading stuff. As a first try, I wrote the following server code:…
Green绿色
  • 1,620
  • 1
  • 16
  • 43