Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mycompany.rubrica;
- import java.util.Scanner;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.BufferedReader;
- import java.io.IOException;
- /**
- *
- * @author alessio
- */
- public class FileManage
- {
- private static String path = "rubrica.txt";
- public static void stampaSuFile(String strObj) throws IOException
- {
- FileWriter writer = new FileWriter(path, true);
- try
- {
- writer.write(strObj + "\n\n");
- }
- catch (FileNotFoundException exc)
- {
- System.out.println("\nFile non trovato.");
- }
- finally
- {
- writer.close();
- }
- }
- public static boolean cercaStringa(String stringa) throws IOException
- {
- File file = new File(path);
- Scanner reader = new Scanner(file);
- while (reader.hasNext())
- {
- if ((reader.next().equals(stringa)) || (reader.next().contains(stringa)))
- {
- return true;
- }
- }
- return false;
- }
- public static boolean cancellaRighe(String stringa) throws IOException
- {
- File file = new File(path);
- Scanner reader = new Scanner(file);
- FileWriter writer = new FileWriter(file);
- HashMap<Integer, String> righe = new HashMap();
- int i = 0;
- while (reader.hasNext())
- {
- righe.put(i, reader.nextLine());
- i++;
- if (stringa.equals(reader.nextLine))
- {
- righe.remove(i);
- }
- }
- for (String riga : righe)
- {
- writer.write()
- }
- return false;
- }
- public static int svuotaFile() throws IOException
- {
- File file = new File(path);
- FileWriter writer = new FileWriter(file);
- writer.write("");
- return 0;
- }
- public static void leggiFile() throws IOException
- {
- File file = new File(path);
- Scanner reader = new Scanner(file);
- while (reader.hasNext())
- {
- System.out.print(reader.nextLine() + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment