writing to an output file

Hi there,Is it possible to write the output of my program to a text file or something like that?jjmclell
[125 byte] By [jjmclella] at [2007-10-2 23:35:17]
# 1
Please don't cross post. http://forum.java.sun.com/thread.jspa?threadID=748904&messageID=4283274#4283274
sabre150a at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 2
Indeed yes,1 ) Put your program's output into a varibale2 ) Write this variable's value to the output stream.Simple ehh...
samue-1a at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 3

> Indeed yes,

> 1 ) Put your program's output into a varibale

> 2 ) Write this variable's value to the output

> stream.

>

> Simple ehh...

To the OP.

Ignore everyting from samue-1 . He know very little and is wrong about 95% of the time.

sabre150a at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 4

> Indeed yes,

> 1 ) Put your program's output into a varibale

> 2 ) Write this variable's value to the output

> stream.

>

> Simple ehh...

Mert, you should stop answering questions here. Your answers are almost always wrong. You're not helping anybody. You're just a nuisance.

jverda at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 5

> > Indeed yes,

> > 1 ) Put your program's output into a varibale

> > 2 ) Write this variable's value to the output

> > stream.

> >

> > Simple ehh...

>

>

> Mert, you should stop answering questions here. Your

> answers are almost always wrong. You're not helping

> anybody. You're just a nuisance.

Heeeey, what is wrong in here ? I think no where .

samue-1a at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 6

> > > Indeed yes,

> > > 1 ) Put your program's output into a varibale

> > > 2 ) Write this variable's value to the output

> > > stream.

> > >

> > > Simple ehh...

> >

> >

> > Mert, you should stop answering questions here.

> Your

> > answers are almost always wrong. You're not

> helping

> > anybody. You're just a nuisance.

>

> Heeeey, what is wrong in here ? I think no where .

Your answer was completely useless.

jverda at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 7
If what you mean by writing the output to file is writing the things that you write to console to a file then just redirect the System.out to a PrintStream which is created from a FileOutputStream
LRMKa at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 8

> > > > Indeed yes,

> > > > 1 ) Put your program's output into a varibale

> > > > 2 ) Write this variable's value to the output

> > > > stream.

> > > >

> > > > Simple ehh...

> > >

> > >

> > > Mert, you should stop answering questions here.

> > Your

> > > answers are almost always wrong. You're not

> > helping

> > > anybody. You're just a nuisance.

> >

> > Heeeey, what is wrong in here ? I think no where

> .

>

>

> Your answer was completely useless.

demonstrate it .

samue-1a at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 9
> > > > Your answer was completely useless.> > demonstrate it .No. I have no reason to do so. Everybody who matters knows that it was useless.
jverda at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 10

> If what you mean by writing the output to file is

> writing the things that you write to console to a

> file then just redirect the System.out to a

> PrintStream which is created from a FileOutputStream

This is exactly what I need to do, I just don't understand the coding. The tutorial is pretty vague. Could anyone do a simple example?

jjmclell

jjmclella at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 11

Example:

import java.io.*;

public class Foo {

public static void main(String[] args) throws Exception {

String path = System.getProperty("java.io.tmpdir");

String filename = path + "helloWorld.txt";

System.out.println("Using file: " + filename);

File file = new File(filename);

FileOutputStream fos = new FileOutputStream(file);

PrintStream ps = new PrintStream(fos);

System.setOut(ps);

System.out.println("Hello, World!");

}

}

Another option (depending on your OS)...

java -cp . YourClass > somefile.txt

~

yawmarka at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 12

> > If what you mean by writing the output to file is

> > writing the things that you write to console to a

> > file then just redirect the System.out to a

> > PrintStream which is created from a

> FileOutputStream

>

> This is exactly what I need to do, I just don't

> understand the coding. The tutorial is pretty vague.

> Could anyone do a simple example?

>

> jjmclell

please get the following book:

Core Java Fundamentals-1

and read starting from page 650. there are examples.

geomana at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 13
> Heeeey, what is wrong in here ? I think no where .Mert, i did not understand your input either.i am sure you are willing to help though.thanks
geomana at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 14

> > Heeeey, what is wrong in here ? I think no where

> .

>

> Mert, i did not understand your input either.

> i am sure you are willing to help though.

> thanks

He may be willing, but he's unable.

His Java knowledge is abysmall, as are his communication skills. He adds a net negative value to these forums.

jverda at 2007-7-14 16:17:23 > top of Java-index,Java Essentials,Java Programming...
# 15

> Example:

> import java.io.*;

>

> public class Foo {

> public static void main(String[] args) throws

> Exception {

> String path =

> System.getProperty("java.io.tmpdir");

> String filename = path + "helloWorld.txt";

> System.out.println("Using file: " + filename);

> File file = new File(filename);

> FileOutputStream fos = new FileOutputStream(file);

> PrintStream ps = new PrintStream(fos);

> System.setOut(ps);

> System.out.println("Hello, World!");

>

>

> Another option (depending on your OS)...

> java -cp . YourClass > somefile.txt

>

> ~

Thanks a lot yawmark, that was very helpful. One question though. Is there anyway to redirect the path that java writes out to? i.e. other than the temporary files in docs and settings?

Also, thanks geoman. I need all the solid reference books I can get my hands on.

jjmclell

jjmclella at 2007-7-21 8:49:12 > top of Java-index,Java Essentials,Java Programming...
# 16
>> Also, thanks geoman. I need all the solid reference> books I can get my hands on. > > jjmclellyou are most welcome, jjmclell.
geomana at 2007-7-21 8:49:12 > top of Java-index,Java Essentials,Java Programming...
# 17

> Thanks a lot yawmark, that was very helpful. One

> question though. Is there anyway to redirect the

> path that java writes out to? i.e. other than the

> temporary files in docs and settings?

Certainly. You can specify any path you want.

[url=http://java.sun.com/docs/books/tutorial/essential/io/]The Java Tutorial - I/O: Reading and Writing (but no 'rithmetic)[/url]

Best of luck!

~

yawmarka at 2007-7-21 8:49:12 > top of Java-index,Java Essentials,Java Programming...