Questions tagged [filechannel]

In Java FileChannel is an abstract class for reading, writing, mapping, and manipulating a file

Filter by
Sorted by
Tagged with
2 votes
0 answers
51 views

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 ...
lineage's user avatar
  • 790
0 votes
0 answers
32 views

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

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 ...
Wajih Bouzid's user avatar
0 votes
1 answer
139 views

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 ...
Johnny's user avatar
  • 57
0 votes
0 answers
118 views

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....
Slevin's user avatar
  • 361
3 votes
1 answer
83 views

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 ...
Gürcan Kavakçı's user avatar
0 votes
0 answers
188 views

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 ...
linux_user36's user avatar
0 votes
1 answer
232 views

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 ...
user15583968's user avatar
0 votes
1 answer
679 views

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

I had the following code: @Nonnull @SneakyThrows private Pair<InputStream, Long> probeSize(@Nonnull final InputStream image) { final String tmpId = UUID.randomUUID().toString(); ...
maress's user avatar
  • 3,533
1 vote
1 answer
527 views

Why BufferedWriter is not writing into the file over the opened FileChannel?

Below is my code. I am able to successfully read the file. But not able to re-write onto it with the replaced content using BufferedWriter. But can do it with ByteBuffer. Any suggestions please where ...
raikumardipak's user avatar
0 votes
1 answer
754 views

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....
M.E.'s user avatar
  • 4,977
-1 votes
1 answer
864 views

Multithread AES encryption in Java

As the title say i'm trying to encrypt a file using 2 thread and to decrypt using 1 thread. Since encryption isn't thread safe i use FileChannel to specify the position where to read in the second ...
Marcus34's user avatar
0 votes
4 answers
243 views

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 ...
Royston Furtado's user avatar
0 votes
1 answer
566 views

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(); ...
Smithfield's user avatar
1 vote
1 answer
44 views

Java: READ and WRITE are "ambiguous" when using FileChannel with ByteChannel?

I am learning Java through an introductory course using the textbook, Java Programming 9th Edition by Joyce Farrel. The examples and exercises are written for Java 9e, however, I am using Java SE 14. ...
Navraj Bains's user avatar
2 votes
2 answers
390 views

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 = ...
stijn van crombrugge's user avatar
1 vote
0 answers
129 views

Reading and writing the file from and to winSCP from the S3 object store

I am trying to put and read file from the remote file system using winSCP through an SFTP connection. The leaf node of the file system is s3 object store which contain the files (for eg: xyz.txt). ...
SSharma2203's user avatar
0 votes
0 answers
277 views

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 ...
Wills Seet's user avatar
2 votes
0 answers
34 views

Cannot delete temporary file after writing to it via a MappedByteBuffer [duplicate]

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(...
Michael's user avatar
  • 4,752
1 vote
0 answers
153 views

How to convert FileChannel object into file?

I am getting below error: ClassCastException: class sun.nio.ch.FileChannelImpl cannot be cast to class java.io.File Please help
Adi Mukherjee's user avatar
0 votes
1 answer
534 views

Getting OverlappingFileLockException when locking file channel in Java

Here's the part of the code that is problematic: FileChannel fileChannel = FileChannel.open(filePath, StandardOpenOption.WRITE); fileChannel.force(true); FileLock lock = fileChannel.lock(); ...
A6SE's user avatar
  • 177
1 vote
1 answer
223 views

Cannot read file in chunks with MappedByteBuffer

During read ops i get java.io.IOException: Channel not open for writing - cannot extend file to required size I have written simple program that reads file with MappedByteBuffer. The idea is to read ...
jspens's user avatar
  • 11
0 votes
0 answers
89 views

Obtaining the file key of an already-open file channel

Is there a way to obtain the file attributes (specifically, the file key) of an already-open file channel? This operation would be similar to the POSIX fstat function. If I know a path of the file, ...
Florian Weimer's user avatar
1 vote
1 answer
237 views

Read JPEG magic number with FileChannel and ByteBuffer

I started digging into Java NIO API and as a first try I wanted to read a JPEG file magic number. Here's the code import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.channels....
aculnaig's user avatar
1 vote
1 answer
493 views

FileChannel and ByteBuffer writing extra data

I am creating a method that will take in a file and split it into shardCount pieces and generate a parity file. When I run this method, it appears that I am writing out extra data into my parity file....
Levi Muniz's user avatar
0 votes
1 answer
147 views

java.io.InputStream#available() method equivalent in nio package

The class FileInputStream has a method available() that returns the remainging size to be consumed I'm trying to convert a program that uses FileInputStream to use FileChannel, I konw that we can ...
octopus's user avatar
  • 13
2 votes
1 answer
390 views

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 ...
PoriPiriPuri's user avatar
0 votes
1 answer
2k views

Multithreaded access to files in Java

I'm working on a multithreaded server in Java. The server monitors a directory of files. Clients can ask the server: to download a file from the server directory to upload a new version of an already ...
PoriPiriPuri's user avatar
16 votes
2 answers
2k views

Using mp4parser , how can I handle videos that are taken from Uri and ContentResolver?

Background We want to let the user choose a video from any app, and then trim a video to be of max of 5 seconds. The problem For getting a Uri to be selected, we got it working fine (solution ...
android developer's user avatar
0 votes
1 answer
202 views

Android - Using Filechannel to copy file at sd card isn't possible?

I made this method to copy file from inputpath to outputpath. It works when input is located in sdcard, and output is located in local storage, But when I change output to a file located in sdcard, it ...
Yoo Minjun's user avatar
-3 votes
1 answer
322 views

How to debug "Java NIO Socket Channel mismatch in size of sent and received bytes"?

Please look at this question. This is exact what I'm looking for but I didn't reach it yet. I'm sending multiple of images over a socketChannel. I try to attach the image size into a (cleared ...
AppFell's user avatar
0 votes
2 answers
770 views

Is FileChannel.position() thread safe?

Is it safe to write from different threads to one file using FileChannel.position()? This is necessary for multipart downloading. Each thread will write to its position in the file, i.e. the positions ...
proninyaroslav's user avatar
2 votes
1 answer
1k views

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): ...
Kalle Richter's user avatar
27 votes
3 answers
6k views

Is there a way to prevent ClosedByInterruptException?

In the following example, I have one file being used by two threads (in the real example I could have any number of threads) import java.io.File; import java.io.IOException; import java.io....
Peter Lawrey's user avatar
1 vote
1 answer
307 views

Why the fileChannel.read loop never end?

I try to use nio to read a small text which contains only 5 characters,however, the fileChannel.read loop never go to end. public static void main(String[] args) throws IOException { ...
HongyanShen's user avatar
  • 1,435
2 votes
1 answer
374 views

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[]) { ...
gesanri's user avatar
  • 237
0 votes
2 answers
4k views

Copy image from one folder to another - Android

Hi Everyone I am trying to copy an image from one folder to another which user selects from the gallery. It's not throwing any error as well. Please check the below code. protected void ...
Mac_Play's user avatar
  • 302
0 votes
1 answer
294 views

ObjectInputStream throws ioexception inside the filelock lock section

I have a function, that has to put key value pair into map stored in file. If file does not exist, I have to create it. I try to lock the file, when I work with it with Filelock lock(). But when I try ...
neverEugene's user avatar
0 votes
1 answer
290 views

Java insert bytes to file at given position without overwriting any data

I have a List backed by (among other things) a FileChannel (of a relatively large file), enabling random reads to objects. The implementations of get(), size(), and add() were fairly straightforward. ...
MeetTitan's user avatar
  • 3,393
1 vote
4 answers
4k views

Java FileChannel Vs BufferedReader - Spring Batch - Reader

We process huge files (sometimes 50 GB each file). The application reads this one file and based on the business logic, it will write multiple output files (4-6). The records in the file are of ...
Sanjeev's user avatar
  • 119
7 votes
0 answers
494 views

How do memory-mapped files work in a multi-process scenario?

This question has been confusing me for days: Assume that I have two processes (p_write and p_read) which run on the same machine. Process p_write is for writing/updating the mmap file. Process ...
Lubor's user avatar
  • 989
-1 votes
2 answers
498 views

Spaces added when writing files

I'm modifying the source code of H2 MVStore 1.4.191 to write files by doing some thread sleep. The big change is that the file is not written in one time anymore, but by 2^16 bytes chunks. MVStore ...
Joss29's user avatar
  • 19
0 votes
0 answers
608 views

Memory is not released while using MappedByteBuffer

I am trying to read one big file in chunk . So the read operation will be called multiple times being offset one of the parameter . The read is working perfectly fine . But the real problem is ...
atanu2destiny's user avatar
2 votes
0 answers
165 views

Why and when we need File Channels in java?

This is the very time I came across working with File Channels. Where I'm putting a lock on channel to lock the process to avoid other process to use it. While reading online, I got to know that ...
Always Thinking's user avatar
1 vote
1 answer
2k views

How to use DigestInputStream to calculate MD5 sum when using FileChannel?

I'm doing multiple parallel HTTP range requests and want to calculate the MD5 sum of each response using DigestInputStream. I also want to write the data from the HTTP stream to a file without ...
Florian Feldhaus's user avatar
1 vote
3 answers
5k views

Java NIO scan through ByteBuffer for certain bytes and word with sections

Okay, so I'm trying to do something that seemed like it should be fairly simple, but with these new NIO interfaces, things are confusing the hell out of me! Here's what I'm trying to do, I need to ...
Joshua Chambers's user avatar
4 votes
1 answer
984 views

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

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 ...
DDoomUs's user avatar
  • 183
19 votes
2 answers
4k views

Concurrency of RandomAccessFile in Java

I am creating a RandomAccessFile object to write to a file (on SSD) by multiple threads. Each thread tries to write a direct byte buffer at a specific position within the file and I ensure that the ...
user1715122's user avatar
0 votes
1 answer
3k views

Reading and writing file in number of Byte Buffer chunks of smaller length

I am trying to read file in ByteBuffer chunks of fixed length and then store it to a list of ByteBuffer and then after some operations read those ByteBuffer chunks in a sequential order to reconstruct ...
andi99's user avatar
  • 117
0 votes
1 answer
111 views

i want java stream channel like filewriter

i just want fileoutputstream like filewriter at same style like this //filewriter code try { File file = ('some file link'); fw=new FileWriter(file,true); fw.write("...
LocketGoma's user avatar
0 votes
1 answer
9k views

java.nio.channels.AsynchronousClose Exception on Java 8

I just moved my code from java 7 windows to java 1.8 Linux and i am getting this stack trace on same code base java.nio.channels.AsynchronousCloseException at sun.nio.ch....
Aravind R's user avatar
  • 736