java编程,写入文本文件

java编程,写入文本文件,java,Java,如何使此代码写入文本文件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

如何使此代码写入文本文件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");}}
import java.io.*;
导入java.util.Scanner;
导入java.io.PrintWriter;
导入java.io.FileWriter;
公共类读入输入{
公共静态字符串性别;
公共静态高度;
公众静态身高;
静态扫描仪键盘=新扫描仪(System.in);
公共静态void main(字符串[]args){
尝试
{
FileWriter fw=新的FileWriter(“height.txt”);
PrintWriter pw=新的PrintWriter(fw);
System.out.println(“输入性别”);
性别=键盘。下一步();
System.out.println(“输入母亲身高”);
motherHeight=keyboard.nextInt();
keyboard.nextLine();
而(高度<0)
{
System.out.println(“输入母亲身高”);
motherHeight=keyboard.nextInt();
}
System.out.println(“输入父亲身高”);
父亲身高=键盘.nextInt();
而(父亲身高<0)
{System.out.println(“输入父亲身高”);
父亲身高=键盘.nextInt();
}
关闭();
}捕获(IOE异常){
System.out.println(“未找到文件”);
}
System.out.println(“写入文件的数据”);}

代码从不向文件写入任何内容。尝试
pw.print()
pw.println()

更改:

System.out...
致:


您当前正在将输出写入控制台,而不是PrintWriter。您的程序只打印您要打印的内容。在本例中,您告诉它打印“写入文件的数据”,但没有告诉它实际写入任何文件。您的程序根据您的指示向您撒谎。

编写文本文件的示例程序

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();

请参阅:如前所述。实际上,您还没有向文件中写入任何内容。 试试这个

import java.io.*;
导入java.util.Scanner;
导入java.io.PrintWriter;
导入java.io.FileWriter;
公共类读入输入{
公共静态字符串性别;
公共静态高度;
公众静态身高;
静态扫描仪键盘=新扫描仪(System.in);
公共静态void main(字符串[]args){
尝试
{
FileWriter fw=新的FileWriter(“height.txt”);
PrintWriter pw=新的PrintWriter(fw);
System.out.println(“输入性别”);
性别=键盘。下一步();
pw.println(“性别:+性别);//***************
System.out.println(“输入母亲身高”);
motherHeight=keyboard.nextInt();
pw.println(“motherHeight:+motherHeight”);//***************
keyboard.nextLine();
而(高度<0)
{
System.out.println(“输入母亲身高”);
motherHeight=keyboard.nextInt();
pw.println(“motherHeight:+motherHeight”);//***************
}
System.out.println(“输入父亲身高”);
父亲身高=键盘.nextInt();
pw.println(“父亲身高:+父亲身高);//***************
而(父亲身高<0)
{System.out.println(“输入父亲身高”);
父亲身高=键盘.nextInt();
pw.println(“父亲身高:+父亲身高);//***************
}
关闭();
}捕获(IOE异常){
System.out.println(“未找到文件”);
}
System.out.println(“写入文件的数据”);}

他正在将控制台输出写入控制台。如果他遵循这个建议,他将失去自己的用户界面。他不需要更改他编写的任何用户界面。它们显然都是为控制台设计的。他需要添加新的值,包括从控制台读取的值。这里既不是面包,也不是面包屑。虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,则“仅链接”答案可能无效。@benka我已包含示例代码。谢谢你提供的信息!
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();
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");}}