Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. package Front_desk;
  2.  
  3.  
  4.  
  5. import java.io.File;
  6. import java.io.FileWriter;
  7. import java.io.PrintWriter;
  8. import java.util.*;
  9.  
  10. public class FileSys {
  11.     int index;
  12.     String Filename = null; //String to hold Filename for txt file
  13.    
  14.     File f = null; //file object for file operations
  15.    
  16.     List<String> file = new ArrayList<>(); // Arraylist to load file elements into list
  17.    
  18.     PrintWriter pw = null; //initialization of printWriter obeject.
  19.    
  20.    
  21.     //Method to set Filename
  22.     void SetFileName(String Filename) {
  23.         this.Filename = Filename;
  24.     }
  25.    
  26.     //Method to write to file.
  27.    
  28.     void FWrite(String element) {
  29.         try {
  30.             FileWriter FWriter = new FileWriter(Filename, true);
  31.             pw = new PrintWriter(FWriter);
  32.             pw.println(element);
  33.             pw.close();
  34.         }
  35.         catch(Exception e) {
  36.             System.out.println("error Write");
  37.         }
  38.     }
  39.        
  40.         void FWrite(String element1,String element2) {
  41.             try {
  42.                 FileWriter FWriter = new FileWriter(Filename, true);
  43.                 pw = new PrintWriter(FWriter);
  44.                 pw.println(element1 + " - " + element2);
  45.                 pw.close();
  46.             }
  47.        
  48.         catch(Exception e) {
  49.             System.out.println("error Write");
  50.         }
  51.     }
  52.  
  53.     //Method to Load file into list.
  54.    
  55.     void Freader() {
  56.         f = new File(Filename);
  57.         try {
  58.            
  59.             Scanner Fread = new Scanner(f);
  60.            
  61.             while(Fread.hasNext()) {
  62.                 file.add(Fread.next());
  63.             }
  64.             Fread.close();
  65.         }
  66.         catch (Exception e) {
  67.             System.out.println("error read");
  68.         }
  69.     }
  70.    
  71.     //Method to print list elements
  72.    
  73.     void arrListPrint() {
  74.         for (String element : file) {
  75.             System.out.println("element = " + element);
  76.         }
  77.     }
  78.    
  79.     //Method to search for elements in the list
  80.    
  81.     void SearchFile (String ElementToSearch) {
  82.         for (int i=0; i<file.size(); i++) {
  83.             String Element = file.get(i);
  84.             if (Element.equals(ElementToSearch)) {
  85.                 System.out.println("file found :" + file.get(i) + " " + file.get(i+1) + " " + file.get(i+2) );
  86.                 index = i;
  87.             }
  88.         }
  89.     }
  90.    
  91.     //Method to write back to the file
  92.    
  93.     void ListToFile() {
  94.        
  95.         try {
  96.             FileWriter FWriter = new FileWriter(Filename, false);
  97.             pw = new PrintWriter(FWriter);
  98.            
  99.             for (String element : file) {
  100.                 pw.println(element);
  101.             }
  102.            
  103.             pw.close();
  104.         }
  105.         catch(Exception e) {
  106.             System.out.println("error Write");
  107.         }
  108.     }
  109.    
  110.     //Method to replace File objects with "empty"
  111.    
  112.     void MakeEmpty (String ElementToReplace) {
  113.        
  114.         for (int i=0; i<file.size(); i++) {
  115.             if (file.get(i) == ElementToReplace) {
  116.                 file.set(i, "empty");
  117.             }
  118.         }
  119.        
  120.     }
  121.  
  122.     //Method to replace File objects
  123.    
  124.     void ReplaceFile (String ElementToReplace, String ElementToReplaceWith) {
  125.        
  126.         for (int i=0; i<file.size(); i++) {
  127.             String Element = file.get(i);
  128.             if (Element.equals(ElementToReplace)) {
  129.                 file.set(i, ElementToReplaceWith);
  130.                 break;
  131.             }
  132.         }
  133.        
  134.     }
  135.  
  136.     void clearList() {
  137.         file.removeAll(file);
  138.     }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement