Questions tagged [java-io]

The java.io package provides blocking input and output through data streams, serialization, and the file system.

Most applications need to process some input and produce some output based on that input. The purpose of the Java IO package (java.io) is to make that possible in Java.

The java.io package provides blocking input and output through data streams, serialization and the file system.

1753 questions
5
votes
2 answers

Why is my program denying access to create a file?

In the book I'm reading on Java, it demonstrates serialization by using a program that writes to a file and stores it away. I'm getting a strange error that I don't know how to read and it denies me access to creating a .txt file. Here's the…
hasherr
  • 669
  • 3
  • 14
  • 28
5
votes
3 answers

What does System.in.read actually return?

What does : System.in.read() return ? The documentation says : Returns: the next byte of data, or -1 if the end of the stream is reached. But for example if I enter : 10 I get back 49 . Why is that ?
saplingPro
  • 20,769
  • 53
  • 137
  • 195
5
votes
4 answers

when does FileInputStream.read() block?

The question is similar to the following two questions. Java InputStream blocking read Why is the FileInputStream read() not blocking? But I still cannot fully understand it. So far I think the read() method in following code will block due to the…
liam xu
  • 2,892
  • 10
  • 42
  • 65
5
votes
4 answers

PrintWriter add text to file

In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. I have gotten almost every aspect of it to work save one. I need to write the surface gravity to a file using a…
spenserpro
  • 53
  • 1
  • 1
  • 3
5
votes
3 answers

BufferedReader, detecting if there is text left to read

I'm running a thread and everytime it runs, It should be checking to see if there is a new line to read from the BufferedReader although, it gets stuck waiting for a line to exist, thus halting the entire code. if((inputLine =…
user1729831
  • 71
  • 3
  • 9
5
votes
1 answer

Identifying Mapped drive from local drive

I am listing all available drives in my desktop using File.listRoots() in Java. I have some Mapped drives. When I list the roots it is fetching me local drives as well as mapped drives. In order to exclude the mapped drives I used following code…
4
votes
6 answers

Find first file in directory with Java

I have some sort of batch program that should pick up a file from a directory and process it. Since this program is supposed to: run on JDK 5, be small (no external libraries) and fast! (with a bang) ...what is the best way to only pick one file…
stephanos
  • 3,319
  • 7
  • 33
  • 47
4
votes
1 answer

Reading integer values from binary file using Java

I am trying to write values greater than 256 using DataOupPutStream.write() method. When i try reading the same value using DataInputStream.read() it will return 0. So, i used DataOutputStream.writeInt() and DataInputStream.readInt() methods to…
rozar
  • 1,058
  • 3
  • 15
  • 28
4
votes
1 answer

Can we read and write to a file concurrently?

Suppose I have two threads (threadA and threadB), where threadA is reading from a file et threadB is writing to this same file, here are the two methods : // Thread A reads java.nio.file.Files.readAllBytes(Path.get("test.txt")); // Thread B…
Olivier Boissé
  • 15,834
  • 6
  • 38
  • 56
4
votes
3 answers

Multipart transferTo looks for a wrong file address when using createTempFile

I have the following java code in Spring framework to convert a multipart file to a regular file: public static File convertToFile(MultipartFile multipart) { File convFile = null; try { convFile =…
Arian
  • 7,397
  • 21
  • 89
  • 177
4
votes
2 answers

Byte Stream vs Character Stream in Java

I am working on I/O classes in Java. I understand that there are two important type of streams: byte stream and character stream. But... I have tried to read and write text file with byte stream and it worked. Here is the code: File klasor = new…
user8177292
4
votes
1 answer

Create folder in a mapped network drive using Java

I'm trying to create a folder to a mapped network drive Z: using Java Unfortunately the file is not getting created. This is what I'm doing: File file = new File("Z:/file1"); file.mkdir(); This code works when not using mapped network drive. This…
Marquinio
  • 4,601
  • 13
  • 45
  • 68
4
votes
1 answer

java.io.File.listFiles vs java.nio.Files.list and its thrown IOException

Why does Files.list throw an IOException whereas File.listFiles doesn't? Looking at the source code of Files.list (Java 8) I am even more curious why there wasn't thrown an UncheckedIOException as it is also thrown inside the iterator. If I replace…
Roland
  • 22,259
  • 4
  • 57
  • 84
4
votes
0 answers

Writing to ZipOutputStream by multiple thread

Problem: I need to download lot of files(size of file can be upto 2GB) and send those as zip to the client requesting for resource. I was looking to parallelize that operation. Currently the library I've to use takes OutputStream (in my case it's…
User5817351
  • 989
  • 2
  • 16
  • 36
4
votes
2 answers

Read and delete files from folder

I am trying to write a program to read each file from a folder and after the contents in the file are read the file should get deleted. I could write programs to read the files from a folder and delete files from a folder separately, but couldn't…
Profiesto
  • 43
  • 1
  • 3