Questions tagged [java-nio]

Use the tag for questions related with the Java package java.nio

Java Package java.nio defines buffers, which are containers for data, and provides an overview of the other NIO packages.

Oracle java-nio Package Description

39 questions
1
vote
0 answers

How to read a specific range of bytes from a GCS Blob?

I would like to read the contents of a GCS blob in chunks of a specified size. I wrote a test where I wanted to retrieve the last 5000 bytes of a 10,000 byte file stored as a GCS blob. The file consisted of 1K of "0"s, followed by 1K of "1"s, 1K…
kc2001
  • 5,008
  • 4
  • 51
  • 92
1
vote
1 answer

JAVA NIO package, getting unexpected network error occured

I have a code in which I read images on a network drive. i read thousands of images, but only sometimes i get following exception occasionally. java.io.IOException: An unexpected network error occurred at…
sogytots
  • 35
  • 4
1
vote
1 answer

How does Java NIO constructs - socketchannel and socketserverchannel work internally?

I have some specific questions related to how socketchannel and socketserverchannel work:- When a ServerSocketChannel accepts a connection( please refer to code below), my understanding is it somehow moves the client to a new randomly selected…
user3740951
  • 1,109
  • 2
  • 20
  • 39
0
votes
1 answer

Java NIO: How can I read into a buffer possibly multiple times?

I am working on a Java project. I have the following code that is part of the Selector's while loop. The response is supposed to be less than 256 and it ends with a line break. But as far as I understand, it is possible (even though I didn't observe…
sleekster
  • 15
  • 5
0
votes
2 answers

How to check content-length match after download image in java Nio?

static void downloadWebp(String url) { URL url1; try { url1 = new URL(url); } catch (MalformedURLException e) { throw new RuntimeException(e); } { // download webp …
djy
  • 737
  • 6
  • 14
0
votes
0 answers

java io: Create file name directly with its digest code

Currently, my code snippet: Path destinationFilepath = Paths.get("/tmp/a/b/c/" + UUID.randomUUID().toString()); FileUtils.createParentDirectories(path.toFile()); destinationFilepath = Files.createFile(path); File destinationFile =…
Jordi
  • 20,868
  • 39
  • 149
  • 333
0
votes
0 answers

Java download video from a given URL

I need to download a video in the given link and save to my local directory. This is the code I used for that. And I'm using java nio package. public void usingNio() throws Exception { ReadableByteChannel readChannel =…
0
votes
0 answers

How to avoid auto forward slash IntelliJ IDEA 2022.2.1 in Windows 10

I am using IntelliJ IDEA 2022.2.1 editor. In one of my test case I am reading the file from resource folder using below method getClass().getResource(filename) I can get the path, but problem is it's always returning with extra forward slash('/')…
Mak
  • 1,068
  • 8
  • 19
0
votes
2 answers

How to get the jar files in the specified directory and why is PathMatcher not working?

I'm trying to get *.jar files in a specified directory (no recursing subdirectories), I think my code is no problem, but the results are incomprehensible, am I doing something wrong? import java.io.*; import java.nio.file.*; import…
Xi Minghui
  • 95
  • 1
  • 9
0
votes
0 answers

How to upload hosted file over the Rest Template?

I want to pass a hosted file over the rest template to another end point. I have to pass the hosted file URL as a String. See the below code. HttpHeaders headers = new…
D.Anush
  • 147
  • 6
  • 15
0
votes
3 answers

How to delete Duplicate files in folder using Java-Stream?

I have a folder with the same photos but having different names and I want to remove the duplicates (doesn't matter which ones) using Stream API, to which I'm fairly new. I tried to use this method, but of course it's not that simple, and it's just…
VegetaSan
  • 35
  • 4
0
votes
0 answers

Generate the required directories while saving files with Files.writeString

I am saving a string as a file. import java.nio.file.Files; import java.nio.file.Path; String data = "hello, world"; Files.writeString(Path.of("upload-dir", "xyz", "abc", "data.txt"), data); I get the following error as xyz and abc are not…
Saif
  • 2,530
  • 3
  • 27
  • 45
0
votes
0 answers

Java BufferedInputStream.read() fails

I am working on an android app that has continuous communication between the phone and a local server on the computer through cable and Android Open Accessory connection(AOA), the server(the computer) keeps sending packets of bytes to the android,…
Mohammad Elsayed
  • 1,885
  • 1
  • 20
  • 46
0
votes
2 answers

How to get String array using Files.list()

I have a txt file with strings. I want to get a String array containing all lines from the file via Files.list(Paths.get("path")) method. I tried to use toArray() but it returns an array of paths to files in "path" directory How can I get it using…
miclelov
  • 35
  • 5
0
votes
1 answer

How to resolve error from classloader.getResource() method?

I am in a strange situation : String filename ="file"+ System.currentTimeMillis()+file.getOriginalFilename(); // suppose the file name is "file123256chart.docx" Path templatePath =…
Anmol Jain
  • 336
  • 4
  • 12