2014-10-28 79 views
0

如何让这段代码写入文本文件height.txt?它创建它,但它不写入它。 它也编译并说数据写入文件,但没有任何数据,当我打开该文件为什么?java编程,写入文本文件

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

的代码永远不会向文件中写入任何东西。试试pw.print()pw.println()

0

变化:

System.out... 

要:

pw.out... 

您正在输出写入到控制台,而不是你的PrintWriter

+0

他正在写控制台输出到控制台。如果他遵循这个建议,他将失去他的用户界面。 – EJP 2014-10-28 05:05:59

+0

我给了面包屑,而不是一个面包,他不需要改变他们所有的人。 – CharlieS 2014-10-28 06:19:19

+0

他不需要改变他写的任何东西。他们都清楚地用于控制台。他需要添加新的,包括从控制台读取的值。这里既没有面包,也没有面包屑。 – EJP 2014-10-28 08:49:11

0

你的程序只打印你告诉它打印的内容。在这种情况下,你告诉它打印'写入文件的数据',但你没有告诉它实际上写入什么文件。你的程序根据你的指示骗了你。

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

参见:How to write text file in Java ...

+0

尽管此链接可能回答问题,但最好在此处包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – benka 2014-10-28 08:56:47

+0

@benka我已经包含了示例代码。感谢您的信息! – 2014-10-28 09:07:02

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");}}