0

I have been learning Java for the past couple of months now and while I have gotten to grips with just about everything, input and output redirection are causing me somewhat of a problem. The book I am using (Introduction to Java Programming, Ninth Edition) brings up the concept fairly early on and references it on and off throughout the book and while I have managed to get away with not knowing exactly how to do it, I feel it will set me back considerably soon if I don't get to grips with it soon.

So, my question is how do I do it? So far I understand that input redirection is done by

java ClassName < input.txt

And Output by

java ClassName > Output.txt

in the Command Prompt. This is basically all the book gives in respect to the topic. Yet I've had limited success with this. Such that I could get it to work by fully directing it to the folder where both ClassName.class and input.txt is saved. For input redirection to work, do both the class file and .txt file have to be situated in the same folder?

If so what are the commands for use in cmd to change the folders in which it is directed towards?

Then for output redirection i have been unsuccessful in getting this to work, does output redirection create a .txt file in which it will store my output data or do i need to create that .txt file before I can use output redirection?

For reference I have organised my work through the book into chapters so that each program created will be easy to find in the eventuality that I need to. they are stored as followed:

C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming

for Chapter 2 respectively.

This is my first post on StackOverflow so any advice is appreciated and I apologies if I'm unclear in what I'm asking as I am not yet proficient in Java and what everything means as of yet

lukewestby
  • 1,197
  • 7
  • 15
Benasbo12
  • 9
  • 1
  • The title and tags refer to JavaScript while it looks the post is all about Java. Java and JavaScript are totally different things. It is fair to remove the JavaScript tag and reference in title. BUT these 2 should help: http://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file and http://examples.javacodegeeks.com/core-java/io/inputstream/read-line-of-chars-from-console-with-inputstream/ – tiblu Oct 04 '15 at 20:36

2 Answers2

0

With the commands you have above, everything will have to be in the same folder, or will be created in the same folder. Think of the second argument, the filename, as a reference to where that file can be found (in the case of input) or where that file will be placed (in the case of output).

If you just specify a simple filename (without a directory structure in front of it), the system will assume that file will be read in or created in the current directory (like your Chapter2ElementaryProgramming).

So if you're in:

C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming

and ClassName.class and input.txt are also in C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming, you can get away by simply doing as you've described above.

If you have ClassName.class somewhere else, but want to read in the input.txt from wherever you call java ClassName, you can do:

java ClassName < C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming\input.txt (assuming you have the input.txt in that directory).

The same applies for output.txt. Except for output, just think of it as capturing what was naturally outputting to the screen to a file instead. So if you just run java ClassName, and it spits out a bunch of output, then if you do java ClassName > output.txt, all it does is save all that output directly to the output.txt instead of showing it to your screen.

Again, if you want output.txt to end up somewhere different than where you have ClassName.class, you can give it the full directory to where you want it to go, like:

java ClassName > C:\Users\Lenovo\Documents\NetBeansProjects\LearningJava\src\Chapter2ElementaryProgramming\output.txt

I hope that helps, if this was indeed what you were asking.

As for something successfully outputting or inputting, that part, as suggested, might help to see the code to make sure it is programmatically sound.

Good luck.

subdigit
  • 431
  • 4
  • 9
  • This is simply untrue. `ClassName` has to be on the CLASSPATH. The default CLASSPATH is `'.'`, and *if that's all he's using* it implies that `ClassName.class` has to be in the current directory. But not otherwise. – user207421 Oct 05 '15 at 00:08
  • I can assure you, the programming is sound as its from a book as I havent created any programs using it until ive used it successfully with the given program. Your answer is much appreciated and was exactly what I was lookung for. – Benasbo12 Oct 05 '15 at 12:58
0
java ClassName < input.txt
  • Runs a class called ClassName that has to be available on the CLASSPATH. If you're using the default CLASSPATH of '.', that means that ClassName.class must be in the current directory.
  • Redirects the input from a file called input.txt in the current directory.
user207421
  • 289,834
  • 37
  • 266
  • 440