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

您只是将标准输出重定向到文件而不是控制台。据我所知,没有办法自动将输出克隆到两个流上,但手动操作很容易:

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 回答