Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) throws FileNotFoundException, IOException {
- String file, FileR,line,result1;
- Scanner in = new Scanner(System.in);
- System.out.println("Введите имя файла, из которого считывать строки:");
- file = in.nextLine();
- Scanner out = new Scanner(System.in);
- System.out.println("Куда сохранить результат?");
- FileR = out.nextLine();
- //запрашиваем кодировку
- Scanner inEncoding = new Scanner(System.in, "UTF-8");
- System.out.println("Введите название кодировки входного файла:");
- String encodingStr = inEncoding.nextLine();
- System.out.println("Введите название кодировки выходного файла:");
- String encodingStrout = inEncoding.nextLine();
- //cчитывание из файла
- InputStream inFile = new FileInputStream(file);
- byte[] str = new byte[inFile.available()];
- inFile.read(str);
- if (encodingStr.compareToIgnoreCase("windows-1251") == 0) {
- line = new String(str, "windows-1251");
- } else if (encodingStr.compareToIgnoreCase("Unicode") == 0) {
- line = new String(str, "UTF-16");
- } else if (encodingStr.compareToIgnoreCase("UTF-8") == 0) {
- line = new String(str, "UTF-8");
- } else {
- return;
- }
- //условия задания
- Pattern p = Pattern.compile("[\p{L}]*",Pattern.UNICODE_CHARACTER_CLASS);
- boolean matches=p.matcher(line).matches();
- if(matches)
- {
- result1="Текст не содержит символы, отличные от букв и пробела";
- }
- else{
- result1="Текст содержит символы, отличные от букв и пробела";
- }
- OutputStream outFile = new FileOutputStream(FileR);
- if (encodingStrout.compareToIgnoreCase("windows-1251") == 0) {
- byte[] result = result1.getBytes("windows-1251");
- outFile.write(result);
- } else if (encodingStrout.compareToIgnoreCase("Unicode") == 0) {
- byte[] result = result1.getBytes("UTF-16");
- outFile.write(result);
- } else if (encodingStrout.compareToIgnoreCase("UTF-8") == 0) {
- byte[] result = result1.getBytes("UTF-8");
- outFile.write(result);
- }}}
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Scanner;
- import java.util.regex.Pattern;
- public class So_600623_FileEcodings {
- static String fixEncodingName(String encName) {
- switch (encName.toLowerCase()) {
- case "windows-1251": return "windows-1251";
- case "utf-16": return "UTF-16";
- case "utf-8": return "UTF-8";
- default:
- System.out.println("Недопустимая кодировка: " + encName);
- System.exit(1);
- return null; // Compiler beleives that one day
- // System.exit() may return control :)
- }
- }
- public static void main(String[] args) throws IOException {
- InputStream inpStream = null;
- OutputStream outStream = null;
- Scanner in = null;
- try {
- in = new Scanner(System.in);
- String inpFileName, outFileName, dataStr, resultStr;
- System.out.println("Введите имя файла, из которого считывать строки:");
- inpFileName = in.nextLine();
- System.out.println("Куда сохранить результат?");
- outFileName = in.nextLine();
- //запрашиваем кодировку
- System.out.println("Введите название кодировки входного файла:");
- String inpEncName = fixEncodingName(in.nextLine());
- System.out.println("Введите название кодировки выходного файла:");
- String outEncName = fixEncodingName(in.nextLine());
- //cчитывание из файла
- inpStream = new FileInputStream(inpFileName);
- byte[] bytes = new byte[inpStream.available()];
- inpStream.read(bytes);
- dataStr = new String( bytes, inpEncName);
- // It may contain BOM. Q&D workaround suitable for UTF-8 and UTF-16
- if ((bytes[0] & 0xEE) == 0xEE) dataStr = dataStr.substring(1);
- //условия задания
- // Pattern p = Pattern.compile("[\p{L}]*",Pattern.UNICODE_CHARACTER_CLASS); // А пробелы где?
- Pattern p = Pattern.compile("[\p{L}\s]*",Pattern.UNICODE_CHARACTER_CLASS);
- resultStr = p.matcher(dataStr).matches()?
- "Текст не содержит символов, отличных от букв и пробела":
- "Текст содержит символы, отличные от букв и пробела";
- outStream = new FileOutputStream(outFileName);
- byte[] result = resultStr.getBytes(outEncName);
- outStream.write(result);
- } catch (IOException x) {
- System.out.println("Ошибка ввода-вывода:");
- x.printStackTrace();
- }
- finally {
- // Открытые файлы надо закрывать
- if (outStream != null)
- try { outStream.close(); }
- catch (Exception x) {
- System.out.println("Ошибка закрытия выходного файла:") ;
- x.printStackTrace();
- }
- if (inpStream != null) try { inpStream.close(); } catch (Exception x) {}
- if (in != null) try { in.close(); } catch (Exception x) {} // Сканер тоже
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement