gpt4 book ai didi

java - 如何写入输出文件?

转载 作者:太空宇宙 更新时间:2023-11-04 10:11:09 24 4
gpt4 key购买 nike

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

我正确导入了所有内容。但我的输出文件不会打印到 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);
}
}

我的代码唯一不做的就是写入单独的文本文件。 (是的,我还有更多需要写入文本文件,我只是给出了我需要的一些东西的示例。)

最佳答案

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

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

不要忘记\ 转义为 \\。现在您可以在绝对路径中看到TEACHERNAME.out

关于java - 如何写入输出文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52304234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com