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

Recommended Answers

All 19 Replies

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

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

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!!!

>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

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

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

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

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


this is what i got.

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

regards

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?

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

commented: homework kiddo, thread hijacker, zombie master, demanding personal service -2

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?

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

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);
}
}
}

Hmm... The first line of code was what I wanted but how would you record all of the input lines?

did you notice those posts are years old? there 's no reason te revive dead threads. it's better to create your own.

Well it was already here. :(
My apologies.
But if there is anyone who would answer please do. :)

seriously, don't revive ancient threads. a lot of the better developers here will ignore the thread once they see it's eight years old. you'll miss out on a lot of usefull help.

Hi Kenton. Welcome to Daniweb.
Start a new thread for your question and someone will help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.