0

Как можно преобразовать кодировку файла из win1251 в ISO-8859-5?

Использовал iconv, mb_convert_encoding, но, возможно, не так как надо.

Прошу помощи у тех, кто решил такую проблему.

1 ответ 1

2

Код универсального перекодировщика.

/**
* @param infile Input file name, if null console input presumed
* @param outfile Output file name, if null console output presumed
* @param from input file code page name (like "UTF-8", "KOI-8", etc), may be null
* @param to output file code page name (like "UTF-8", "KOI-8", etc), may be null
*/
public static void convert(String infile, String outfile, String from, String to)
        throws IOException, UnsupportedEncodingException
{
    // set up byte streams
    InputStream in;
    if(infile != null)
        in=new FileInputStream(infile);
    else
        in=System.in;
    OutputStream out;
    if(outfile != null)
        out=new FileOutputStream(outfile);
    else
        out=System.out;

    // Use default encoding if no encoding is specified.
    if(from == null) from=System.getProperty("file.encoding");
    if(to == null) to=System.getProperty("file.encoding");

    // Set up character stream
    Reader r=new BufferedReader(new InputStreamReader(in, from));
    Writer w=new BufferedWriter(new OutputStreamWriter(out, to));

    // Copy characters from input to output.  The InputStreamReader
    // converts from the input encoding to Unicode,, and the OutputStreamWriter
    // converts from Unicode to the output encoding.  Characters that cannot be
    // represented in the output encoding are output as '?'
    char[] buffer=new char[4096];
    int len;
    while((len=r.read(buffer)) != -1)
        w.write(buffer, 0, len);
    r.close();
    w.flush();
    w.close();
}
3
  • Спасибо за функцию! Она мне пригодиться! Но мне нужно именно в php! Т.к я пишу exe-шник на php! через develstudio! Может Кто нибудь объяснит как с mb_convert_encoding работать?
    – chambo
    30 июл 2013 в 6:36
  • exe через php? я думаю, что Вы не совсем правильно выбрали окружение. Но если сильно хочется... есть такая функция - iconv. @Barmaley какой смысл давать универсальную функцию на неведомом языке? при чем автору вопроса она никак не поможет.
    – KoVadim
    30 июл 2013 в 6:45
  • @KoVadim Упс, как-то не заметил тег PHP - извиняюсь...
    – Barmaley
    30 июл 2013 в 6:53

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.