954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Redirecting console output to file

Hi everyone,

I am having trouble with getting output from the console onto a file, for example a .TXT. I suspect that I have to make use of the System.out function, but I can't quite figure out how I might use it. Thanks for the help

ghfeyn
Newbie Poster
1 post since May 2005
Reputation Points: 10
Solved Threads: 0
 

You can use " java.io" package classes to do that.

yassar
Light Poster
35 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

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 or email me at

NPH
Junior Poster in Training
55 posts since May 2005
Reputation Points: 10
Solved Threads: 1
 

is it possible to type in a class as a .txt file and then
print out:


system.out.println "this is true"

as This is true on the cmd???

tell me how please..

my books have given progs to write nad see output and i can't see output althougt all progs are running and compling...

thanks in advance!!!

CAESARIO
Newbie Poster
6 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

>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 examplesget 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.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

CAESARIO
Newbie Poster
6 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

java DooBee
at the cmd to "run" the file .class

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

Exception in thread "main" java.lang.NoClassDefFoundError: DooBee


this is what i got.

CAESARIO
Newbie Poster
6 posts since Jun 2008
Reputation Points: 10
Solved Threads: 0
 

You need to start with the most basic "getting started" tutorial: http://java.sun.com/docs/books/tutorial/getStarted/cupojava/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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

hi,
i was trying to contact to at needprogramminghelp but the mail delivery failed.
can i have ur ID to contact you?

regards

sana.malik
Newbie Poster
5 posts since Mar 2010
Reputation Points: 8
Solved Threads: 0
 

hi, i was trying to contact to at needprogramminghelp but the mail delivery failed. can i have ur ID to contact you?

regards


That was all junk that slipped by attention of moderators in the pass. Why did you wanted to contact somebody who obviously deleted that account and website is dead as some marketing company has some rubbish on it?

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

i wanted to discuss some problems that i am facing in java thats why, can u suggest me any person from java community?

sana.malik
Newbie Poster
5 posts since Mar 2010
Reputation Points: 8
Solved Threads: 0
 
i wanted to discuss some problems that i am facing in java thats why, can u suggest me any person from java community?


You are already in Java community! So why don't you create new thread and post your question?

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

where to put the code in the third reply by NPH. I have created a java test class.

ErnestGma
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 
import java.io.*;
public class cmdread
{
public static void main(String ar[])
{
try{
InputStreamReader is=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(is);
String s=br.readLine();
File f1=new File("aaryan.txt");   //creating a new file in which u want to write contents of console 
f1.createNewFile();
FileWriter fw=new FileWriter(f1);
fw.write(s);
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
deepaktanwar22
Newbie Poster
1 post since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: