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
2
votes
1 answer

Java NIO FileChannels Track Progress

I'm trying to download a mp4 file from the web. I'd like to do it async and track the progress so it can be displayed in a progressbar. My code looks as following: URLConnection con = url.openConnection(); ReadableByteChannel rbc =…
Zackline
  • 804
  • 1
  • 9
  • 28
2
votes
0 answers

How to copy files over socket using Java NIO FileChannel?

I am supposed to implement a simple Client/Server application similar to FTP Server/Client . Both the client and the server should be able to send and receive files, but for now let's address just sending a file from the server to the client. There…
Maverick
  • 62
  • 1
  • 9
2
votes
2 answers

How use Java FileChannel to copy preserving timestamps

How use Java FileChannel to copy preserving timestamps for files and directories? Looks like the files are not preserving timestamps while copying to another location. How is that possible using FileChannel in Java?
Marvado
  • 331
  • 1
  • 3
  • 11
2
votes
2 answers

Copy large files in fastest way possible

I tried to find a way to copy large files in fastest way possible... import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; public class FastFileCopy { public static void…
AvB
  • 171
  • 1
  • 2
  • 12
2
votes
1 answer

FileChannel map - Cannot extend for writing

I'm opening large file (~ 200 MB) with RandomAccessFile and then get Channel for it. I'm trying to map some data to MappedByteBuffer, but I'm getting exception: Channel not open for writing - cannot extend file for required size. I can't figure…
user2160375
2
votes
1 answer

How to concatenate two files in Java using transferFrom

Below is my code. Have I correctly noted that transferFrom does not modify the position of the channel being transferred to? This does not work. It just copies the first file. Regards. File file1 = new File(binDirectory + "/file1.avi"); File file2 =…
Danny Rancher
  • 1,923
  • 3
  • 24
  • 43
2
votes
4 answers

why is java.nio.FileChannel transferTo() and transferFrom() faster??? Does it use DMA?

Why is java.nio.FileChannel transferTo() and transferFrom() faster than byte-by-byte transfer (stream based or using ByteBuffer) on some JVM/OS combinations??? Do these methods use direct memory access (DMA) as opposed to issuing interrupt requests…
murungu
  • 2,090
  • 4
  • 21
  • 45
2
votes
2 answers

File Channel in C++

In java, there are FileChannels where I can read from file channel. I can also set the position in the channel where I want to start reading. Any similar functions in C++/C?
user1785771
  • 487
  • 2
  • 7
  • 18
2
votes
1 answer

Does RandomAccessFile.close() internally call FileChannel.force()?

I am using RandomAccessFile to perform some writes to a file as part of a transaction. Before I commit my transaction, I want to be absolutely sure that the data is written to disk. Calling force(boolean) on the RAF's FileChannel appears to provide…
Chris B
  • 9,149
  • 4
  • 32
  • 38
2
votes
3 answers

Java: Writing with FileChannel to a file make the file shrink?

I tried to write specific bytes to certain position of a file using FileChannel. But actually the file shrink to the last position where I write change. I do it like this: Path path = Paths.get("I://music - Copy.mp3"); …
Henry
  • 43
  • 9
1
vote
2 answers

Disadvantage of FileChannel -> BufferedReader -> Reader

FileChannel will faster than BufferedReader, BufferedReader will more faster than Reader because FileChannel and BufferedReader has cut off some itermediate steps to receive data. My question is : the advantage is obvious, but I don't see any…
hqt
  • 29,632
  • 51
  • 171
  • 250
1
vote
4 answers

Remove first line from delimited file

I have a delimited file which can contain around millions of records , now I want to delete the first line from the delimited file before processing it further. The length of the first line is variable , it differs as per the file type.. now I have…
Vivek
  • 41
  • 1
  • 5
1
vote
1 answer

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 I am going wrong. myFile.txt…
raikumardipak
  • 1,461
  • 2
  • 29
  • 49
1
vote
2 answers

Filechannel position and string length

In Java chars are 2 bytes long, But when I write a string to a file using a bytebuffer the filechannel position increments by the number of chars. I read that the Filechannel.position() method returns the number of bytes from the beginning of the…
dasman
  • 311
  • 3
  • 16
1
vote
1 answer

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. I've successfully navigated Java API and found…