2

これが私のコードです。うまくいくようですが、両方を実行するのではなく、ファイルに情報を出力するだけです(コンソールにデータを表示し、情報をテキストファイルに保存します)。助けていただければ幸いです。

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

       }
   }
4

2 に答える 2

3

Apache Commons TeeOutputStreamを試してください。

テストされていませんが、トリックを実行する必要があります:

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);
于 2013-01-03T19:58:44.437 に答える
1

コンソールではなく、標準出力をファイルにリダイレクトしているだけです。私の知る限り、出力を 2 つのストリームに自動的に複製する方法はありませんが、手動で行うのは非常に簡単です。

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

印刷したいときはいつでも、この関数を呼び出すだけです:

FileOutputStream out=new FileOutputStream("out.txt");
multiPrint("hello world\n", out);
于 2013-01-03T19:59:29.813 に答える