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

How to tail a file using a NIO selector, in other words, as lines are added to the file a channel is selected so you can read the lines?

Because you cannot redirect GC logs I am left with the option to redirect it to a file with -Xloggc and then get the contents of this file inside my selector through a file channel of some kind. Basically as lines are being added to my file, the…
chrisapotek
  • 6,007
  • 14
  • 51
  • 85
0
votes
1 answer

Random access a file using File Channel?

When I get FileChannel from FileInputStream, I found I can use position method to freely move file pointer. Does it mean FileChannel provides the functionality of RandomAccessFile?
user496949
  • 83,087
  • 147
  • 309
  • 426
0
votes
1 answer

Read specific bytes from RandomAccessFile using FileChannel : Java

I have a RandomAccessFile and its FileChannel. What I'm trying to do is read a specific section of the bytes from said file; however, while looking over the FileChannel read methods, I didn't see overloads that would help with what I'm trying to do,…
Chris V.
  • 1,123
  • 5
  • 22
  • 50
0
votes
0 answers

FileChannel Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

I had an outofmemory error when launching the batch on the server: below are the details of the error as well as the piece of code written in java, can you help me please , the error is triggered at this line: rowString = new String(barray, 0,…
0
votes
1 answer

Java - MP4 files appear corrupt after split

I followed this tutorial detailing how to split a large file into smaller files based on maximum allowed file size. It works - for example, it successfully splits a 84.4MB mp4 file into 30MB, 30MB and 24.4MB mp4 files. However, I found that the…
Johnny
  • 55
  • 7
0
votes
0 answers

FileChannel not writing special characters properly

I'm trying to write some text into a file using a FileChannel. So far everything works fine except for the fact that umlauts are not written correctly. Path fileChannel = Paths.get("c:/channel.txt"); try (FileChannel channel = FileChannel.open( …
Slevin
  • 617
  • 1
  • 7
  • 17
0
votes
0 answers

Using FileChannel to write in the middle of binary files

I'm working on a project in Java with a large binary file from which I always want to read and write small fragments (for writing I'll fill the remainder with padding). Now, when reading a file with InputStream I can use the built-in skip(long)…
linux_user36
  • 113
  • 1
  • 8
0
votes
2 answers

Processing huge pipe delimited files

With reference to my previous post Remove first line from a delimited file I was able to process smaller files and remove the first line .... but incase of huge files there is an issue of memory as I am reading the whole file and then writing it…
Vivek
  • 41
  • 1
  • 5
0
votes
1 answer

Java FileChannel keeps locking file

Hope someone can shed some light into what I'm doing wrong. I have a DataLoader class that creates a FileInputStream. Since FileInputStream implements Closeable, I create that instance as part of the try block. I then pass the newly created stream…
0
votes
1 answer

FileChannel.open(path, CREATE|CREATE_NEW) without WRITE option throws NoSuchFileException

I had the following code: @Nonnull @SneakyThrows private Pair probeSize(@Nonnull final InputStream image) { final String tmpId = UUID.randomUUID().toString(); final File probeFile = new File(tmpDir, tmpId +…
maress
  • 3,533
  • 1
  • 19
  • 37
0
votes
1 answer

Java File Locking with 2 Processess

To start off I have two processes that are running concurrently that support each other. One process reads a simple flatfile which contains snapshots of data separated by timestamps. This application simply opens this file (without file locking),…
Mitchell Romanuik
  • 193
  • 1
  • 1
  • 7
0
votes
1 answer

BufferUnderflowException while trying to read an Int from a binary file in Java

I am trying to read 4 bytes which represent an int, located at byte position 64 in a binary file. This is what I have tried: package testbinaryfile2; import java.io.IOException; import java.nio.ByteBuffer; import…
M.E.
  • 4,955
  • 4
  • 49
  • 128
0
votes
4 answers

Is there a way to add some text in between a file without overwriting any existing content of the file using FileChannel

Let's say I have a txt file:Hello World I want to just add "My" in between so that the file looks like this:Hello My World I was trying to achieve this using java.nio.channels.FileChannel class as you can seek the file pointer.But when I move the…
0
votes
1 answer

Writing an InputStream with ReadableByteChannel and TransferFrom()

The code is split accross several functions of a Grails Web Application but here's the idea MultipartFile mf = request.getPart(); InputStream is = mf.getInputSteam(); Long size = mf.getSize(); ReadableByteChannel source =…
Smithfield
  • 341
  • 4
  • 11
0
votes
0 answers

File cannot be opened/corrupted after writing sucessfully using Java NIO file channel

i have the following code which runs without error and writes the output file successfully. However, i am not able to open the output file. it always prompts me an error saying "word found unreadable content" anyone can advise if there is anything…