Questions tagged [io]

In computing, input/output, or I/O, refers to the communication between an information processing system (such as a computer), and the outside world, possibly a human, or another information processing system.

In computing, input/output, or I/O, refers to the communication between an information processing system (such as a computer), and the outside world, possibly a human, or another information processing system.

(Source: Wikipedia [Input/Output])


I/O includes tasks such as:

  • Reading and writing files ()
  • Interacting with a user through a command-line terminal session ( / / )
  • Connecting programs so that one sends its output to the next (s)
  • Sending data over a network in packets or as a data stream ()

For the Io language, see

17482 questions
4673
votes
64 answers

How do I read / convert an InputStream into a String in Java?

If you have a java.io.InputStream object, how should you process that object and produce a String? Suppose I have an InputStream that contains text data, and I want to convert it to a String, so for example I can write that to a log file. What is…
Johnny Maelstrom
  • 47,581
  • 5
  • 21
  • 18
2041
votes
16 answers

Looping through the content of a file in Bash

How do I iterate through each line of a text file with Bash? With this script: echo "Start!" for p in (peptides.txt) do echo "${p}" done I get this output on the screen: Start! ./runPep.sh: line 3: syntax error near unexpected token…
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1753
votes
35 answers

How do I create a Java string from the contents of a file?

I've been using the idiom below for some time now. And it seems to be the most wide-spread, at least on the sites I've visited. Is there a better/different way to read a file into a string in Java? private String readFile(String file) throws…
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
1521
votes
11 answers

How to redirect output to a file and stdout

In bash, calling foo would display any output from that command on the stdout. Calling foo > output would redirect any output from that command to the file specified (in this case 'output'). Is there a way to redirect output to a file and have it…
SCdF
  • 57,260
  • 24
  • 77
  • 113
998
votes
22 answers

How can I read a large text file line by line using Java?

I need to read a large text file of around 5-6 GB line by line using Java. How can I do this quickly?
manoj singh
  • 10,045
  • 3
  • 15
  • 3
976
votes
19 answers

How do I check if a file exists in Java?

How can I check whether a file exists, before opening it for reading in Java (the equivalent of Perl's -e $filename)? The only similar question on SO deals with writing the file and was thus answered using FileWriter which is obviously not…
DVK
  • 126,886
  • 32
  • 213
  • 327
935
votes
25 answers

Scanner is skipping nextLine() after using next() or nextFoo()?

I am using the Scanner methods nextInt() and nextLine() for reading input. It looks like this: System.out.println("Enter numerical value"); int option; option = input.nextInt(); // Read numerical value from input System.out.println("Enter 1st…
blekione
  • 10,152
  • 3
  • 20
  • 26
784
votes
35 answers

How to read all files in a folder from Java?

How to read all the files in a folder through Java? It doesn't matter which API.
M.J.
  • 16,266
  • 28
  • 75
  • 97
769
votes
31 answers

How to append text to an existing file in Java?

I need to append text repeatedly to an existing file in Java. How do I do that?
flyingfromchina
  • 9,571
  • 12
  • 35
  • 38
692
votes
8 answers

Calculate the execution time of a method

Possible Duplicate: How do I measure how long a function is running? I have an I/O time-taking method which copies data from a location to another. What's the best and most real way of calculating the execution time? Thread? Timer? Stopwatch? Any…
Mahdi Tahsildari
  • 13,065
  • 14
  • 55
  • 94
663
votes
6 answers

Get an OutputStream into a String

What's the best way to pipe the output from an java.io.OutputStream to a String in Java? Say I have the method: writeToStream(Object o, OutputStream out) Which writes certain data from the object to the given stream. However, I want to get this…
Adrian Mouat
  • 44,585
  • 16
  • 110
  • 102
637
votes
9 answers

How to use StringIO in Python3?

I am using Python 3.2.1 and I can't import the StringIO module. I use io.StringIO and it works, but I can't use it with numpy's genfromtxt like this: x="1 3\n 4.5 8" numpy.genfromtxt(io.StringIO(x)) I get the following error: TypeError:…
Babak Abdolahi
  • 6,559
  • 3
  • 15
  • 4
620
votes
31 answers

A non-blocking read on a subprocess.PIPE in Python

I'm using the subprocess module to start a subprocess and connect to its output stream (standard output). I want to be able to execute non-blocking reads on its standard output. Is there a way to make .readline non-blocking or to check if there is…
Mathieu Pagé
  • 10,764
  • 13
  • 48
  • 71
568
votes
33 answers

How do I get the file extension of a file in Java?

Just to be clear, I'm not looking for the MIME type. Let's say I have the following input: /path/to/file/foo.txt I'd like a way to break this input up, specifically into .txt for the extension. Is there any built in way to do this in Java? I would…
longda
  • 10,153
  • 7
  • 46
  • 66
502
votes
24 answers

Easy way to write contents of a Java InputStream to an OutputStream

I was surprised to find today that I couldn't track down any simple way to write the contents of an InputStream to an OutputStream in Java. Obviously, the byte buffer code isn't difficult to write, but I suspect I'm just missing something which…
Matt Sheppard
  • 116,545
  • 46
  • 111
  • 131
1
2 3
99 100