Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Menu {
- boolean exit;
- public static void main(String[] args) {
- Menu menu = new Menu();
- menu.runMenu();
- }
- public void runMenu() {
- printHeader();
- while(!exit) {
- printMenu();
- int optionsea = getInput();
- performAction(optionsea);
- }
- }
- private void performAction(int optionsea) {
- switch(optionsea) {
- case 0:
- exit = true;
- System.out.println("File (Compressor / Cecompressor) Exited");
- break;
- case 1:
- compressFile();
- break;
- case 2:
- decompressFile();
- break;
- default:
- System.out.println("Unknown error, unlucks, no idea how you got here");
- }
- }
- private int getInput() {
- Scanner seascanner = new Scanner(System.in);
- int optionsea = -1;
- while(optionsea < 0 || optionsea > 2) {
- try {
- System.out.print("Enter value: ");
- optionsea = Integer.parseInt(seascanner.nextLine());
- }
- catch(NumberFormatException e) {
- System.out.println("Invalid selection");
- }
- }
- return optionsea;
- }
- private void printHeader() {
- System.out.println("File (Compressor / Cecompressor) Launched");
- System.out.println("Uncompressed file -> Compressed file (uncompressed.txt)");
- System.out.println("Compressed file -> Uncompressed file (compressed.txt) \n");
- }
- private void printMenu() {
- System.out.println("Please make a selection");
- System.out.println("0 - Exit menu");
- System.out.println("1 - Compress file");
- System.out.println("2 - Uncompress file \n");
- }
- private static final String seafile = "C:\\Users\\mpspo\\Documents\\NetBeansProjects\\newFile\\uncompressed.txt";
- public void compressFile() {
- BufferedReader seaReader = null;
- FileReader seaFileReader = null;
- String seaCurrentLine = null;
- try {
- seaFileReader = new FileReader(seafile);
- seaReader = new BufferedReader(seaFileReader);
- seaReader = new BufferedReader(new FileReader(seafile));
- while ((seaCurrentLine = seaReader.readLine()) != null) {
- System.out.println("\nFile read: uncompressed.txt");
- String sea1 = seaCurrentLine;
- String sea2 = sea1.toLowerCase();
- String sea3 = sea2.replaceAll("[!.,:;?]", " "); //remove punctuation + sets to lower case
- String sea4 = sea3.replaceAll(" ", " "); //closes gaps
- String sea5 = sea4.replaceAll(" ", ","); //allows it to be converted into array
- String sea6 = sea5.replaceAll("[']", " "); //remove appostrophies (different to all other grammer
- String sea7 = sea6.replaceAll(" ", ""); //closes appostrophie gap and any other error gap
- System.out.println("\nOriginal: " + sea7); // prints the string and seperates from line below
- String[] sealist = sea7.split(","); //splits string into arraylist (so it can be looped)
- Map<String, Set<Integer>> seaMap = new HashMap<>(); //new hashmap
- for (int seaInt = 0; seaInt < sealist.length; seaInt++) {
- if (seaMap.keySet().contains(sealist[seaInt])) {
- Set<Integer> index = seaMap.get(sealist[seaInt]);
- index.add(seaInt);
- } else {
- Set<Integer> index = new HashSet<>();
- index.add(seaInt);
- seaMap.put(sealist[seaInt], index);
- }
- }
- System.out.print("Compressed: ");
- seaMap.forEach((seawords, seavalues) -> System.out.print(seawords + seavalues + ",")); //prints words and indexes of words to console
- System.out.println("\n");
- try{
- PrintWriter writer = new PrintWriter("compressed.txt", "UTF-8"); //setup of printwriter & file name & text method
- seaMap.forEach((seawords, seavalues) -> writer.print(seawords + seavalues + ",")); // foreach loop, writer prints words and indexes
- writer.close(); //writer closed
- }
- catch (IOException e) { //catch failures in writer
- System.err.print("Writer Error"); //prints error message
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (seaReader != null)
- try {
- seaReader.close();
- } catch (IOException ex) {
- Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
- }
- if (seaFileReader != null)
- try {
- seaFileReader.close();
- } catch (IOException ex) {
- Logger.getLogger(Menu.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public void decompressFile() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement