程序显示文件内容为“java.io.FileWriter@55f96302” - Java

时间:2015-03-17 12:32:55

标签: java

对于我的菜单,我在菜单中有一个选项(案例2)以显示文件的内容(" Grades.txt")。如果我输入示例数据:

First Name: Fred
Surname: Smith
Course: IT
Mark: 66

它将文件另存为:

Fred Smith, Computing, 66

哪个是对的。但是,当我在菜单中选择选项2后,要显示文件的内容,显示为:

java.io.FileWriter@55f96302

现在我环顾四周,找不到任何类似的东西。

如果有人能帮忙解决这个问题,我将不胜感激。感谢

代码:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class ExamGrades {

    public static void main(String[] args) throws Exception {
        FileWriter grades = new FileWriter("grades.txt",true);
        BufferedWriter bw = new BufferedWriter(grades);
        PrintWriter out = new PrintWriter(bw);
        Scanner scan = new Scanner(System.in);

        int examMark =0;
        String firstName = "";
        String surName = "";
        String course = "";
        String entry = "";
        int menu =0;

        System.out.println("Welcome to the 'GradeEnter' program! ");
        System.out.println("Menu: ");
        System.out.println("1) Enter Student Grade(s)");
        System.out.println("2) View Student Grade(s)");
        System.out.println("3) Delete Grade(s)");
        System.out.println("4) Exit");
        menu = scan.nextInt();
        switch (menu) {
        case 1:
            System.out.print("Please enter student first Name: "); 
            firstName = scan.next(); 

            System.out.print("Please enter student surname: ");
            surName = scan.next();

            System.out.print("Please select student course: "); 
            course = scan.next();

            System.out.print("Please enter student mark: ");
            while (!scan.hasNextInt())
            {
                System.out.print("Please enter a valid mark: ");
                scan.next();
            }
            examMark = scan.nextInt();

            if (examMark < 40)
            {
                System.out.println("Failed");
            }
            else if (examMark >= 40 && examMark <= 49)
            {
                System.out.println("3rd");
            }
            else if (examMark >= 50 && examMark <= 59)
            {
                System.out.println("2/2");
            }
            else if (examMark >= 60 && examMark <= 69)
            {
                System.out.println("2/1");
            }
            else if (examMark >= 70 && examMark <= 100)
            {
                System.out.println("1st");
            }
            else
            {
                System.out.println("Invalid Mark");
            }
            break;

        case 2:
            System.out.println(grades); 
        case 3:
            /*          System.out.print("Please enter a the first name you want to delete: ");
            if (firstName == scan.next()) {
            grades.remove(scan.next());
            }
             */
        case 4:
            System.out.println("Thanks for using 'GradeEnter' ");
            System.exit(0);
        default:
        } 
        entry = (firstName + " " + surName + ", " + course + ", " + examMark);
        out.println(entry);

        out.close();
        scan.close();
    }
}

3 个答案:

答案 0 :(得分:2)

在FileWriter中成绩,因此您只能将数据写入文件。 如果您想从文件中读取数据,则需要使用FileReader http://docs.oracle.com/javase/7/docs/api/java/io/FileReader.html 然后打印文件的内容

答案 1 :(得分:0)

您不能只打印FileWriter对象。特别是 - 您不需要使用FileWriter。 Reading a plain text file in Java。这个主题可以帮到你。

答案 2 :(得分:0)

我认为程序的行为符合预期,您正在打印对象本身。

从我理解的问题中,您希望在选择选项2时显示内容。所以,如果你想读取文件的内容,你需要使用FileReader或BufferedReader。