如何将此代码写入文本文件height.txt?它会创建它,但它不会写入它。 并且它还编译并说数据写入文件但是当我打开文件时没有任何数据为什么会这样?
import java.io.*;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileWriter;
public class readinguserinput {
public static String gender;
public static int motherHeight;
public static int fatherHeight;
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
try
{
FileWriter fw = new FileWriter("height.txt");
PrintWriter pw = new PrintWriter(fw);
System.out.println ("Enter gender");
gender = keyboard.next();
System.out.println ("Enter Mother Height");
motherHeight = keyboard.nextInt();
keyboard.nextLine();
while (motherHeight < 0)
{
System.out.println ("Enter Mother Height");
motherHeight = keyboard.nextInt();
}
System.out.println ("Enter father Height");
fatherHeight = keyboard.nextInt();
while (fatherHeight < 0)
{ System.out.println ("Enter Father Height");
fatherHeight = keyboard.nextInt();
}
pw.close();
}catch (IOException e){
System.out.println("file not found");
}
System.out.println("data written to the file");}}
答案 0 :(得分:0)
代码永远不会向文件写入任何内容。试试pw.print()
和pw.println()
。
答案 1 :(得分:0)
变化:
System.out...
要:
pw.out...
您当前正在将输出写入控制台,而不是PrintWriter
答案 2 :(得分:0)
您的程序只打印您要打印的内容。在这种情况下,您告诉它打印'写入文件的数据',但您没有告诉它实际上写任何文件。你的程序按照你的指示对你撒谎。
答案 3 :(得分:0)
编写文本文件的示例程序
String content = "This is the content to write into file";
File file = new File("/data/filename.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(content);
bufferedWriter.close();
答案 4 :(得分:0)
如前所述。您实际上没有在文件中写入任何内容。 试试这个
import java.io.*;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.FileWriter;
public class readinguserinput {
public static String gender;
public static int motherHeight;
public static int fatherHeight;
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
try
{
FileWriter fw = new FileWriter("height.txt");
PrintWriter pw = new PrintWriter(fw);
System.out.println ("Enter gender");
gender = keyboard.next();
pw.println("Gender: " + gender); // ***************
System.out.println ("Enter Mother Height");
motherHeight = keyboard.nextInt();
pw.println("motherHeight: " + motherHeight); // ***************
keyboard.nextLine();
while (motherHeight < 0)
{
System.out.println ("Enter Mother Height");
motherHeight = keyboard.nextInt();
pw.println("motherHeight: " + motherHeight); // ***************
}
System.out.println ("Enter father Height");
fatherHeight = keyboard.nextInt();
pw.println("fatherHeight: " + fatherHeight); // ***************
while (fatherHeight < 0)
{ System.out.println ("Enter Father Height");
fatherHeight = keyboard.nextInt();
pw.println("fatherHeight: " + fatherHeight); // ***************
}
pw.close();
}catch (IOException e){
System.out.println("file not found");
}
System.out.println("data written to the file");}}