出于某种原因,当我尝试将一个名为 duration 的 int 写入一个名为 newSession
的文件并且程序已完成编译并打开位于桌面上的文件时,所有其他文件都很好(这意味着我想写入该文件的内容已成功写入)但是 newSession
中写入了随机字母。这是为什么,谁能解释为什么没有将 int 持续时间写入文件 newSession
而是随机字母。
package kappa;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Reader
{
public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException, IOException
{
File dateFile = null;
Scanner reader = null;
try
{
String filePath = "/Users/john/Desktop/firstTime.txt";
Scanner reader2 = null;
while(true)
{
FileWriter fw = new FileWriter(filePath);
BufferedWriter bw = new BufferedWriter(fw);
File firstTime = new File(filePath);
firstTime.createNewFile();
bw.write("1");
bw.close();
reader2 = new Scanner(new File(filePath));
break;
}
if(reader2.nextInt() == 1)
{
dateFile = new File("/Users/john/Desktop/Kunja.txt");
dateFile.createNewFile();
reader = new Scanner(dateFile);
}
// if file doesnt exists, then create it
if (dateFile.exists())
{
FileWriter fw2 = new FileWriter(dateFile.getAbsoluteFile());
BufferedWriter bw2 = new BufferedWriter(fw2);
dateFile.createNewFile();
bw2.write("0");
bw2.close();
System.out.println("Done");
int duration;
String ans = JOptionPane.showInputDialog ("Enter the amount of problems per training session (with number in minutes):");
while(!ans.matches("[0-9]+"))
{
ans = JOptionPane.showInputDialog ("Please re-enter the amount of problems per training session (with number in minutes):" );
}
duration = Integer.parseInt(ans);
System.out.println("Duration is " + duration);
int numSessions = (reader.nextInt() + 1);
System.out.println("Number of sessions is: " + numSessions);
String fileName = ("sessionNumber"+numSessions);
File newSession = new File("/Users/john/Desktop/"+fileName);
System.out.println(fileName);
if (!newSession.exists())
{
newSession.createNewFile();
}
FileWriter fw3 = new FileWriter(newSession.getAbsoluteFile());
System.out.println("THE FILE PATH IS " + newSession.getAbsoluteFile());
BufferedWriter bw3 = new BufferedWriter(fw3);
bw3.write(duration);
bw3.close();
}else
{
int duration;
String ans = JOptionPane.showInputDialog ("Enter a number (only numbers please)");
while(!ans.matches("[0-9]+"))
{
ans = JOptionPane.showInputDialog ("Please re-enter a number (NOTHING ELSE!)" );
}
duration = Integer.parseInt(ans);
System.out.println(duration);
int numSessions = reader.nextInt();
System.out.println("Number of sessions is: " + numSessions);
String fileName = ("sessionNumber"+numSessions);
File newSession = new File("/Users/john/Desktop/"+fileName);
System.out.println(fileName);
if (!newSession.exists())
{
newSession.createNewFile();
System.out.println("IT DOES NOT EXIST!");
}
FileWriter fw = new FileWriter(newSession.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(duration);
bw.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
最佳答案
BufferedWriter#write(int)
采用单个字符(由 int
表示)。如果您想编写整数的文本表示,您必须自己将其转换为 String
。
简而言之,替换:
bw3.write(duration);
与:
String durationString = String.valueOf(duration);
bw3.write(durationString, 0, durationString.length());
关于java - BufferedWriter 将随机字母写入文件(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31087253/