2

I would like to know how I can convert standard input to a string. For instance, I have a txt file with n amount of letters - all these I want to read into one string.

Just to make sure: standard input is when you give the program a .txt as input.

Help would be appreciated!

Marc Mutz - mmutz
  • 22,883
  • 10
  • 72
  • 86
ISJ
  • 501
  • 2
  • 8
  • 15
  • possible duplicate of [In Java how do a read/convert an InputStream in to a string?](http://stackoverflow.com/questions/309424/in-java-how-do-a-read-convert-an-inputstream-in-to-a-string) – BalusC Mar 04 '11 at 15:30
  • Do you have a file, which you want to use as standard input, or do want to read standard input, and store this input in a string? – aioobe Mar 04 '11 at 15:36
  • @aioobe I have a file and I want to store the data of it in a string. – ISJ Mar 04 '11 at 15:38
  • With "give a.txt as input" you mean "program < a.txt" or "program a.txt"? I'd understand the first case as referring to standard input. – Axel Mar 04 '11 at 15:55
  • @Axel Thats what I mean, yes! The first case. java program < a.txt – ISJ Mar 04 '11 at 16:06

5 Answers5

3

I have a file and I want to store the data of it in a string.

This can be done using commons-io:

String content = FileUtils.readFileToString(file);

(complete example)

To read from stdin to a string, you could use a scanner. Use while (scanner.hasNextLine()) if you want the entire file.

String firstLineFromStdin = new Scanner(System.in).readLine();
aioobe
  • 383,660
  • 99
  • 774
  • 796
  • I get the following error: ValidDNA.java:26: cannot find symbol symbol : variable FileUtils location: class ValidDNA String s = FileUtils.readFileToString(ValidDNA_example_1.txt); I did Import java.io.File – ISJ Mar 04 '11 at 15:59
  • Will i be able to read in any file? It seems as if this only allow me to read in the same file over and over again. I can't change it by using: java program < a.txt – ISJ Mar 04 '11 at 16:08
  • Ah, no, if you do `program < a.txt` then you should go with the `Scanner` solution that I suggested. I'll put it in the answer here. – aioobe Mar 04 '11 at 16:20
  • 1
    `Scanner` does not provide a method `readLine()`, it is `nextLine()`. – Konrad Reiche Feb 02 '13 at 22:58
0

What you want to do is pipe the file into your application...

java your.package < file.txt

Then you can read the file as normal via Java's System.in.

Andrew White
  • 50,300
  • 17
  • 108
  • 132
  • OP is basically asking how to read `InputStream` into a `String`. – BalusC Mar 04 '11 at 15:34
  • @BalusC, I'm not familiar with the term InputStream. What exactly is it? Also what exactly is standard input. Anything that's being written from a file? – ISJ Mar 04 '11 at 15:41
  • You said that you want to read from standard input. Thus, `System.in`. Guess what it returns? – BalusC Mar 04 '11 at 15:42
0

First, do you want to give a filename as a argument with your program. Or the whole file as input? In the first case you do:

"program filename.txt" 

In the second you input:

"program < filename.txt".

In the first case the filename is the input. And your program will have to open a file itself. In the second case the contents of the file are given as input of the file.

If you only give the filename, the filename is in the arguments of your main function (the array args of the "main(String args[])" part. Using this filename you can then use the earlier suggested readFileToString to convert the contents of the file into a string.

If you want to use the other method of file input "program < filename.txt", use the a InputStream for that. See the documentation for more information about InputStream http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html

You also mention you are rather new to Java. I hope you do know of the existence of the java documentation? http://download.oracle.com/javase/6/docs/api/index.html contains a lot of information you might want to know.

Ids
  • 198
  • 1
  • 12
0

I end up using this:

    String s = "";

    while (!StdIn.isEmpty()){

        s = s + StdIn.readChar();

    }

I ran the program using: java program < a.text

ISJ
  • 501
  • 2
  • 8
  • 15
-1

You could try something like

System.setIn(new FileInputStream(filename));

Here's an example on redirecting standard i/o

Bala R
  • 101,930
  • 22
  • 186
  • 204
  • Ok, I didn't even know you could do that. I'm not sure if that's exactly what he is looking for but I learned something none-the-less. – Andrew White Mar 04 '11 at 15:32