Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.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 BufferedWriter未写入所有信息_Java_Printwriter_Bufferedwriter_Writer - Fatal编程技术网

Java BufferedWriter未写入所有信息

Java BufferedWriter未写入所有信息,java,printwriter,bufferedwriter,writer,Java,Printwriter,Bufferedwriter,Writer,我正在制作一个简单的教程程序,用户在其中输入一些数据,然后将数据写入文本文件,然后读取文本文件并显示数据。但是,并非所有数据都写入文本文件。请查看以下内容: 这是我的代码: package chasi.fecalMatter.ExcrementalProgram; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import j

我正在制作一个简单的教程程序,用户在其中输入一些数据,然后将数据写入文本文件,然后读取文本文件并显示数据。但是,并非所有数据都写入文本文件。请查看以下内容:

这是我的代码:

package chasi.fecalMatter.ExcrementalProgram;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;

public class RedundantExcrement {

    static String inputName = JOptionPane.showInputDialog("Please input your FULL name:");
    static String inputUsername = JOptionPane.showInputDialog("Please input your desired Username:");
    static String inputJobTitle = JOptionPane.showInputDialog("Please input your Job title:");
    static String inputSalary = JOptionPane.showInputDialog("Please input your monthly salary:");

    static int convertedSalary = 0; /*Convert salary from String to int*/
    static int benefitDeduction = 0;

    static String empFileName = inputUsername+"INFO";
    static BufferedWriter bwriter = null;

    public static void main (String[] args) throws IOException {
        //Catch NumberFormatException.
        try {
            Integer.parseInt(inputSalary);
        } catch (NumberFormatException nfEx) {
            JOptionPane.showMessageDialog(null, "Please ensure that " +
                    "your salary is entered in NUMERIC form.", "ERROR!", 2);
            return;
        }

        //Specify instructions as regard salary -to- Benefit deduction ratio
        if (convertedSalary >= 3500) {
            benefitDeduction = 300;
        } else {
            benefitDeduction = 180;
        }

    try { /*Catches IOException AND NullPointerException*/ 
            FileWriter fwriter = new FileWriter(empFileName);
            bwriter = new BufferedWriter(fwriter);

            bwriter.write(inputName);
            bwriter.newLine();
            bwriter.write(inputJobTitle);
            bwriter.newLine();
            bwriter.write(inputSalary);
            bwriter.newLine();
            bwriter.write(benefitDeduction);
            bwriter.newLine();
            bwriter.write("----------");
            bwriter.newLine();              
        } catch (IOException | NullPointerException ionpEx) {
            JOptionPane.showMessageDialog(null, "An ERROR has occured", "ERROR!", 2);
            return;
        } finally { /*CLOSE the writer*/                
            try{
                if (bwriter != null) {
                    bwriter.close();
                }
            } catch (IOException ioEx2) {
                JOptionPane.showMessageDialog(null, "ERROR!");
                return;
            }
        }
    }
}
如您所见,我使用int
convertedSalary
将用户输入的
inputSalary
从字符串转换为数字。在我的方法中,我使用

try {
    Integer.parseInt(inputSalary);
} catch (NumberFormatException nfEx) {
    JOptionPane.showMessageDialog(null, "Please ensure that " +
            "your salary is entered in NUMERIC form.", "ERROR!", 2);
    return;
}
为了掩盖它。它稍后(应该)由BufferedWriter编写

我还有以下代码:

try {
    Integer.parseInt(inputSalary);
} catch (NumberFormatException nfEx) {
    JOptionPane.showMessageDialog(null, "Please ensure that " +
                "your salary is entered in NUMERIC form.", "ERROR!", 2);
    return;
}
以根据用户的工资值更改我的<代码>福利扣减额

但是,
benefitextraction
被写入文本文件,但我得到的是:

如果你能帮忙,谢谢你

看看这张照片

只写一个字符。要写入的字符包含在 给定整数值的16个低位;16高阶 位被忽略

这意味着int实际上是一个字符,将根据Unicode/ASCII表进行转换

要写入整数值,请使用
bwriter.writer(String.valueOf(benefitdecredition))

查看

只写一个字符。要写入的字符包含在 给定整数值的16个低位;16高阶 位被忽略

这意味着int实际上是一个字符,将根据Unicode/ASCII表进行转换


要写入整数值,请使用
bwriter.writer(String.valueOf(benefitdecredition))

您的问题是,当您编写数字时,这些值被编写为二进制数字,而不是文本数字。您可以通过执行
writer.write(“+myNumber”)

您的问题是,当您编写数字时,这些值被编写为二进制数字,而不是文本数字。您可以通过执行
writer.write(“+myNumber”)

您的代码有两个主要问题

首先,您实际上没有将
ParseInt
中的值赋给
convertedSalary
变量。因此,它将始终为零

同样,您也没有将
convertedSalary
写入该文件

其次,我认为您混淆了
BufferedWriter.write(int)
PrintWriter.print(int)
。如果该整数介于
0
65535
之间,则
BufferedWriter
将打印该整数表示的字符(如果不在
0
65535
之间,则打印其下两个字节中的任何字符)

因此,如果您的整数是,例如,65,那么您将打印字符
A
,其值是Unicode中的
65


因此,您可能应该使用
PrintWriter
而不是
BufferedWriter
。或者按照其他答案的建议将数字转换为字符串。

您的代码存在两个主要问题

首先,您实际上没有将
ParseInt
中的值赋给
convertedSalary
变量。因此,它将始终为零

同样,您也没有将
convertedSalary
写入该文件

其次,我认为您混淆了
BufferedWriter.write(int)
PrintWriter.print(int)
。如果该整数介于
0
65535
之间,则
BufferedWriter
将打印该整数表示的字符(如果不在
0
65535
之间,则打印其下两个字节中的任何字符)

因此,如果您的整数是,例如,65,那么您将打印字符
A
,其值是Unicode中的
65


因此,您可能应该使用
PrintWriter
而不是
BufferedWriter
。或者按照其他答案的建议将数字转换成字符串。

正如其他答案所指出的,您的问题是调用
write(int)
。但是,您也可以使用
StringBuilder
先创建文本,然后通过调用
write(String)
来编写文本,而不用担心转换。这样,您就不必担心福利扣减的类型:

final String inputName = "Tom";
final String inputJobTitle = "Developer";
final String inputSalary = "1337";
final int benefitDeduction = 180;

try(final BufferedWriter bwriter = new BufferedWriter(new FileWriter("blub.txt"))) {
    final StringBuilder builder = new StringBuilder(inputName);
    builder.append("\n").append(inputJobTitle);
    builder.append("\n").append(inputSalary);
    builder.append("\n").append(benefitDeduction);
    builder.append("\n----------\n");
    bwriter.write(builder.toString());
} catch (IOException | NullPointerException ex) {
  JOptionPane.showMessageDialog(null, "An ERROR has occured", "ERROR!", 2);
  return;
}
还要注意,我使用了来处理
BufferedWriter
。它将处理资源并为您关闭它。不需要手动操作

上述代码将以下数据写入文件
“blub.txt”


正如我在评论中所写的,您没有为变量
convertedSalary
指定新值

更改行:

Integer.parseInt(inputSalary);
致:


要做到这一点。

正如其他答案所指出的,您的问题是调用
write(int)
。但是,您也可以使用
StringBuilder
先创建文本,然后通过调用
write(String)
来编写文本,而不用担心转换。这样,您就不必担心福利扣减的类型:

final String inputName = "Tom";
final String inputJobTitle = "Developer";
final String inputSalary = "1337";
final int benefitDeduction = 180;

try(final BufferedWriter bwriter = new BufferedWriter(new FileWriter("blub.txt"))) {
    final StringBuilder builder = new StringBuilder(inputName);
    builder.append("\n").append(inputJobTitle);
    builder.append("\n").append(inputSalary);
    builder.append("\n").append(benefitDeduction);
    builder.append("\n----------\n");
    bwriter.write(builder.toString());
} catch (IOException | NullPointerException ex) {
  JOptionPane.showMessageDialog(null, "An ERROR has occured", "ERROR!", 2);
  return;
}
还要注意,我使用了来处理
BufferedWriter
。它将处理资源并为您关闭它。不需要手动操作

上述代码将以下数据写入文件
“blub.txt”


正如我在评论中所写的,您没有为变量
convertedSalary
指定新值

更改行:

Integer.parseInt(inputSalary);
致:

要执行此操作。

Integer.parseInt(inputSalary)//返回一个整数

引用JDK: 整数。parseInt();=将字符串参数解析为带符号的十进制整数。字符串中的字符必须全部为十进制数字,但第一个字符可以是ASCII减号“-”(“\u002D”)以表示负值,也可以是ASCII加号“+”(“\u002B”)以表示正值。生成的整数值为retu
convertedSalary = Integer.parseInt(inputSalary);