Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. public static void main(String[] args) throws FileNotFoundException, IOException {
  2. String file, FileR,line,result1;
  3. Scanner in = new Scanner(System.in);
  4. System.out.println("Введите имя файла, из которого считывать строки:");
  5. file = in.nextLine();
  6. Scanner out = new Scanner(System.in);
  7. System.out.println("Куда сохранить результат?");
  8. FileR = out.nextLine();
  9. //запрашиваем кодировку
  10. Scanner inEncoding = new Scanner(System.in, "UTF-8");
  11. System.out.println("Введите название кодировки входного файла:");
  12. String encodingStr = inEncoding.nextLine();
  13. System.out.println("Введите название кодировки выходного файла:");
  14. String encodingStrout = inEncoding.nextLine();
  15. //cчитывание из файла
  16. InputStream inFile = new FileInputStream(file);
  17. byte[] str = new byte[inFile.available()];
  18. inFile.read(str);
  19. if (encodingStr.compareToIgnoreCase("windows-1251") == 0) {
  20. line = new String(str, "windows-1251");
  21. } else if (encodingStr.compareToIgnoreCase("Unicode") == 0) {
  22. line = new String(str, "UTF-16");
  23. } else if (encodingStr.compareToIgnoreCase("UTF-8") == 0) {
  24. line = new String(str, "UTF-8");
  25. } else {
  26. return;
  27. }
  28. //условия задания
  29. Pattern p = Pattern.compile("[\p{L}]*",Pattern.UNICODE_CHARACTER_CLASS);
  30. boolean matches=p.matcher(line).matches();
  31. if(matches)
  32. {
  33. result1="Текст не содержит символы, отличные от букв и пробела";
  34. }
  35. else{
  36. result1="Текст содержит символы, отличные от букв и пробела";
  37. }
  38. OutputStream outFile = new FileOutputStream(FileR);
  39. if (encodingStrout.compareToIgnoreCase("windows-1251") == 0) {
  40. byte[] result = result1.getBytes("windows-1251");
  41. outFile.write(result);
  42. } else if (encodingStrout.compareToIgnoreCase("Unicode") == 0) {
  43. byte[] result = result1.getBytes("UTF-16");
  44. outFile.write(result);
  45. } else if (encodingStrout.compareToIgnoreCase("UTF-8") == 0) {
  46. byte[] result = result1.getBytes("UTF-8");
  47. outFile.write(result);
  48. }}}
  49.  
  50. import java.io.FileInputStream;
  51. import java.io.FileOutputStream;
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54. import java.io.OutputStream;
  55. import java.util.Scanner;
  56. import java.util.regex.Pattern;
  57.  
  58. public class So_600623_FileEcodings {
  59.  
  60. static String fixEncodingName(String encName) {
  61. switch (encName.toLowerCase()) {
  62. case "windows-1251": return "windows-1251";
  63. case "utf-16": return "UTF-16";
  64. case "utf-8": return "UTF-8";
  65. default:
  66. System.out.println("Недопустимая кодировка: " + encName);
  67. System.exit(1);
  68. return null; // Compiler beleives that one day
  69. // System.exit() may return control :)
  70. }
  71. }
  72.  
  73. public static void main(String[] args) throws IOException {
  74. InputStream inpStream = null;
  75. OutputStream outStream = null;
  76. Scanner in = null;
  77. try {
  78. in = new Scanner(System.in);
  79. String inpFileName, outFileName, dataStr, resultStr;
  80.  
  81. System.out.println("Введите имя файла, из которого считывать строки:");
  82. inpFileName = in.nextLine();
  83. System.out.println("Куда сохранить результат?");
  84. outFileName = in.nextLine();
  85.  
  86. //запрашиваем кодировку
  87. System.out.println("Введите название кодировки входного файла:");
  88. String inpEncName = fixEncodingName(in.nextLine());
  89. System.out.println("Введите название кодировки выходного файла:");
  90. String outEncName = fixEncodingName(in.nextLine());
  91.  
  92. //cчитывание из файла
  93. inpStream = new FileInputStream(inpFileName);
  94. byte[] bytes = new byte[inpStream.available()];
  95. inpStream.read(bytes);
  96. dataStr = new String( bytes, inpEncName);
  97.  
  98. // It may contain BOM. Q&D workaround suitable for UTF-8 and UTF-16
  99. if ((bytes[0] & 0xEE) == 0xEE) dataStr = dataStr.substring(1);
  100.  
  101. //условия задания
  102. // Pattern p = Pattern.compile("[\p{L}]*",Pattern.UNICODE_CHARACTER_CLASS); // А пробелы где?
  103. Pattern p = Pattern.compile("[\p{L}\s]*",Pattern.UNICODE_CHARACTER_CLASS);
  104. resultStr = p.matcher(dataStr).matches()?
  105. "Текст не содержит символов, отличных от букв и пробела":
  106. "Текст содержит символы, отличные от букв и пробела";
  107.  
  108. outStream = new FileOutputStream(outFileName);
  109. byte[] result = resultStr.getBytes(outEncName);
  110. outStream.write(result);
  111.  
  112. } catch (IOException x) {
  113. System.out.println("Ошибка ввода-вывода:");
  114. x.printStackTrace();
  115. }
  116. finally {
  117. // Открытые файлы надо закрывать
  118. if (outStream != null)
  119. try { outStream.close(); }
  120. catch (Exception x) {
  121. System.out.println("Ошибка закрытия выходного файла:") ;
  122. x.printStackTrace();
  123. }
  124. if (inpStream != null) try { inpStream.close(); } catch (Exception x) {}
  125. if (in != null) try { in.close(); } catch (Exception x) {} // Сканер тоже
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement