首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何写入输出文件?

如何写入输出文件?
EN

Stack Overflow用户
提问于 2018-09-13 06:18:18
回答 1查看 55关注 0票数 1

如何将底部写入输出文件?它编译并运行,但会关闭,而不是将其写入文件。

我正确地导入了所有内容。但是我的输出文件不能打印到TEACHERNAME.out?

import javax.swing.JOptionPane;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.Date;
public class Corpus03
{
  public static void main(String arg[])
   throws FileNotFoundException
   {

   Scanner inputFile = new Scanner(new FileReader("input.txt"));
   PrintWriter outFile = new PrintWriter("TEACHERNAME.out"); 

       //INPUT
   name = JOptionPane.showInputDialog("Enter your name: ");
   ID = JOptionPane.showInputDialog("Enter your ID: ");
   str = JOptionPane.showInputDialog("Enter your Annual Salary");
   annualSalary = Double.parseDouble(str);
   //CALCULATIONS
   grossPayPerCheck = annualSalary / 24;
   actualDayWorked = annualSalary / 174;
   perWeek = (annualSalary / 50);
   perHour = perWeek / 40; 
   ssTax = grossPayPerCheck * SS_TAX;
   medTax = grossPayPerCheck * MED_TAX;
   fedTax = grossPayPerCheck * FED_TAX;
   stateTax = grossPayPerCheck * STATE_TAX;
   unitedWay = 20;
   healthIns = 125;
   totalDeductions = ssTax + medTax + fedTax + stateTax + healthIns +    unitedWay;
   netPay = (grossPayPerCheck - totalDeductions);
   Date now = new Date();
   //OUTPUT
   outputStr = "Name: " + name
      + "\r\n" + "Employee ID: " + ID
      + "\r\n" + "Annual Salary: " + String.format("%.2f", annualSalary)
      + "\r\n" + "Gross Pay per Check: $"
      + String.format("%.2f", grossPayPerCheck)
      + "\r\n" + "$" + String.format("%.2f", actualDayWorked) + " per actual day worked"
      + "\r\n" + "$" + String.format("%.2f", perHour) + " per hour"
      + "\r\n\r\n" + "===================="
      + "\r\n" + "DEDUCTIONS PER CHECK"
      + "\r\n" + "===================="
      + "\r\n" + "Social Security Tax: $" 
      + String.format("%.2f", ssTax)
      + "\r\n" + "Medicare Tax: $"
      + String.format("%.2f", medTax)
      + "\r\n" + "Federal Income: $"
      + String.format("%.2f", fedTax)
      + "\r\n" + "State Income Tax: $"
      + String.format("%.2f", stateTax)
      + "\r\n" + "Health Insurance: $125.00"
      + "\r\n" + "United Way Contribution: $20.00"
      + "\r\n\r\n" + "Total Deductions: $" 
      + String.format("%.2f", totalDeductions)
      + "\r\n\r\n" + "Net Pay per check: $" 
      + String.format("%.2f", netPay);
   JOptionPane.showMessageDialog(null, outputStr, 
                     "Teacher Pay Information", 
                   JOptionPane.INFORMATION_MESSAGE);
   outFile.printf("City School Dstric, 111 First Stree, Anytown, OK 12345");
   outFile.println();
   outFile.println();
   outFile.printf("%-40s%-10s\r\n\n", name, ID);
   outFile.close();  
   inputFile.close();
   System.exit(0);
   }
}

我的代码唯一不做的事情就是写入一个单独的文本文件。(是的,我有更多需要写入文本文件的内容,我只是举例说明了一些我需要的东西。)

EN

回答 1

Stack Overflow用户

发布于 2018-09-13 06:55:47

您可以将PrintWriter的相对路径指定为

PrintWriter outFile = 
            new PrintWriter("C:\\Users\\example\\IdeaProjects\\untitled\\news\\TEACHERNAME.out");

别忘了用\换成\\。现在您可以在绝对路径中看到TEACHERNAME.out

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52304234

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档