在控制台上显示数据,并将数据保存到文本文件。

时间:2013-01-03 19:54:38

标签: java io

所以这是我的代码,它似乎工作,但它只打印出文件中的信息而不是两者兼而有之(在控制台上显示数据并将信息保存到文本文件)。帮助赞赏。

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

       }
   }

2 个答案:

答案 0 :(得分:3)

尝试Apache Commons TeeOutputStream

未经测试,但应该执行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);

答案 1 :(得分: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);