簡體   English   中英

PrintWriter將文本添加到文件中

[英]PrintWriter add text to file

在我的在線計算機科學課上,我必須編寫一個程序來確定太陽系中每個行星的表面引力。 除了一個,我幾乎已經完成了它的每個方面。 我需要使用單獨的方法將表面重力寫入文件。 這是我目前的方法:

public static void writeResultsToFile(double g) throws IOException{

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

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

我的問題是,當我將它寫入文件時,它將覆蓋以前的值。 我如何獲得它包括所有值。 如果有幫助,這是我的全部代碼:

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

來自Mkyong的教程

FileWriter,一個將字符寫入文件的字符流。 默認情況下,它會用新內容替換所有現有內容,但是,當您在FileWriter構造函數中指定true(boolean)值作為第二個參數時,它將保留現有內容並將新內容附加到文件末尾。

你可以使用像 -

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

兩個問題:

1)您每次都要覆蓋文件。 printResultsToFile應該采用整個數組,而不僅僅是一個數據。 從循環外部調用它。

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();
}

以下代碼也適用於打開文件名planetData.txt。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM