Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Java结果导出到文本文件_Java_Export To Text - Fatal编程技术网

将Java结果导出到文本文件

将Java结果导出到文本文件,java,export-to-text,Java,Export To Text,您好,我所在的班级要求将我计算联邦和州税收的代码结果导出到文本文件中。我试着四处寻找帮助,但似乎就是找不到。请帮忙 public class FedStateTax { public static void main(String[] args) { String tn = javax.swing.JOptionPane.showInputDialog("Please enter your name."); String fn = javax.swing.JOptio

您好,我所在的班级要求将我计算联邦和州税收的代码结果导出到文本文件中。我试着四处寻找帮助,但似乎就是找不到。请帮忙

public class FedStateTax {    

public static void main(String[] args) {

    String tn = javax.swing.JOptionPane.showInputDialog("Please enter your name.");
    String fn = javax.swing.JOptionPane.showInputDialog("Enter income value");


        int income = Integer.parseInt(fn);
        double sax = 0;
        double tax = 0;

        if (income <= 50000)
            tax = income * 0.10;
        else if (income <= 100000)
            tax = income * 0.15;
        else if (income <= 150000)
            tax = income * 0.20;

        if (income <= 50000)
            sax = income * 0.05;
        else if (income <= 100000)
            sax = income * 0.10;
        else if (income <= 150000)
            sax = income * 0.15;


        if (income <=  50000)
            System.out.println("Federal tax is: $" + tax);
        else if (income <= 100000)
            System.out.println("Federal tax is: $"+ tax);
        else if (income <= 150000)
            System.out.println("Federal tax is: $"+ tax);

        if (income <=  50000)
            System.out.println("State tax is: $" + sax);
        else if (income <= 100000)
            System.out.println("State tax is: $"+ tax);
        else if (income <= 150000)
            System.out.println("State tax is: $"+ tax);


}
}

据我所知,您希望将税务数据写入文件。那你为什么不直接使用:

try {
    Files.write(Paths.get("output.txt"), Arrays.asList("Tas is : " + tax));
} catch (IOException e) {
    e.printStackTrace();
}
write方法属于java.nio包,它接受Pathas作为第一个参数和enumerable string作为第二个参数,并将它们逐行写入文件

您不应该记住,它会覆盖现有文件。如果只想追加,则应添加第三个参数StandartOpenOption,如下所示:

    Files.write(Paths.get("output.txt"), Arrays.asList("Tas is : " + "5", "2"), StandardOpenOption.APPEND);

看看FileWriter类。我刚刚试过,它成功了!谢谢你的帮助,我很高兴能帮上忙。