简体   繁体   English

PrintWriter将文本添加到文件中

[英]PrintWriter add text to file

In my online computer science class I have to write a program to determine the surface gravity on each planet in the solar system. 在我的在线计算机科学课上,我必须编写一个程序来确定太阳系中每个行星的表面引力。 I have gotten almost every aspect of it to work save one. 除了一个,我几乎已经完成了它的每个方面。 I need to write the surface gravity to a file using a separate method. 我需要使用单独的方法将表面重力写入文件。 This is my current method: 这是我目前的方法:

public static void writeResultsToFile(double g) throws IOException{

    PrintWriter outFile = new PrintWriter(new File("planetaryData.txt"));

    outFile.printf("%.2f%n", g);
    outFile.close();
}

My problem is that when I write it to a file it will overwrite the previous value. 我的问题是,当我将它写入文件时,它将覆盖以前的值。 How do I get it include all of the values. 我如何获得它包括所有值。 Here is the entirety of my code if that helps: 如果有帮助,这是我的全部代码:

import java.util.Scanner;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;


public class gravityV1 {

    /**
     * @param args
     */

    public static double calculateSurfaceGravity(double d, double M){
        double G = 6.67 * Math.pow(10, -11);
        double r = d;
        double g;

        g = G * M/Math.pow(r/2*1000, 2);

        return g;
    }

    public static void printResultsToScreen(String planetName, double diameterKm, double massKg, double g){

        System.out.printf("%-15s%-17.0f%-17.2e%.2f%n", planetName, diameterKm, massKg, g);

    }

    public static void writeResultsToFile(double g) throws IOException{

        PrintWriter outFile = new PrintWriter(new File("planetaryData.txt"));

        outFile.printf("%.2f%n", g);
        outFile.close();
    }

    public static void main(String[] args) throws IOException {
        // Variables
        String [] planetName = new String[8];
        planetName[0] = "Mercury";
        planetName[1] = "Venus  ";
        planetName[2] = "Earth  ";
        planetName[3] = "Mars   ";
        planetName[4] = "Jupiter";
        planetName[5] = "Saturn ";
        planetName[6] = "Uranus ";
        planetName[7] = "Neptune";
        double [] diameterKm = new double[8];
        diameterKm[0] = 4880;
        diameterKm[1] = 12103.6;
        diameterKm[2] = 12756;
        diameterKm[3] = 6794;
        diameterKm[4] = 142984;
        diameterKm[5] = 120536;
        diameterKm[6] = 51118;
        diameterKm[7] = 49532;
        double [] massKg = new double[8];
        massKg[0] = 3.30 * Math.pow(10, 23);
        massKg[1] = 4.869 * Math.pow(10, 24);
        massKg[2] = 5.97 * Math.pow(10, 24);
        massKg[3] = 6.4219 * Math.pow(10, 23);
        massKg[4] = 1.900 * Math.pow(10, 27);
        massKg[5] = 5.68 * Math.pow(10, 26);
        massKg[6] = 8.683 * Math.pow(10, 25);
        massKg[7] = 1.0247 * Math.pow(10, 26);
        double [] g = new double[8];
        int array = 0;

        //code

        System.out.printf("%s%20s%15s%15s%n", "Planet", "Diameter (km)", "Mass (kg)", "g (m/s^2)");

        for(double d : diameterKm){
            g[array] = calculateSurfaceGravity(d, massKg[array]);
            printResultsToScreen(planetName[array], d, massKg[array], g[array]);
            writeResultsToFile(g[array]);
            array++;
        }
    }

}

这样做是为了创建一个在附加模式下使用FileWriterPrinterWriter

PrintWriter outFile = new PrintWriter(new FileWriter("planetaryData.txt", true)); 

From Mkyong's tutorials : 来自Mkyong的教程

FileWriter, a character stream to write characters to file. FileWriter,一个将字符写入文件的字符流。 By default, it will replace all the existing content with new content, however, when you specified a true (boolean) value as the second argument in FileWriter constructor, it will keep the existing content and append the new content in the end of the file. 默认情况下,它会用新内容替换所有现有内容,但是,当您在FileWriter构造函数中指定true(boolean)值作为第二个参数时,它将保留现有内容并将新内容附加到文件末尾。

You can use something like - 你可以使用像 -

    PrintWriter outputFile = new PrintWriter(new FileWriter(file, true));

Two problems: 两个问题:

1) You are overwriting the file each time. 1)您每次都要覆盖文件。 printResultsToFile should take the whole array, not just one datum. printResultsToFile应该采用整个数组,而不仅仅是一个数据。 Call it from outside your loop. 从循环外部调用它。

2) Consider entering your floating point numbers as 2.08e24 for brevity. 2)为简洁起见,请考虑输入浮点数为2.08e24。

private void printResultsToFile(double result[]) {
  PrintWriter pw = new PrintWriter(new FileWriter("planetaryData.txt"));
  for (int i=0; i< result.length; i++) {
    pw.printf("%.2f%n", result[i]);
  }
  pw.close();
}

the following code also works to open file name planetaryData.txt. 以下代码也适用于打开文件名planetData.txt。

PrintWriter outFile = new PrintWriter("planetaryData.txt"); PrintWriter outFile = new PrintWriter(“planetaryData.txt”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM