1

Подскажите, пожалуйтса, где ошибка? Последяя строка(и) текстового файла читается неполностью:

public void readFile(String filename) {
    File f=new File(filename);
    StringBuilder sb = new StringBuilder();
    BufferedInputStream bufferedInput = null;
    int bytesRead = 0;
    byte[] buffer = new byte[1024];
    try {
        bufferedInput = new BufferedInputStream(new FileInputStream(f));
        while ((bytesRead = bufferedInput.read(buffer)) != -1) {
            sb.append(new String(buffer,"windows-1251"));
        }
    }
    catch(FileNotFoundException e) {
        System.out.println("File not found" + e);
    }
    catch(IOException ioe) {
        System.out.println("Exception while reading the file " + ioe);
    }
    finally {
        try{
            if(bufferedInput != null)
                bufferedInput.close();
            }
        catch(IOException ioe){
            System.out.println("Error while closing the stream : " + ioe);
        }
    }
}

1 ответ 1

2

Попробуйте так:

sb.append(new String(buffer, 0, bytesRead, "windows-1251"));

Да, и в данном контексте StringBuffer лучше заменить на StringBuilder

2
  • Спасибо! Так всё ОК! Не понял на счёт StringBuffer - вроде его не использую.
    – Sergey_New
    20 авг 2013 в 8:43
  • @Sergey_New забей... И пометь ответ как принятый (галочка с левой стороны от ответа)
    – Barmaley
    20 авг 2013 в 10:49

Ваш ответ

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

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