如何将数组内容写入输出文件?

时间:2017-04-16 19:26:53

标签: java netbeans

我有一个程序从输入文件中读取,该文件有两个字段,一周中的某一天(1-7)和当天的温度。然后它将它们输入一个数组(highArray,lowArray)并确定每天的高,低,总和平均温度并将它们写入输出文件。我遇到的问题是如何将我的数组的内容写入我的输出文件。我可以用以下行编写一周的日期:myOutput.writeInt(dow);我意识到输出类INT不适用于数组,所以如何写入文件中的数据? (highArray,lowArray)以下是我的代码

package dow;

import java.util.Arrays;

public class DOW
{  
    public static void main(String[] args)
    {
        //  INITIALIZATION
        InputFile myInput = new InputFile("in.txt");
        OutputFile myOutput = new OutputFile("out.txt");

        int dow=0;
        int temperature = 0;
        int[] highArray = new int [8];
        int[] lowArray = new int [8];
        int[] countArray = new int [8];
        int[] totalArray = new int [8];

        // initialize array
        for (dow = 0; dow <8; dow++)
        {
            totalArray [dow]= 0;
            countArray [dow]= 0;
            highArray [dow]= -999;
            lowArray [dow]= 999;            
        }

        while (!myInput.eof())
        {
            dow = myInput.readInt();
            temperature = myInput.readInt();
            if (temperature > highArray[dow]) // High Per Day
            {
                highArray [dow] = temperature;
            }
            if (temperature < lowArray [dow]) // Low Per DAy
            {
                lowArray[dow] = temperature;
            }

            countArray [dow] = countArray[dow] +1;
            totalArray[dow] = totalArray [dow] + temperature;

            System.out.println(dow);
        }
        System.out.println(Arrays.toString(lowArray));
        System.out.println(Arrays.toString(highArray));
        System.out.println(Arrays.toString(totalArray));
        System.out.println(Arrays.toString(countArray));                

        //OUTPUT LOOP             

        for(dow = 1; dow < 8; dow++)
        {
            outputFile.println(highArray[dow]);
        }  

        myOutput.writeInt(dow);

        // myOutput.write(highArray);           
        // myOutput.writeInt(totalArray);
        // myOutput.writeEOL(countArray); 
        // myOutput.writeInt (temperature); 

        myOutput.close();
    }    
}
//END OF MAIN   

1 个答案:

答案 0 :(得分:0)

  • 我强烈建议您使用tf.app.flags.DEFINE_string('train_directory', '/tmp/', 'C:/Users/Lenovo/dir2/tensorflow_code/cell_tf/cell_images/main_cell_images/TRAIN_DIR') tf.app.flags.DEFINE_string('validation_directory', '/tmp/', 'C:/Users/Lenovo/dir2/tensorflow_code/cell_tf/cell_images/main_cell_images/VALIDATION_DIR') tf.app.flags.DEFINE_string('output_directory', '/tmp/', 'C:/Users/Lenovo/dir2/tensorflow_code/cell_tf/cell_images/main_cell_images/OUTPUT_DIR') tf.app.flags.DEFINE_integer('train_shards', 1, 'Number of shards in training TFRecord files.') tf.app.flags.DEFINE_integer('validation_shards', 1, 'Number of shards in validation TFRecord files.') tf.app.flags.DEFINE_integer('num_threads', 1, 'Number of threads to preprocess the images.') tf.app.flags.DEFINE_string('labels_file', '','C:/Users/Lenovo/dir2/tensorflow_code/cell_tf/cell_images/main_cell_images/MYLABELS.TXT') BufferedReader代替BufferedWriterInputFile

  • 作为旁注,将OutputFile数组中的所有值初始化为0是微不足道的,因为int将打印出所有0。 System.out.print(Arrays.toString(new int[10]));是Java中的基本类型,因此始终初始化为0且不能为int。这也适用于数组。

示例:

null