user1946095
user1946095

Reputation: 21

Display data on console and also save data to text file.

So here is my code, it seems to work, but it just prints out the info on file rather than doing both (displaying data on console and saving the information to a text file). Help appreciated.

   // imports
   import java.io.BufferedReader;
   import java.io.FileOutputStream;
   import java.io.FileReader;
   import java.io.IOException;
   import java.io.PrintStream;

   public class DTM {

       // The main method for our Digital Terrain Models
       /** @param args
        * @throws IOException
        */
       public static void main(String[] args) throws IOException {


           //Prints the console output on a text file (Output.txt)
           PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
           System.setOut(out);

           //Declare some variables
           int aRows = 401;
           int bCols = 401;
           String DMTfile = "sk28.asc";
           //Declare some tables
           double data[][] = new double[aRows][bCols];

           BufferedReader file = new BufferedReader(new FileReader(DMTfile));
           //Write data into array
           for (int i = 0; i < aRows; i++) {
               String rowArray[] = file.readLine().split(" ");
               for (int j = 0; j < bCols; j++) {
                   data[i][j] = Double.parseDouble(rowArray[j]);
               }
           }

           //Closing the file
           file.close();

           //print out the array
           for (int i = 0; i < aRows; i++) {
               for (int j = 0; j < bCols; j++) {
                   System.out.println(data[i][j]);
               }
           }

           // this hold's the smallest number
           double high = Double.MIN_VALUE;
           // this hold's the biggest number
           double low = Double.MAX_VALUE;

           //initiate a "For" loop to act as a counter through an array
           for (int i = 0; i < data.length; i++) {
               for (int j = 0; j < data[i].length; j++)

               //determine the highest value
               if (data[i][j] > high) {
                   high = data[i][j];
               }
               //determine the lowest value
               else if (data[i][j] < low) {
                   low = data[i][j];

               }
           }

           // Code here to find the highest number
           System.out.println("Peak in this area = " + high);
           // Code here to find the lowest number
           System.out.println("Dip in this area = " + low);

       }
   }

Upvotes: 2

Views: 2495

Answers (2)

Frank
Frank

Reputation: 15641

Try the Apache Commons TeeOutputStream.

Untested, but should do the tric:

outStream = System.out;   
// only the file output stream  
OutputStream os = new FileOutputStream("output.txt", true);   
// create a TeeOutputStream that duplicates data to outStream and os  
os = new TeeOutputStream(outStream, os); 
PrintStream printStream = new PrintStream(os);       
System.setOut(printStream);

Upvotes: 3

ailnlv
ailnlv

Reputation: 1849

You're merely redirecting standard output to a file instead of the console. As far as I know there is no way to automagically clone an output onto two streams, but it's pretty easy to do it by hand:

public static void multiPrint(String s, FileOutputStream out){
    System.out.print(s);
    out.write(s);
}

Whenever you want to print you just have to call this function:

FileOutputStream out=new FileOutputStream("out.txt");
multiPrint("hello world\n", out);

Upvotes: 1

Related Questions