Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.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 Add方法不在RandomAccessFile中添加学生_Java_Arraylist_Add_Random Access - Fatal编程技术网

Java Add方法不在RandomAccessFile中添加学生

Java Add方法不在RandomAccessFile中添加学生,java,arraylist,add,random-access,Java,Arraylist,Add,Random Access,嘿,我的randomAccessFile应用程序没有将学生添加到文件中。我已经使用arrayList设置了一个学生存储,添加的学生将被添加到存储中,但它不会显示在文件“ContactDetails.txt”中。有人能看出哪里出了问题吗 这是我的密码: 主应用程序 学生商店 //--------------------------------------------------------------------------- //Imports. //----------------------

嘿,我的randomAccessFile应用程序没有将学生添加到文件中。我已经使用arrayList设置了一个学生存储,添加的学生将被添加到存储中,但它不会显示在文件“ContactDetails.txt”中。有人能看出哪里出了问题吗

这是我的密码: 主应用程序

学生商店

//---------------------------------------------------------------------------
//Imports.
//---------------------------------------------------------------------------   
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
//---------------------------------------------------------------------------   

public class StudentStore
{
//---------------------------------------------------------------------------
//ArrayList declaration.
//---------------------------------------------------------------------------
    List<Student> students = new ArrayList<Student>();
//---------------------------------------------------------------------------
//Name:          Add method.
//Description:   Adds a student to the ArrayList.
//---------------------------------------------------------------------------
    public void add(Student student) 
    {
        students.add(student);
    }
//---------------------------------------------------------------------------
//Name:          DeleteAll method.
//Description:   Delete's everything in the ArrayList.
    //---------------------------------------------------------------------------
     public void deleteAll()
     {
           students.clear();
     }
//---------------------------------------------------------------------------
//Name:          Print method.
//Description:   Prints out the contents of the ArrayList.
//---------------------------------------------------------------------------
    public void print() 
    {
        for (int i = 0; i < students.size(); i++) 
        {
          Student a = students.get(i);
            System.out.println(a.toString());
        }
    }
    public int size()
    {
        return (students == null) ? 0 : students.size();
    }
    public void write(RandomAccessFile file) throws IOException
    {
        for (Student s: students)
        {
            byte[] bytes = s.toString().getBytes();
            for(byte byteWrite : bytes)
            {
                file.writeByte(byteWrite);
            }
        }

    }

    public void readAll(RandomAccessFile file) throws IOException
    {
        final int Record_Length = 30;
        int recordNumber = 0;
        file.seek((recordNumber) * Record_Length);

        String code ="";
        for(int i = 0; i < 30; i++)
        {
        code += file.readLine() + "\n";
        }
        System.out.println(code);
    }

}

请张贴学生班级代码 是否重写学生类中的toString()方法

我有测试你的代码,如果我输入1,它会打印你在你的主方法中输入的所有学生信息。 但是如果我输入2,在输入学生信息之后,您只需将信息添加到内存中的Arraylist中,而不是将其输出到文件中。所以我输入1,它不会输出我之前输入的新学生信息

我认为您可能需要将新学生信息输出到文件中

编辑

在read All方法中,您刚刚读取了文件的前30行 改变


谢天谢地,这个人工作得很卖力。我可以问一下readAll方法到底在做什么吗?
//---------------------------------------------------------------------------
//Imports.
//---------------------------------------------------------------------------   
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
//---------------------------------------------------------------------------   

public class StudentStore
{
//---------------------------------------------------------------------------
//ArrayList declaration.
//---------------------------------------------------------------------------
    List<Student> students = new ArrayList<Student>();
//---------------------------------------------------------------------------
//Name:          Add method.
//Description:   Adds a student to the ArrayList.
//---------------------------------------------------------------------------
    public void add(Student student) 
    {
        students.add(student);
    }
//---------------------------------------------------------------------------
//Name:          DeleteAll method.
//Description:   Delete's everything in the ArrayList.
    //---------------------------------------------------------------------------
     public void deleteAll()
     {
           students.clear();
     }
//---------------------------------------------------------------------------
//Name:          Print method.
//Description:   Prints out the contents of the ArrayList.
//---------------------------------------------------------------------------
    public void print() 
    {
        for (int i = 0; i < students.size(); i++) 
        {
          Student a = students.get(i);
            System.out.println(a.toString());
        }
    }
    public int size()
    {
        return (students == null) ? 0 : students.size();
    }
    public void write(RandomAccessFile file) throws IOException
    {
        for (Student s: students)
        {
            byte[] bytes = s.toString().getBytes();
            for(byte byteWrite : bytes)
            {
                file.writeByte(byteWrite);
            }
        }

    }

    public void readAll(RandomAccessFile file) throws IOException
    {
        final int Record_Length = 30;
        int recordNumber = 0;
        file.seek((recordNumber) * Record_Length);

        String code ="";
        for(int i = 0; i < 30; i++)
        {
        code += file.readLine() + "\n";
        }
        System.out.println(code);
    }

}
//---------------------------------------------------------------------------------------
//  Name:           Imports. 
//  Description:    To allow the use of different Java classes.
//---------------------------------------------------------------------------------------
import java.util.Scanner;
//---------------------------------------------------------------------------------------

public class MenuMethods 
{
    private static Scanner keyboard = new Scanner(System.in);
//---------------------------------------------------------------------------------------
//  Methods for the Company Application menu.
//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------
//  Name:           getMenuChoice.
//  Description:    Method for validating the choice.
//---------------------------------------------------------------------------------------
    public static int getMenuChoice(String menuString, int limit,String prompt, String errorMessage) 
    {
        System.out.println(menuString);
        int choice = inputAndValidateInt(1, limit, prompt, errorMessage);
        return choice;
    }

//---------------------------------------------------------------------------------------
//  Name:        inputAndValidateInt.
//  Description: This method is used in the getMenuChoice method.
//---------------------------------------------------------------------------------------
    public static int inputAndValidateInt(int min, int max, String prompt,String errorMessage) 
    {
        int number;
        boolean valid;
        do 
        {
            System.out.print(prompt);
            number = keyboard.nextInt();
            valid = number <= max && number >= min;
            if (!valid) 
            {
                System.out.println(errorMessage);
            }
        } while (!valid);
        return number;
    }

//---------------------------------------------------------------------------------------
//  Name:        userInput
//  Description: This method is used in the MainApp to give the user capability to enter
//               the details when adding details of an employee into the store.
//---------------------------------------------------------------------------------------
    public static Student userInput() 
    {
        String temp = keyboard.nextLine();
        Student s = null;
        System.out.println("Please enter the Student Name:");
        String studentName = keyboard.nextLine();
        System.out.println("Please enter the Student ID:");
        String studentId = keyboard.nextLine();
        System.out.println("Please enter the Student E-mail address:");
        String studentEmail = keyboard.nextLine();
        System.out.println("Please enter the Student telephone number:");
        String studentTelephoneNumber = keyboard.nextLine();
        return s = new Student(studentName, studentId, studentEmail,studentTelephoneNumber);

    }

//---------------------------------------------------------------------------------------
//  Name:        userInputByName.
//  Description: This method is used in the MainApp to give the user capability to search by name.
//---------------------------------------------------------------------------------------
    public static Student userInputByName() 
    {
        // String temp is for some reason needed. If it is not included
        // The code will not execute properly.
        String temp = keyboard.nextLine();
        Student s = null;
        System.out.println("Please enter the Student Name:");
        String studentName = keyboard.nextLine();

        return s = new Student(studentName);

    }

//---------------------------------------------------------------------------------------
//  Name:        userInputByEmail
//  Description: This method is used in the MainApp to give the user capability to search by email.
//---------------------------------------------------------------------------------------
    public static String userInputByEmail() 
    {
        // String temp is for some reason needed. If it is not included
        // The code will not execute properly.
        String temp = keyboard.nextLine();
        Student s = null;
        System.out.println("Please enter the StudentEmail:");
        String studentEmail = keyboard.nextLine();
        // This can use the employeeName's constructor because java accepts the
        // parameters instead
        // of the name's.
        return studentEmail;

    }
//---------------------------------------------------------------------------------------
}
import java.io.Serializable;

public class Student implements Serializable
{
//---------------------------------------------------------------------------
//  Class Variables.
//---------------------------------------------------------------------------   
    private String studentName;
    private String studentId;
    private String studentTelephoneNumber;
    private String studentEmail;
//---------------------------------------------------------------------------
//  Constructor.
//---------------------------------------------------------------------------   
    public Student(String studentName, String studentId,String studentTelephoneNumber, String studentEmail) 
    {
        this.studentName = studentName;
        this.studentId = studentId;
        this.studentTelephoneNumber = studentTelephoneNumber;
        this.studentEmail = studentEmail;
    }
//---------------------------------------------------------------------------------------
//  Overloading the constructor for the use with userInputByName method.
//---------------------------------------------------------------------------------------
    public Student(String studentName) 
    {
        this.studentName = studentName;
    }
//---------------------------------------------------------------------------
//  Getters.
//---------------------------------------------------------------------------
    public String getStudentName()
    {
        return studentName;
    }
    public String getStudentId() 
    {
        return studentId;
    }
    public String getStudentTelephoneNumber() 
    {
        return studentTelephoneNumber;
    }
    public String getStudentEmail() 
    {
        return studentEmail;
    }
//---------------------------------------------------------------------------
//  Setters.
//---------------------------------------------------------------------------
    public void setStudentName(String studentName) 
    {
        this.studentName = studentName;
    }
    public void setStudentId(String studentId) 
    {
        this.studentId = studentId;
    }
    public void setStudentTelephoneNumber(String studentTelephoneNumber) 
    {
        this.studentTelephoneNumber = studentTelephoneNumber;
    }
    public void setStudentEmail(String studentEmail) 
    {
        this.studentEmail = studentEmail;
    }
//---------------------------------------------------------------------------
//  toString.
//---------------------------------------------------------------------------
    public String toString() 
    {
        return "---------------------------Student--------------------------- " +
                "\nStudent Name:" + studentName + 
                "\nStudent Id:"+ studentId + 
                "\nStudent Telephone Number:"+ studentTelephoneNumber + 
                "\nStudent Email:" + studentEmail +"\n\n";
    }


}
case 2:
                        System.out.println("Add");
                        Student student = MenuMethods.userInput();
                        details.add(student);
                        break;
case 2:
                        System.out.println("Add");
                        Student student = MenuMethods.userInput();
                        details.add(student);
file.write(student.toString().getBytes());
                    break;
public void readAll(RandomAccessFile file) throws IOException
    {
        final int Record_Length = 30;
        int recordNumber = 0;
        file.seek((recordNumber) * Record_Length);

        String code ="";
        for(int i = 0; i < 30; i++)
        {
        code += file.readLine() + "\n";
        }
        System.out.println(code);
    }
public void readAll(RandomAccessFile file) throws IOException
    {
        file.seek(0);

        String code;
while((code=file.readLine())!=null)
        System.out.println(code);
    }