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 {
- int index;
- String Filename = null; //String to hold Filename for txt file
- File f = null; //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 set Filename
- void SetFileName(String Filename) {
- this.Filename = Filename;
- }
- //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");
- }
- }
- void FWrite(String element1,String element2) {
- try {
- FileWriter FWriter = new FileWriter(Filename, true);
- pw = new PrintWriter(FWriter);
- pw.println(element1 + " - " + element2);
- pw.close();
- }
- catch(Exception e) {
- System.out.println("error Write");
- }
- }
- //Method to Load file into list.
- void Freader() {
- f = new File(Filename);
- 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++) {
- String Element = file.get(i);
- if (Element.equals(ElementToSearch)) {
- System.out.println("file found :" + file.get(i) + " " + file.get(i+1) + " " + file.get(i+2) );
- index = 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(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++) {
- String Element = file.get(i);
- if (Element.equals(ElementToReplace)) {
- file.set(i, ElementToReplaceWith);
- break;
- }
- }
- }
- void clearList() {
- file.removeAll(file);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement