You are not logged in.

#1 2009-02-26 04:54:38

estex198
Member
Registered: 2009-02-17
Posts: 39

redirecting java program output (including user input) to file

I'm taking an introductory course in Java. Since I'm running Arch as my full time OS, I thought I'd give it a shot as a programming environment (I've heard good things.) I'm using kate to write the code because it makes it easy to read and what not, and javac / java included with openjdk6 to compile / execute my programs in the console. Right now I'm coding a very small program that includes asking the user several times for input, performing some basic calculations, and then printing the results to the standard output. I've tried using pipes and output redirection operators, but I can only redirect the program output until the first user prompt, which then leaves me no choice other than killing the program. Any way around this? I should also mention this is not part of the hw assignment, but I'm trying to learn more about bash scripting as well. Thanks in advance!

- Rusty

Offline

#2 2009-02-26 06:57:06

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: redirecting java program output (including user input) to file

So you're taking in input from the user, right?  Store it in variables, then set up a BufferedWriter, and write out each variable to the file in turn.  That should do it for you.

Unless you mean by "standard output" the command line.  Then you'd have to still store it in variables and do your calculations, then return it with System.out.println(variable/variable2); or so.

Hope that helps,
Lswest

P.S. I realize now you're talking about redirecting it with the actual command line.  I don't see why you'd need to, since Java supplies packages to write to files.  I'm not sure how you could have it redirect all input and output to a file, sorry, misunderstood at first.

Last edited by lswest (2009-02-26 07:00:10)


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#3 2009-02-26 08:36:14

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: redirecting java program output (including user input) to file

I'm not really clear on what the problem here is, or what you're trying to do. It sounds like a mistake with your redirection in the shell though. Post the command-line invocation you're using?

Offline

#4 2009-02-26 19:54:30

estex198
Member
Registered: 2009-02-17
Posts: 39

Re: redirecting java program output (including user input) to file

command-line invocation? I know that java supplies libraries to read/write a file, but I'm interested in using bash scripting to append to the file. Say I don't have access rights to the source code. What if I'm a sys admin thats trying to log all user interaction with a proprietary program to a file? I've posted here my source code. I want to run the program from the command line along with extra shell commands to record all output, including user input to a file, just like its displayed in the shell. I've also included what I want the log file to look like after the user runs the program. Keep in mind I'm no professional. wink


// Here is my code for lab homework...

import java.util.Scanner;
import java.text.DecimalFormat;

public class lab3
{
    public static void main(String[] args)
    {
    DecimalFormat df = new DecimalFormat("$#,###.00");
    Scanner usrInput = new Scanner(System.in);
   
    int qtyAdults, qtyChildren, qtyNights;
    double dblBase, dblTotal, dblTaxes;
       

    final double ADULT = 25.0;
    final double CHILD = 8.0;
    final double TAX_RATE = 0.1;


    // Acquire input from user
    System.out.println("Welcome to FunStay Hotel! For how many adults will you need accomodations?");
        qtyAdults = usrInput.nextInt();
    System.out.println("How many children?");
    qtyChildren = usrInput.nextInt();
    System.out.println("How many nights will you be staying with us?");
    qtyNights = usrInput.nextInt();

    // Calculate charges
    dblBase = (qtyAdults * qtyNights * ADULT) + (qtyChildren * qtyNights * CHILD);
    dblTaxes = dblBase * TAX_RATE;
    dblTotal = dblBase + dblTaxes;

    // Display charges and confirm purchase
    System.out.format("You entered %d adults, and %d children. You plan to be staying with us for %d nights.%n", qtyAdults, qtyChildren, qtyNights);
    System.out.println("Base Charge: " + df.format(dblBase));
    System.out.println("Taxes: " + df.format(dblTaxes));
    System.out.println("Total: " + df.format(dblTotal));
       
    } // End main

} // end Lab_Three


What I want is to do something like this....

[estex@myhost java]$ java lab3 >> lab3.out

and have the lab3.out look just like this...


Welcome to FunStay Hotel! For how many adults will you need accomodations?
3
How many children?
2
How many nights will you be staying with us?
1
You entered 3 adults, and 2 children. You plan to be staying with us for 1 night(s).
Base Charge: $91.00
Taxes: $9.10
Total: $100.10


Of course it would be helpful to be able to append to the file, but I think once I get something to write the file it should be easy to figure a way to append. Hope this clarifies my question. TIA!

- Rusty

Offline

#5 2009-02-26 20:12:55

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: redirecting java program output (including user input) to file

Try this:

java myProgram | tee /path/to/file

It should pipe the output of your program both to the console and append it to the file using tee.

Last edited by lswest (2009-02-26 20:13:20)


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

#6 2009-02-27 06:25:21

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: redirecting java program output (including user input) to file

estex198 wrote:

command-line invocation?

...

What I want is to do something like this....

[estex@myhost java]$ java lab3 >> lab3.out

This is what I was asking about. Are you wanting it to get input from a file as well?

BTW, >> does append to the file. > is the one that replaces it. What you typed there should log all program output, but I'm not sure how you would log user input as well.

EDIT: Using logging in screen may be a way you can record a complete transcript.

Last edited by pauldonnelly (2009-02-27 06:27:54)

Offline

#7 2009-03-08 18:51:32

estex198
Member
Registered: 2009-02-17
Posts: 39

Re: redirecting java program output (including user input) to file

thanks lswest. Tee did the trick!

- Rusty

Offline

#8 2009-03-09 06:57:01

lswest
Member
From: Munich, Germany
Registered: 2008-06-14
Posts: 456
Website

Re: redirecting java program output (including user input) to file

estex198 wrote:

thanks lswest. Tee did the trick!

- Rusty

No problem at all smile  Glad I could help


Lswest <- the first letter of my username is a lowercase "L".
"...the Linux philosophy is "laugh in the face of danger". Oops. Wrong one. "Do it yourself". That's it." - Linus Torvalds

Offline

Board footer

Powered by FluxBB