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

16399 questions
4332
votes
60 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
  • 44,165
  • 5
  • 19
  • 18
1616
votes
15 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
  • 28,342
  • 21
  • 95
  • 123
1614
votes
32 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
  • 184,433
  • 106
  • 369
  • 548
1173
votes
10 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
  • 51,261
  • 23
  • 74
  • 108
899
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
  • 9,047
  • 3
  • 13
  • 3
895
votes
17 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
  • 119,765
  • 29
  • 201
  • 317
796
votes
21 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
  • 8,666
  • 3
  • 15
  • 23
729
votes
32 answers

How to read all files in a folder from Java?

How to read all the files in a folder through Java?
M.J.
  • 14,866
  • 25
  • 70
  • 95
717
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
  • 8,991
  • 11
  • 33
  • 36
605
votes
5 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
  • 38,986
  • 15
  • 98
  • 99
596
votes
7 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
  • 11,900
  • 14
  • 50
  • 89
542
votes
9 answers

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:…
user1591744
  • 5,509
  • 2
  • 12
  • 4
541
votes
29 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,075
  • 11
  • 45
  • 70
520
votes
30 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
  • 9,445
  • 6
  • 44
  • 66
468
votes
23 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
  • 111,039
  • 46
  • 105
  • 128
1
2 3
99 100