I need to write the same data to stdout and to a save file. In C I'd do something like:
Google shows me how to use file I/O, and how to use stdio, but not how to combine the two easily.
With my *nix command line I could "program file | tee savefile", but savefile holds the result of prior runs, gets read before I parse 'file', and this command line zero's out savefile before the program can read it.
The data is standard ASCII, I doubt I'll ever have 20k of data.
Post by:Joanne Neal
To do any form of output you need an output stream. See the Java i/o tutorial for details of the different sorts and how to work with them.
The JVM automatically supplies an output stream connected to standard output - you can access it using the System.out static variable.
Post by:Winston Gutkowski
Jim Venolia wrote:I need to write the same data to stdout and to a save file. In C I'd do something like:
Is this a Java question? Because you seem to be listing all the ways you'd do this other than Java.
Joanne's right. If you want to use Java, you need to read the tutorials. We could just give you the answer, but I doubt that you'd be any better off.
And rest assured: if you can do it in C, you can almost certainly do it in Java. Indeed, if a cat/tee-type program is all you want, there are many ways to do it.
However, since you already havetee, why would you want to rewrite it in Java? One possible reason is that you would then have a utility you could run on pretty much any platform, but it's worth thinking about these things before you start simply writing Java.
Tip: Java characters are NOT the same thing as bytes, so you really do need to read the link that Joanne gave you. For starters, it means that, depending on your exact requirement, stdout - which in Java is a PrintStream (←click) - may not be what you want (although it will probably do just fine).
Post by:Jim Venolia
, Ranch Hand
Yeah, it's a Java question. I showed the C example because I've done this sort of things several times in C. I showed the tee example to so you knew that I knew there were other ways to solve the problem.
The code currently has a "showData(data)" that parses the data and uses multiple System.out.printf's for each line. I want to make minimal changes to the working code to wedge this new feature in.
I'll try to get back to it tomorrow.
Post by:Winston Gutkowski
Jim Venolia wrote:The code currently has a "showData(data)" that parses the data and uses multiple System.out.printf's for each line. I want to make minimal changes to the working code to wedge this new feature in.
Well, System.out is Java's equivalent of stdout. However, looking again at your OP, it would seem that you're more interested in a "connector" to transfer between streams, as with the Unix pipe ('|') symbol.
Java does have a Pipe class (java.nio.channels.Pipe), and also various "Piped" ones; but you may then end up with a Java program that simply looks like a Unix pipeline, when in fact combining tasks on a single stream might be a better way to do it ... Or maybe not ... But make sure that you're writing a Java program, not a Java translation of a Unix script.
This thread has been viewed 535 times.
All times above are in ranch (not your local) time.
The current ranch time is Oct 17, 2018 12:10:13.