PrintWriter将文本添加到文件 [英] PrintWriter add text to file

查看:111
本文介绍了PrintWriter将文本添加到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的在线计算机科学课上,我必须编写一个程序来确定太阳系中每个星球上的表面引力。我已经几乎得到了它的每一个方面的工作,保存一个。我需要使用单独的方法将表面引力写入文件。这是我现在的方法:

pre $ public $ {

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

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





$ b我的问题是,当我把它写入一个文件时,它会覆盖以前的值。我如何得到它包括所有的值。这是我的代码的全部,如果有帮助:

  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,行星名,直径Km,质量Kg,g);


$ b public static void writeResultsToFile(double g)throws IOException {

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

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

$ b $ public static void main(String [] args)throws IOException {
//变量
String [] planetName = new String [8];
planetName [0] =水星;
planetName [1] =Venus;
planetName [2] =地球;
planetName [3] =火星;
planetName [4] =木星;
planetName [5] =土星;
planetName [6] =天王星;
planetName [7] =海王星;
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数组= 0;
$ b $ // code

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

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




$ b $ div class =h2_lin>解决方法

这样做是为了在追加模式下创建 PrinterWriter 使用 FileWriter

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


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++;
    }
}

}

解决方案

Do this in order to create a PrinterWriter working with a FileWriter in append mode:

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

这篇关于PrintWriter将文本添加到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆