# Работа с текстовыми файлами

Java использует двухбайтовые символы для хранения букв, а классы FileReader и FileWriter предназначены для удобной работы с текстовыми файлами. Эти классы могут считывать файлы посимвольно, используя метод read(), или же построчно, с помощью метода readLine(). У классов FileReader и FileWriter также есть аналоги, BufferedReader и BufferedWriter, позволяющие ускорить работу с файлами.

Пример:

```
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;

public class ScoreReader {

    public static void main(String[] args) {
        FileReader myFile = null;
        BufferedReader buff = null;
        try {
            myFile=new FileReader("c:\\scores.txt");
            buff = new BufferedReader(myFile);
            while (true) {
                // считывается строка из файла scores.txt
                String line = buff.readLine();

                // проверка достижения конца файла
                if (line == null) break;
                System.out.println(line);
            } // конец цикла while
        }
        catch (IOException e){
            e.printStackTrace();
        }
        finally {
            try{
                buff.close();
                myFile.close();
            }
            catch(IOException e1){
                e1.printStackTrace();
            }
        }
    } // конец метода main
}
```

Вопрос 1.

В рассмотренном примере проанализируйте содержимое блока finally. Для чего он используется в данном случае

Вопрос 2.

Каким образом в Java производится запись данных в файл?

Вопрос 3.

В чем разница между методами append() и write() класса FileWriter


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://comaqa.gitbook.io/java-automation/rabota-s-failami/rabota-s-tekstovymi-failami.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
