Here are the steps that I made in creating my Simple File Handling System.
For the main class, I used the JFrame form. The following codes composed the main methods of my file handling system which include the Writing, Reading and Deleting the entire file.
____________________________________________________________________
Below is the design that I formulated in JFrame form. The Palettes that I used are also indicated below.
In addition, I created another class (for the Reading function) within the main class above. For reference, here are the codes:
____________________________________________________________________
package textfiles;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
public class Readfile {
private String path;
public Readfile(String file_path){
path=file_path;
}
int numberOfLines = 4;
int readLines() throws IOException{
FileReader file_to_read = new FileReader(path); BufferedReader bf=new BufferedReader(file_to_read); String aLine;
while (( aLine = bf.readLine()) != null){
numberOfLines++;} bf.close(); return numberOfLines;
}
public String[] OpenFile() throws IOException { FileReader fr =new FileReader(path); BufferedReader textReader = new BufferedReader(fr); String[ ] textData = new String[numberOfLines]; int i;
for (i=0; i < numberOfLines; i++) {
textData[ i ] = textReader.readLine();
}
textReader.close();return textData;
}
}
____________________________________________________________________
Next, I also created another class for the Writing function. I still located this in the main class. The codes are as follow:
____________________________________________________________________
package textfiles;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class WriteFile {
private String path;
private boolean append_to_file=false;
public WriteFile(String file_path){
path = file_path;
}
public WriteFile(String file_path, boolean append_value){
path = file_path;
append_to_file=append_value;
}
public void writeToFile( String textLine ) throws IOException {
FileWriter write = new FileWriter (path, append_to_file);
PrintWriter print_line = new PrintWriter(write);
print_line.printf("%s" + "%n", textLine);
print_line.close();
}
}
____________________________________________________________________
The organization of my project package is shown in the highlighted box below:
Finally, the output of my program is:
____________________________________________________________________
THANK YOU FOR READING!!! :) :) :)
(Comments, suggestions and reactions are welcomed and will be highly appreciated.)
____________________________________________________________________
No comments:
Post a Comment