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
15
votes
3 answers

How to use flush() for PrintWriter

I have some codes like this: PrintWriter pw = new PrintWriter(new BufferedReader(....)); for(int i=0; i<10; i++) { pw.println("a"); pw.flush();// flush each time when println()? } pw.close(); Is the flush() in each 'for' statement…
janekey
  • 264
  • 1
  • 3
  • 9
15
votes
2 answers

What's the benefit of Async File NIO in Java?

According to the documentation of AsynchronousFileChannel and AsynchronousChannelGroup, async NIO is using a dedicated thread pool where "IO events are handled". I couldn't find any clear statement what "handling" means in this context but according…
Peter
  • 323
  • 1
  • 16
  • 46
15
votes
2 answers

Java: RandomAccessFile Mode "rws" vs "rwd"?

The RandomAccessFile constructor accepts a mode string specifying how a file should be open. I'm confused about the difference between "rws" and "rwd" modes. Here's what the docs state: "rws" Open for reading and writing, as with "rw", and also…
Cristian Diaconescu
  • 34,633
  • 32
  • 143
  • 233
14
votes
2 answers

Java Scanner(File) misbehaving, but Scanner(FIleInputStream) always works with the same file

I am having weird behavior with Scanner. It will work with a particular set of files I am using when I use the Scanner(FileInputStream) constructor, but it won't with the Scanner(File) constructor. Case 1: Scanner(File) Scanner s = new Scanner(new…
kashiko
  • 173
  • 1
  • 6
14
votes
3 answers

Using AutoClosable interfaces inside Stream API

Today I tried to refactor this code, that reads ids from files in a directory, Set ids = new HashSet<>(); for (String fileName : fileSystem.list("my-directory")) { InputStream stream = fileSystem.openInputStream(fileName); …
AdamSkywalker
  • 11,408
  • 3
  • 38
  • 76
14
votes
2 answers

Reading a text file line by line in android

hi i just started learning android development and i'm trying to build an app that reads text from files. i have been searching all over the internet but i don't seem to find the way to do so , so i have a few questions.. 1.how to do this? what is…
user3668496
  • 166
  • 1
  • 1
  • 8
14
votes
4 answers

Why does FileOutputStream throw FileNotFoundException?

The Android Developer reference (this page) says: Throws FileNotFoundException But at the very start, it says: Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist. If…
1owk3y
  • 1,115
  • 1
  • 15
  • 30
13
votes
9 answers

How to find out which line separator BufferedReader#readLine() used to split the line?

I am reading a file via the BufferedReader String filename = ... br = new BufferedReader( new FileInputStream(filename)); while (true) { String s = br.readLine(); if (s == null) break; ... } I need to know if the lines are separated by…
chacko
  • 5,004
  • 9
  • 31
  • 39
13
votes
5 answers

Checking if a number entered is odd

`I'm not sure what code to insert or even where, but I would like to check the number I enter is an odd number. import java.io.*; import javax.swing.JOptionPane; public class Diamond { public static void main(String [] args) throws IOException…
Mike
  • 2,293
  • 13
  • 42
  • 56
13
votes
4 answers

Java: opening and reading from a file without locking it

I need to be able to mimic 'tail -f' with Java. I'm trying to read a log file as it's being written by another process, but when I open the file to read it, it locks the file and the other process can't write to it anymore. Any help would be greatly…
rogue780
  • 155
  • 1
  • 1
  • 9
13
votes
3 answers

Robust skipping of data in a java.io.InputStream and its subtypes

I'm processing a binary stream and need to skip efficiently past a range of data that I'm not interested in, to some data that will be processed. InputStream.skip(long) doesn't make much in the way of guarantees: Skips over and discards n bytes of…
Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
12
votes
4 answers

Sockets: BufferedReader readLine() blocks

I am using BufferedReader.readLine() method to read a response from a remote server (which is written in C and I have no access to source code). BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while((line =…
neo
  • 2,461
  • 9
  • 42
  • 67
12
votes
3 answers

Java File.exists() versus File.isFile()

I'm unable to think of a realistic use case for the method java.io.File.exists() or its equivalent in Java 7 java.nio.file.Files.exists(Path). It seems that isFile() or isDirectory() would be preferable in all cases (or canRead(), canWrite(),…
jaco0646
  • 15,303
  • 7
  • 59
  • 83
12
votes
5 answers

InputStream or Reader wrapper for progress reporting

So, I'm feeding file data to an API that takes a Reader, and I'd like a way to report progress. It seems like it should be straightforward to write a FilterInputStream implementation that wraps the FileInputStream, keeps track of the number of bytes…
David Moles
  • 48,006
  • 27
  • 136
  • 235
12
votes
5 answers

hadoop java.io.IOException: while running namenode -format

I ran namenode -format.This is my output. I tried changing the file permissions chmod 777 hadoop. I believe this line is the error ERROR namenode.NameNode: java.io.IOException: Cannot create directory…
alex
  • 398
  • 1
  • 6
  • 24