Guest User

Untitled

a guest
Feb 18th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package ru.sincore.tools.asc;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. /**
  7.  * Create user: lh
  8.  * Create date: 10/24/11 / 11:32 AM
  9.  */
  10. public class Main
  11. {
  12.     public static String fromAdcString(String adcString)
  13.     {
  14.         if (adcString == null)
  15.             return null;
  16.  
  17.         return adcString.replaceAll("\\\\s", " ")
  18.                    .replaceAll("\\\\n", "\n")
  19.                    .replaceAll("\\\\\\\\", "\\\\")
  20.                    .replaceAll("\\\\ ", "\\\\s")
  21.                    .replaceAll("\\\\\\n", "\\\\n");
  22.     }
  23.  
  24.     /**
  25.      * @param args the command line arguments (Not used)
  26.      */
  27.     public static void main(String[] args)
  28.     {
  29.         System.out.println("Achtung! Application started!");
  30.  
  31.         if (args.length < 1)
  32.         {
  33.             System.out.println("Usage: asc <input filename> [<output filename>]");
  34.             System.out.println("\t Where <input filename> is a name of file with normal text,");
  35.             System.out.println("\t and <output filename> is a name of output file with adc compatible text.");
  36.             return;
  37.         }
  38.         else
  39.         {
  40.             String outputFileName = args.length == 2 ? args[1] : "adc_" + args[0];
  41.  
  42.             try
  43.             {
  44.                 File file = new File(outputFileName);
  45.                 if (!file.exists())
  46.                 {
  47.                     System.out.println("File doesn't exists. Creating new...");
  48.  
  49.                      if (!file.createNewFile())
  50.                      {
  51.                          System.out.println("Error! File cannot be created.");
  52.                          return;
  53.                      }
  54.                 }
  55.  
  56.                 Scanner scanner = new Scanner(new FileInputStream(args[0]), "UTF-8");
  57.  
  58.                 FileOutputStream fos = new FileOutputStream(outputFileName);
  59.                 OutputStreamWriter out = new OutputStreamWriter(fos);
  60.  
  61.                 int charBuffer;
  62.                 int offset = 0;
  63.  
  64.                 StringBuilder text = new StringBuilder();
  65.  
  66.                 while (scanner.hasNextLine())
  67.                 {
  68.                     text.append(scanner.nextLine());
  69.                 }
  70.  
  71.                 out.write(toAdcString(text.toString()));
  72.  
  73.                 scanner.close();
  74.                 out.close();
  75.             }
  76.             catch (FileNotFoundException e)
  77.             {
  78.                 e.printStackTrace();
  79.             }
  80.             catch (IOException e)
  81.             {
  82.                 e.printStackTrace();
  83.             }
  84.         }
  85.  
  86.     }
  87. }
Add Comment
Please, Sign In to add comment