| | |
Redirecting console output to file
![]() |
•
•
Join Date: May 2005
Posts: 55
Reputation:
Solved Threads: 1
First: import java.io.*;
Then you need to read from the console:
try
{
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in.readLine();
//create an print writer for writing to a file
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//output to the file a line
out.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out.close();
}
catch(IOException e)
{
System.out.println("Error during reading/writing");
}
If you need more programming help visit www.NeedProgrammingHelp.com or email me at NeedProgrammingHelp@hotmail.com
Then you need to read from the console:
try
{
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in.readLine();
//create an print writer for writing to a file
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//output to the file a line
out.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out.close();
}
catch(IOException e)
{
System.out.println("Error during reading/writing");
}
If you need more programming help visit www.NeedProgrammingHelp.com or email me at NeedProgrammingHelp@hotmail.com
>is it possible to type in a class as a .txt file and then print out:
You better explain what you mean by this. To help you here is few examples
PS: You should post your code so we can see what you actualy trying to do. Please use code tags to post any code.
You better explain what you mean by this. To help you here is few examples
- get user input from console and then store it in file
- get user input through some Swing components and store it in the file
- read another file and either store it all or just part of it in new file
PS: You should post your code so we can see what you actualy trying to do. Please use code tags to post any code.
Last edited by peter_budo; Jun 17th, 2008 at 4:15 am. Reason: typo
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Hi,,
what i meant was... I have written out a class in a .txt file...
I got a portion that said:
{
System.out.println"this is true";
}
Now i compiled my program on my cmd, and get the .class file in my folder in C drive.
I want to see the out put of my program on my cmd screen.... I want to see
"this is true"
written out theere...
and i don' tknow how.
I write :
javac DooBee.java ->it gets compiled and i get my DooBee.class file.
but now how do i get to see my sentence written out where i wanted it to print?
I want to see the output of the class i wrote to see if my program was not only compiled right but can make some sense to mee too!
this is my program.:
class DooBee
{
public static void main (String args[] )
{
int x=1;
while (x<3)
{
System.out.print("Doo");
System.out.print("Bee");
x=x+1;
}
if(x==2)
System.out.print("Do");
}
}
I hope this is clearer.
Thanks
what i meant was... I have written out a class in a .txt file...
I got a portion that said:
{
System.out.println"this is true";
}
Now i compiled my program on my cmd, and get the .class file in my folder in C drive.
I want to see the out put of my program on my cmd screen.... I want to see
"this is true"
written out theere...
and i don' tknow how.
I write :
javac DooBee.java ->it gets compiled and i get my DooBee.class file.
but now how do i get to see my sentence written out where i wanted it to print?
I want to see the output of the class i wrote to see if my program was not only compiled right but can make some sense to mee too!
this is my program.:
class DooBee
{
public static void main (String args[] )
{
int x=1;
while (x<3)
{
System.out.print("Doo");
System.out.print("Bee");
x=x+1;
}
if(x==2)
System.out.print("Do");
}
}
I hope this is clearer.
Thanks
Last edited by CAESARIO; Jun 17th, 2008 at 9:31 am. Reason: incomplete message.
java DooBee
at the cmd to "run" the file .class
at the cmd to "run" the file .class
Check out my New Bike at my Public Profile at the "About Me" tab
You need to start with the most basic "getting started" tutorial: http://java.sun.com/docs/books/tutor...ava/win32.html
And do not post new questions to the end of other people's old threads. Post a new thread of your own if you have a question.
And do not post new questions to the end of other people's old threads. Post a new thread of your own if you have a question.
![]() |
Similar Threads
- Capturing Console Output (C)
- System function, output to file (C++)
- Getting all data from an input and output file (C++)
- Please I need help formatting an output file and sorting the names! (C++)
Other Threads in the Java Forum
- Previous Thread: classpath wrong, how to set it?
- Next Thread: method is calling itself???
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






