Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在数组中存储值并将其保存在文件中_Java - Fatal编程技术网

Java 如何在数组中存储值并将其保存在文件中

Java 如何在数组中存储值并将其保存在文件中,java,Java,这是我代码的一部分。基本上,我试图将double Avg和char gradeLetter中的值存储在数组double Avg和charGRADE中。到目前为止,我得到的是,在第一个循环中,假设Avg=2,那么它将被存储在文件中,但是如果有第二个循环,并且Avg的值更改为3,那么3将被保存在文件中两次。它删除2并保存数字3两次。我怎样才能解决这个问题?我希望第一个循环存储Avg的第一个值,可以是2,然后在第二个循环中存储Avg的新值,可以是3,但不覆盖2。忽略这些评论 try {

这是我代码的一部分。基本上,我试图将double Avg和char gradeLetter中的值存储在数组double Avg和charGRADE中。到目前为止,我得到的是,在第一个循环中,假设Avg=2,那么它将被存储在文件中,但是如果有第二个循环,并且Avg的值更改为3,那么3将被保存在文件中两次。它删除2并保存数字3两次。我怎样才能解决这个问题?我希望第一个循环存储Avg的第一个值,可以是2,然后在第二个循环中存储Avg的新值,可以是3,但不覆盖2。忽略这些评论

    try {
        FileWriter fw = new FileWriter(fileN);

        for (int count = 0; count < students; count++) {

            doubleAVG = new double[students];
            charGRADE = new char[students];

            doubleAVG[count] = Avg;
            charGRADE[count] = gradeLetter;

            fw.write("This is the grade of: " + FirstName + " " + LastName
                    + " ");
            fw.write(String.valueOf(doubleAVG[count]) + " ");
            fw.write(charGRADE[count]);
        }
        fw.close();
    } catch (Exception e) {
        System.out.println(e);
    }
试试看{
FileWriter fw=新的FileWriter(fileN);
for(int count=0;count
您知道java Serialize方法(ology)吗?该方法创建一个包含类文件中所有数据结构及其对象的xml文件,使其中的嵌套结构在特定时间点看起来像对象,就像原始xml一样?这是可能的,因为java在5种确切的本机数据类型上进行中继,而对象只是这些数据类型的集合。看看上面的评论和Stackoverflow答案的链接,我刚刚向您解释了一点机制

请将您的代码修改为:

public class Main {

    public static void main(String[] args) {
        try {
            FileWriter fw = new FileWriter(new File("F://test.txt"));
            Scanner sc = new Scanner(System.in);
            int students = 2;
            double[] doubleAVG = new double[students];
            char[] charGRADE = new char[students];
            double avg = 0.0;
            char gradeLetter;
            String FirstName = "";
            String LastName = "";
            for (int count = 0; count < students; count++) {
                System.out.println("Enter average :");
                avg = sc.nextDouble();
                System.out.println("Enter grade :");
                gradeLetter = sc.next().charAt(0);
                System.out.println("Enter First Name :");
                FirstName = sc.next();
                System.out.println("Enter Last Name :");
                LastName = sc.next();
                doubleAVG[count] = avg;
                charGRADE[count] = gradeLetter;
                fw.write("This is the grade of: " + FirstName + " " + LastName + " ");
                fw.write(String.valueOf(doubleAVG[count]) + " ");
                fw.write(charGRADE[count] + System.lineSeparator());
            }
            fw.close();
            sc.close();
        } catch (Exception e) {
            System.out.println(e);
        }

    }

}
在test.txt文件中:

This is the grade of: John Johnson 70.45 B
This is the grade of: Michael Jordan 90.47 A

一旦填充了数组,就可以简单地将其序列化为文件。请参阅。请一步一步(以点为单位)描述您希望实现的目标。您正在保存的内容、来源、位置?
忽略注释。
:请不要用大写字母书写。为什么不删除这些无用的注释以生成更可读的代码呢;最后?这意味着什么?必须关闭Scanner类才能消除内存泄漏。我来看看。谢谢
This is the grade of: John Johnson 70.45 B
This is the grade of: Michael Jordan 90.47 A