⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modulefile.java

📁 This example shows how to create a second window and also how to read and write from/to a text file
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -