diff --git a/LP/src/LukePack/GestioneFile.java b/LP/src/LukePack/GestioneFile.java new file mode 100644 index 0000000..f1dc2b9 --- /dev/null +++ b/LP/src/LukePack/GestioneFile.java @@ -0,0 +1,325 @@ +package LukePack; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.util.List; + +public class GestioneFile { + + protected static String readFile_Implementazione(String nomeFile){ + + String contenuto = ""; + + String s; + + try{ + + BufferedReader reader = new BufferedReader(new FileReader(nomeFile) ); + + if( (s = reader.readLine()) != null ) contenuto += s; + + while( (s = reader.readLine()) != null ){ + + contenuto += "\r\n"; + + contenuto += s; + + } + + reader.close(); + + } catch(Exception error1){ + + return null; + + } + + return contenuto; + + } + + protected static boolean writeNewFile_Implementazione(String nomeFile, String riga, boolean aCapo){ + + try{ + + FileOutputStream file = new FileOutputStream(nomeFile); + + PrintStream Output = new PrintStream(file); + + if(aCapo) Output.println(riga); + + else Output.print(riga); + + Output.close(); + + return true; + + } catch(Exception error1){ + + return false; + + } + + } + + protected static boolean writeNewFile_Implementazione(String nomeFile, List contenuto){ + + try{ + + FileOutputStream file = new FileOutputStream(nomeFile); + + PrintStream Output = new PrintStream(file); + + for(String riga: contenuto) Output.println(riga); + + Output.close(); + + return true; + + } catch(Exception error1){ + + return false; + + } + + } + + protected static boolean addToFile_Implementazione(String nomeFile, String riga, boolean aCapo){ + + try { + + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(nomeFile, true))); + + if(aCapo) out.println(riga); + + else out.print(riga); + + out.close(); + + return true; + + } catch (Exception error1) { + + return false; + + } + + } + + protected static boolean addToFile_Implementazione(String nomeFile, List contenuto){ + + try { + + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(nomeFile, true))); + + for(String riga:contenuto) out.println(riga); + + out.close(); + + return true; + + } catch (Exception error1) { + + return false; + + } + + } + + protected static boolean emptyFile_Implementazione(String nomeFile){ + + try{ + + FileOutputStream file = new FileOutputStream(nomeFile); + + PrintStream Output = new PrintStream(file); + + Output.print(""); + + Output.close(); + + return true; + + } catch(Exception error1){ + + return false; + + } + + } + + protected static boolean deleteFile_Implementazione(String nomeFile){ + + File f = new File(nomeFile); + + if(f.delete()){ + + return true; + + } else { + + return false; + + } + + } + + //-----------------------Throws Exception------------------------------------ + + protected static String readFileTE_Implementazione(String nomeFile) throws Exception{ + + String contenuto = ""; + + String s; + + try{ + + BufferedReader reader = new BufferedReader(new FileReader(nomeFile) ); + + if( (s = reader.readLine()) != null ) contenuto += s; + + while( (s = reader.readLine()) != null ){ + + contenuto += "\r\n"; + + contenuto += s; + + } + + reader.close(); + + } catch(FileNotFoundException error1){ + + throw new FileNotFoundException(" Impossible to open this file: "+ nomeFile +" "); + + } catch(IOException error2){ + + throw new IOException(" Impossible to open this file: "+ nomeFile +" "); + + } + + return contenuto; + + } + + protected static void writeNewFileTE_Implementazione(String nomeFile, String riga, boolean aCapo) throws IOException{ + + try{ + + FileOutputStream file = new FileOutputStream(nomeFile); + + PrintStream Output = new PrintStream(file); + + if(aCapo) Output.println(riga); + + else Output.print(riga); + + Output.close(); + + } catch(IOException error){ + + throw new IOException(" Unable to create this file " + nomeFile + " "); + + } + + } + + protected static void writeNewFileTE_Implementazione(String nomeFile, List contenuto) throws IOException{ + + try{ + + FileOutputStream file = new FileOutputStream(nomeFile); + + PrintStream Output = new PrintStream(file); + + for(String riga:contenuto) Output.println(riga); + + Output.close(); + + } catch(IOException error){ + + throw new IOException(" Unable to create this file " + nomeFile + " "); + + } + + } + + protected static void addToFileTE_Implementazione(String nomeFile, String riga, boolean aCapo) throws IOException{ + + try { + + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(nomeFile, true))); + + if(aCapo) out.println(riga); + + else out.print(riga); + + out.close(); + + } catch (IOException error) { + + throw new IOException(" Unable to modify this file " + nomeFile + " "); + + } + + } + + protected static void addToFileTE_Implementazione(String nomeFile, List contenuto) throws IOException{ + + try { + + PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(nomeFile, true))); + + for(String riga:contenuto) out.println(riga); + + out.close(); + + } catch (IOException error) { + + throw new IOException(" Unable to modify this file " + nomeFile + " "); + + } + + } + + protected static void emptyFileTE_Implementazione(String nomeFile) throws IOException{ + + try{ + + FileOutputStream file = new FileOutputStream(nomeFile); + + PrintStream Output = new PrintStream(file); + + Output.print(""); + + Output.close(); + + } catch(IOException error){ + + throw new IOException(" Unable to modify this file " + nomeFile + " "); + + } + + } + + protected static void deleteFileTE_Implementazione(String nomeFile) throws IOException{ + + File f = new File(nomeFile); + + if(!f.delete()){ + + throw new IOException(" Unable to delete this file " + nomeFile + " "); + + } + + } + +} diff --git a/LP/src/LukePack/GestionePause.java b/LP/src/LukePack/GestionePause.java new file mode 100644 index 0000000..d69e058 --- /dev/null +++ b/LP/src/LukePack/GestionePause.java @@ -0,0 +1,23 @@ +package LukePack; + +import java.util.Scanner; + +public class GestionePause { + + private static Scanner sc; + + protected static void pause_Interno(){ + sc = new Scanner(System.in); + System.out.print(" Press enter to continue. "); + sc.nextLine(); + } + + public static void waitFor_Interno(long milliseconds) { + try { + Thread.sleep(milliseconds); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + +} diff --git a/LP/src/LukePack/InputTastiera.java b/LP/src/LukePack/InputTastiera.java new file mode 100644 index 0000000..d1f0053 --- /dev/null +++ b/LP/src/LukePack/InputTastiera.java @@ -0,0 +1,302 @@ +package LukePack; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +public class InputTastiera{ // LukePack + + protected static byte inByte_Interno(){ + + InputStreamReader lettore; + lettore = new InputStreamReader(System.in); + BufferedReader myInput; + myInput = new BufferedReader(lettore); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch(IOException e) { + + System.out.println (" IOException: " + e); + + } + + try { + + return Byte.parseByte(variabile); + + } catch (NumberFormatException e) { + + System.out.print(" NumberFormatException. Required BYTE. Try again: "); + + return InputTastiera.inByte_Interno(); + + } + + } + + + protected static short inShort_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + try { + + return Short.parseShort(variabile); + + } catch (NumberFormatException e) { + + System.out.print(" NumberFormatException. Required SHORT. Try again: "); + + return InputTastiera.inShort_Interno(); + + } + + } + + protected static int inInt_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + try { + + return Integer.parseInt(variabile); + + } catch (NumberFormatException e) { + + System.out.print(" NumberFormatException. Required INT. Try again: "); + + return InputTastiera.inInt_Interno(); + + } + + } + + protected static long inLong_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + try { + + return Long.parseLong(variabile); + + } catch (NumberFormatException e) { + + System.out.print(" NumberFormatException. Required LONG. Try again: "); + + return InputTastiera.inLong_Interno(); + + } + + } + + protected static float inFloat_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + try { + + return Float.parseFloat(variabile); + + } catch (NumberFormatException e) { + + System.out.print(" NumberFormatException. Required FLOAT. Try again: "); + + return InputTastiera.inFloat_Interno(); + + } + + } + + protected static double inDouble_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + try { + + return new Double(variabile).doubleValue(); + + } catch (NumberFormatException e) { + + System.out.print(" NumberFormatException. Required DOUBLE. Try again: "); + + return InputTastiera.inDouble_Interno(); + + } + + + } + + protected static boolean inBoolean_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + if (variabile.equals("1")) return true; + + if(variabile.length()==0){ + + System.out.print(" NumberFormatException. Required BOOLEAN. Try again: "); + + return InputTastiera.inBoolean_Interno(); + + } + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + try { + + return new Boolean(variabile).booleanValue(); + + } catch (NumberFormatException e) { + + System.out.print(" NumberFormatException. Required BOOLEAN. Try again: "); + + return InputTastiera.inBoolean_Interno(); + + } + + } + + protected static char inChar_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + if(variabile.equals("")){ + + System.out.print(" EmptyCharException. Required CHAR. Try again: "); + + return InputTastiera.inChar_Interno(); + + } + + if(variabile.length()>1){ + + System.out.print(" EmptyCharException. Required CHAR. Try again: "); + + return InputTastiera.inChar_Interno(); + + } + + return variabile.charAt(0); + + } + + protected static String inString_Interno(){ + + InputStreamReader reader = new InputStreamReader (System.in); + BufferedReader myInput = new BufferedReader (reader); + + String variabile = ""; + + try { + + variabile = myInput.readLine(); + + } catch (IOException e) { + + System.out.println (" IOException: " + e); + + } + + return variabile; + + } + + + + + +} diff --git a/LP/src/LukePack/LP.java b/LP/src/LukePack/LP.java new file mode 100644 index 0000000..2a6ae54 --- /dev/null +++ b/LP/src/LukePack/LP.java @@ -0,0 +1,205 @@ +package LukePack; + +import java.io.IOException; +import java.util.List; + +public class LP { // Adapter + + // OutputSchermo + + public static void out(String stringa){ + + System.out.print(stringa); + + } + + public static void outln(String stringa){ + + System.out.println(stringa); + + } + + // InputTastiera + + public static byte inByte(){ + + return InputTastiera.inByte_Interno(); + + } + + + public static short inShort(){ + + return InputTastiera.inShort_Interno(); + + } + + public static int inInt(){ + + return InputTastiera.inInt_Interno(); + + } + + public static long inLong(){ + + return InputTastiera.inLong_Interno(); + + } + + public static float inFloat(){ + + return InputTastiera.inFloat_Interno(); + + } + + public static double inDouble(){ + + return InputTastiera.inDouble_Interno(); + + } + + public static boolean inBoolean(){ + + return InputTastiera.inBoolean_Interno(); + + } + + public static char inChar(){ + + return InputTastiera.inChar_Interno(); + + } + + public static String inString(){ + + return InputTastiera.inString_Interno(); + + } + + // Gestione File + + public static String readFile(String nomeFile){ + + return GestioneFile.readFile_Implementazione(nomeFile); + + } + + public static boolean writeNewFile(String nomeFile, String riga){ + + return GestioneFile.writeNewFile_Implementazione(nomeFile, riga, false); + + } + + public static boolean writeNewFileln(String nomeFile, String riga){ + + return GestioneFile.writeNewFile_Implementazione(nomeFile, riga, true); + + } + + public static boolean writeNewFile(String nomeFile, List contenuto){ + + return GestioneFile.writeNewFile_Implementazione(nomeFile, contenuto); + + } + + public static boolean addToFile(String nomeFile, String riga){ + + return GestioneFile.addToFile_Implementazione(nomeFile, riga, false); + + } + + public static boolean addToFileln(String nomeFile, String riga){ + + return GestioneFile.addToFile_Implementazione(nomeFile, riga, true); + + } + + public static boolean addToFile(String nomeFile, List contenuto){ + + return GestioneFile.addToFile_Implementazione(nomeFile, contenuto); + + } + + public static boolean emptyFile(String nomeFile){ + + return GestioneFile.emptyFile_Implementazione(nomeFile); + + } + + public static boolean deleteFile(String nomeFile){ + + return GestioneFile.deleteFile_Implementazione(nomeFile); + + } + + //-----------------------Throws Exception------------------------------------ + + public static String readFileTE(String nomeFile) throws Exception{ + + return GestioneFile.readFileTE_Implementazione(nomeFile); + + } + + public static void writeNewFileTE(String nomeFile, String riga) throws IOException{ + + GestioneFile.writeNewFileTE_Implementazione(nomeFile, riga, false); + + } + + public static void writeNewFilelnTE(String nomeFile, String riga) throws IOException{ + + GestioneFile.writeNewFileTE_Implementazione(nomeFile, riga, true); + + } + + public static void writeNewFileTE(String nomeFile, List contenuto) throws IOException{ + + GestioneFile.writeNewFileTE_Implementazione(nomeFile, contenuto); + + } + + public static void addToFileTE(String nomeFile, String riga) throws IOException{ + + GestioneFile.addToFileTE_Implementazione(nomeFile, riga, false); + + } + + public static void addToFilelnTE(String nomeFile, String riga) throws IOException{ + + GestioneFile.addToFileTE_Implementazione(nomeFile, riga, true); + + } + + public static void addToFileTE(String nomeFile, List contenuto) throws IOException{ + + GestioneFile.addToFileTE_Implementazione(nomeFile, contenuto); + + } + + public static void emptyFileTE(String nomeFile) throws IOException{ + + GestioneFile.emptyFileTE_Implementazione(nomeFile); + + } + + public static void deleteFileTE(String nomeFile) throws IOException{ + + GestioneFile.deleteFileTE_Implementazione(nomeFile); + + } + + // Gestione Pause + + public static void pause(){ + + GestionePause.pause_Interno(); + + } + + public static void waitFor(long milliseconds){ + + GestionePause.waitFor_Interno(milliseconds); + + } + +}