简体   繁体   English

java,将计算值打印到文件时不是数组?

[英]java, Printing calculated values to a File when not an array?

This program calculates the surface gravities of 9 planets, prints the data to the screen in a formatted table, and is supposed to print only the surfaceGravity values to a file. 该程序计算9个行星的表面重力,将数据以格式化表的形式打印到屏幕上,并假定仅将surfaceGravity值打印到文件中。 I'm not sure why my code won't print the calculated surfaceGravity value to the file called gravities2.txt. 我不确定为什么我的代码不会将计算出的surfaceGravity值打印到名为gravities2.txt的文件中。 Can someone please provide some enlightenment? 有人可以提供一些启示吗? Is it maybe because the gravity data is not put into an array- it's just values that get looped through? 可能是因为重力数据未放入数组中-只是值被循环通过了吗?

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

public class GravityV1
{   
   //print the data to screen
   public static void printResults(String[] names, double[] diameter, double[] mass, int x)
   {
     System.out.printf("%-10s%21.2f%20.4e",names[x],diameter[x],mass[x]);   
   }

   //calculate the surface gravity
   public static double calcGravity(double m, double d)throws IOException
   { 
      double gravity = 0.0;    

      gravity = (((6.67E-11) * (m)) / (Math.pow((d) / 2, 2)));

      printToFile(gravity);     

      return gravity;        
   }

   //writes gravity data to a text file
   public static void printToFile(double gravity)throws IOException
   { 
       File gravities = new File("gravity2.txt"); 

       PrintWriter outFile = new PrintWriter(gravities);

       outFile.printf("%2.2f\n", gravity);
   }

   //main method
   public static void main (String [ ] args) throws IOException
   {        
      double[] mass = { 3.30E23, 4.869E24, 5.972E24, 6.4219E23, 1.900E27, 5.68E26, 8.683E25, 1.0247E26, 1.27E22 };

      double[] diameter = { 4880000, 12103000.6, 12756000.3, 6794000, 142984000, 120536000, 51118000, 49532000, 2274000 };

      String[] names = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };

      System.out.printf("%-7s%20s%20s%20s\n","Planet Name","Diameter","Mass(kg)", "g (m/s^2)");    
      System.out.println("=============================================================================");

      for (int x = 0; x < 9; x++) {
         printResults(names, diameter, mass, x);

         double surfaceGravity = calcGravity(mass[x], diameter[x]);

         System.out.printf("%17.2f\n", surfaceGravity);         
      }  

   System.out.println();
  }  
}

The output to the screen is correct, but unfortunately the file contains nothing. 屏幕上的输出是正确的,但不幸的是,该文件不包含任何内容。 The file does get created, so I suppose that's one good thing... Any help is very greatly appreciated!! 该文件确实已创建,所以我想这是一件好事...非常感谢您的帮助!!

You need to close and flush the PrintWriter . 您需要关闭并冲洗PrintWriter This is because the PrintWriter "buffers" your output, or saves it, and only writes once there is a fair amount saved up (this helps your program go faster, because it causes fewer big writes rather than a lot of small ones). 这是因为PrintWriter“缓冲”或保存您的输出,并且仅在保存了相当数量的存储量后才写入(这有助于您的程序运行得更快,因为它会导致较少的大写次数而不是很多小写次数)。 Closing or flushing a file forces it to write. 关闭或刷新文件会强制其写入。

Adding outFile.close() at the end of the function will help, although you should try to refactor you code so that you don't have to constantly create new writers, and can simply close the one writer when you are done! 在函数末尾添加outFile.close()会有所帮助,尽管您应该尝试重构代码,从而不必不断创建新的编写器,并且只需在完成后关闭一个编写器即可!

Two issues: 两个问题:

1 - You're never closing the PrintWriter . 1-您永远不会关闭PrintWriter You need to invoke outFile.close() after you're done writing. 完成编写后,您需要调用outFile.close()

2 - Since you're calling the printToFile method from within the loop in your main method, and that method creates a new PrintWriter object each time it is called, what you will effectively end up with is a file containing only the LAST planet in the loop. 2-由于您是从main方法的循环内调用printToFile方法,并且每次调用该方法都会创建一个新的PrintWriter对象,因此最终将得到的文件实际上是仅包含LAST行星的文件环。

To solve the last problem, I would create a PrintWriter before the loop is created, and pass it in to the printToFile method each time you call it. 为了解决最后一个问题,我将在创建循环之前创建一个PrintWriter ,并在每次调用它时将其传递给printToFile方法。 Remember to close() it after the loop. 请记住在循环后将其close()

尝试在调用printf方法时添加冲洗。

outFile.flush();

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

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