Advertisement
Guest User

Untitled

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