gFile Class

Step 1: problem domain

  • This class is used to read and write text files.
  • Text files consist of several lines and each line is a String

 

Step 2: Public Interface

    //Constructors
    public gFile();
	public gFile(String FileAddress);
	
    // use to change file, if it does not exist, create it
	public void SetFileAddress (String FileAddress)
	
	public void Clear (); // clear contents of given file
	public void Load ();  // loads whole text file into array A
	public void Save();   // save array A into file

	// append newItem to end of file as a new line
	public void Append (String newItem); 
	public void InsertRow (int Row,String newItem);
	public void UpdateRow (int Row,String newItem);
	public void DeleteRow (int Row);
	public int Find(String s); //returns row number

	
	public boolean hasNextLine();
	public String nextLine();	
	public int getLines ();
	public String getLine (int Row);
	public String[] getAll ();

 

Step 3: private variables

private String FileAddress;
private String A[]; 
private int cA;  // Length of A

 

Step 4: implementing main classes

Students work !

Step 5: useful application

To test the correctness and abilities of gFile, Students should develop a small project.

In this project Users can enter file name and create a text file. Then by the use of menu, users can manipulate some lines of text file and etc.

gFile will be used widely in next projects such as gIS.