modulefile.java

来自「This example shows how to create a secon」· Java 代码 · 共 71 行

JAVA
71
字号
import java.util.*;
import java.io.*;
/**
 * Class to obtain student list from a file.
 * 
 * @author Ian Bradley 
 * @version 10/03/2007
 */
public class ModuleFile extends Module
{


	/**
	 * Constructor for objects of class ModuleFile
	 */
	public ModuleFile(String moduleName)
	{
		super(moduleName);
		readModuleList(moduleName);
	}

	/**
	 * reads data from a text file
	 * format name  id
	 * 
	 * @param  fileName name of file based on module fileName.txt
	 */
	private void readModuleList( String fileName)
	{
	    String studentName;
	    String studentID;
	    try
	    {
	        Scanner fileScanner = new Scanner( new File ( fileName + ".txt"));
	        while (fileScanner.hasNext())
	        {
	            studentName = fileScanner.next();
	            studentID = fileScanner.next();
	            addStudent(studentName, studentID);
	        }
	        fileScanner.close();
	    }
	    catch (IOException e)
        {
            System.out.println("File not found");
        }
    }
    
    
    public void saveModuleList()
    {
        String fileName = getModuleName() +".txt" ;  
        try { 
          PrintWriter print = new PrintWriter( 
                      new BufferedWriter( 
                                 new FileWriter( fileName ) ) ); 
          
          print.println(getAllStudents());
          print.close();
        } 
        catch ( IOException iox ) {
           System.out.println("Problem writing " + fileName ); 
        } 

    } 

   
    
	            
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?