Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Front_desk;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.PrintWriter;
- import java.util.*;
- public class FileSys {
- String Filename; //String to hold Filename for txt file
- File f = new File(Filename); //file object for file operations
- List<String> file = new ArrayList<>(); // Arraylist to load file elements into list
- PrintWriter pw = null; //initialization of printWriter obeject.
- //Method to write to file.
- void FWrite(String element) {
- try {
- FileWriter FWriter = new FileWriter(Filename, true);
- pw = new PrintWriter(FWriter);
- pw.println(element);
- pw.close();
- }
- catch(Exception e) {
- System.out.println("error Write");
- }
- }
- //Method to Load file into list.
- void Freader() {
- try {
- Scanner Fread = new Scanner(f);
- while(Fread.hasNext()) {
- file.add(Fread.next());
- }
- Fread.close();
- }
- catch (Exception e) {
- System.out.println("error read");
- }
- }
- //Method to print list elements
- void arrListPrint() {
- for (String element : file) {
- System.out.println("element = " + element);
- }
- }
- //Method to search for elements in the list
- void SearchFile (String ElementToSearch) {
- for (int i=0; i<file.size(); i++) {
- if (file.get(i) == ElementToSearch) {
- System.out.println("file found :" + file.get(i));
- }
- }
- }
- //Method to write back to the file
- void ListToFile() {
- try {
- FileWriter FWriter = new FileWriter(Filename, false);
- pw = new PrintWriter(FWriter);
- for (String element : file) {
- pw.println("New: " + element);
- }
- pw.close();
- }
- catch(Exception e) {
- System.out.println("error Write");
- }
- }
- //Method to replace File objects with "empty"
- void MakeEmpty (String ElementToReplace) {
- for (int i=0; i<file.size(); i++) {
- if (file.get(i) == ElementToReplace) {
- file.set(i, "empty");
- }
- }
- }
- //Method to replace File objects
- void ReplaceFile (String ElementToReplace, String ElementToReplaceWith) {
- for (int i=0; i<file.size(); i++) {
- if (file.get(i) == ElementToReplace) {
- file.set(i, ElementToReplaceWith);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement