我正在制作一个简单的教程程序,用户在其中输入一些数据,然后将这些数据写入文本文件,然后读取文本文件并显示数据。但是,并非所有数据都被写入文本文件。请查看以下内容:
这是我的代码:
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;
}
为了根据用户的薪水值改变我的benefitDeduction
的值。
但是,benefitDeduction
已写入文本文件,但我得到的是:
谢谢,如果你能帮忙!
最佳答案
查看API .
public void write(int c)
throws IOException
Writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
这意味着int实际上是一个char,将根据Unicode/ASCII表进行转换。
要写入整数值,请使用 bwriter.writer(String.valueOf(benefitDeduction));
关于java - BufferedWriter 不写入所有信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27826860/