blooming8

FileManage.java [in progress]

Mar 30th, 2022 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. package com.mycompany.rubrica;
  2. import java.util.Scanner;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. /**
  10.  *
  11.  * @author alessio
  12.  */
  13. public class FileManage
  14. {
  15.     private static String path = "rubrica.txt";
  16.      
  17.     public static void stampaSuFile(String strObj) throws IOException
  18.     {
  19.         FileWriter writer = new FileWriter(path, true);
  20.  
  21.         try
  22.         {  
  23.             writer.write(strObj + "\n\n");      
  24.         }
  25.         catch (FileNotFoundException exc)
  26.         {
  27.             System.out.println("\nFile non trovato.");
  28.         }
  29.         finally
  30.         {
  31.             writer.close();
  32.         }
  33.     }
  34.    
  35.     public static boolean cercaStringa(String stringa) throws IOException
  36.     {
  37.         File file = new File(path);
  38.         Scanner reader = new Scanner(file);  
  39.        
  40.         while (reader.hasNext())
  41.         {
  42.             if ((reader.next().equals(stringa)) || (reader.next().contains(stringa)))
  43.             {
  44.                 return true;
  45.             }  
  46.         }    
  47.        
  48.         return false;
  49.     }
  50.    
  51.     public static boolean cancellaRighe(String stringa) throws IOException
  52.     {
  53.         File file = new File(path);
  54.         Scanner reader = new Scanner(file);
  55.         FileWriter writer = new FileWriter(file);
  56.         HashMap<Integer, String> righe = new HashMap();
  57.         int i = 0;
  58.        
  59.         while (reader.hasNext())
  60.         {
  61.             righe.put(i, reader.nextLine());
  62.             i++;
  63.             if (stringa.equals(reader.nextLine))
  64.             {
  65.                 righe.remove(i);   
  66.             }
  67.         }
  68.        
  69.         for (String riga : righe)
  70.         {
  71.             writer.write()
  72.         }
  73.        
  74.         return false;
  75.        
  76.     }
  77.    
  78.     public static int svuotaFile() throws IOException
  79.     {
  80.         File file = new File(path);
  81.         FileWriter writer = new FileWriter(file);
  82.         writer.write("");
  83.        
  84.         return 0;
  85.     }
  86.    
  87.     public static void leggiFile() throws IOException
  88.     {
  89.         File file = new File(path);
  90.         Scanner reader = new Scanner(file);    
  91.        
  92.         while (reader.hasNext())
  93.         {
  94.             System.out.print(reader.nextLine() + "\n");
  95.         }
  96.     }
  97. }
  98.  
  99.    
  100.  
Advertisement
Add Comment
Please, Sign In to add comment