CodeGym/Java Blog/무작위의/BufferedReader 및 BufferedWriter
John Squirrels
레벨 41
San Francisco

BufferedReader 및 BufferedWriter

무작위의 그룹에 게시되었습니다
회원
BufferedReader 및 BufferedWriter - 1Java의 BufferedReader클래스는 기호 스트림에서 텍스트를 읽고 기호를 버퍼링하여 문자, 배열 및 문자열을 효율적으로 읽습니다. 버퍼 크기를 생성자에 두 번째 인수로 전달할 수 있습니다. BufferedReader 및 BufferedWriter - 2생성자:
BufferedReader(Reader in) // Creates a buffered stream for reading symbols. It uses the default buffer size.
BufferedReader(Reader in, int sz) // Creates a buffered stream for reading symbols. It uses the specified buffer size.
행동 양식:
close() // Close the stream
mark(int readAheadLimit) // Mark the position in the stream
markSupported() // Indicates whether stream marking is supported
int	read() // Read the buffer
int	read(char[] cbuf, int off, int len) // Read the buffer
String	readLine() // Next line
boolean	ready() // Is the stream ready to read?
reset() // Reset the stream
skip(long n) // Skip characters

BufferedReader다음은 및 클래스를 사용하는 예입니다 BufferedWriter.

파일 읽기:
import java.io.*;

public class ReadFile {
	public static void main(String[] args) {
		try {
			File file = new File("file.txt");
			FileReader fileReader = new FileReader(file); // A stream that connects to the text file
			BufferedReader bufferedReader = new BufferedReader(fileReader); // Connect the FileReader to the BufferedReader

			String line;
			while((line = bufferedReader.readLine()) != null) {
				System.out.println(line); // Display the file's contents on the screen, one line at a time
			}

			bufferedReader.close(); // Close the stream
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
Java의 BufferedWriter클래스는 출력 문자 스트림에 텍스트를 쓰고 문자, 배열 및 문자열을 효율적으로 쓰기 위해 문자를 버퍼링합니다. 버퍼 크기를 생성자에 두 번째 인수로 전달할 수 있습니다. 생성자:
BufferedWriter(Writer out) // Create a buffered output character stream that uses the default buffer size.
BufferedWriter(Writer out, int sz) // Creates a buffered character output stream that uses a buffer with the specified size.
행동 양식:
close() // Close the stream
flush() // Send the data from the buffer to the Writer
newLine() // Move to a new line
write(char[] cbuf, int off, int len) // Write to the buffer
write(int c) // Write to the buffer
write(String s, int off, int len) // Write to the buffer

BufferedReader다음은 Java 와 클래스를 사용하는 예입니다 BufferedWriter.

파일에 쓰기:
import java.io.*;

public class WriteFile {
    public static void main(String[] args) {
        String[] list = {"one", "two", "three", "fo"};
        try {
            File file = new File("file.txt");
            FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
            BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the BufferedWriter

            for (String s : list) {
                bufferedWriter.write(s + "\n");
            }

            bufferedWriter.close (); // Close the stream
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
FileWriter즉시 데이터를 디스크에 기록합니다. 액세스할 때마다 버퍼를 감싸서 애플리케이션 속도를 높입니다. 버퍼는 내부적으로 데이터를 쓴 다음 나중에 디스크에 많은 양의 파일을 씁니다. 콘솔에서 데이터를 읽고 파일에 씁니다.
import java.io.*;

class ConsoleRead {
    public static void main(String[] args) {
        try {
            File file = new File("file.txt");
            InputStreamReader inputStreamReader = new InputStreamReader(System.in); // A stream for reading from the console
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader); // Connect InputStreamReader to a BufferedReader

            FileWriter fileReader = new FileWriter(file); // A stream that connects to the text file
            BufferedWriter bufferedWriter = new BufferedWriter(fileReader); // Connect the FileWriter to the BufferedWriter

            String line;
            while(!(line = bufferedReader.readLine()).equals("exit")) {
                bufferedWriter.write(line);
            }

            bufferedReader.close(); // Close the stream
            bufferedWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
코멘트
  • 인기
  • 신규
  • 이전
코멘트를 남기려면 로그인 해야 합니다
이 페이지에는 아직 코멘트가 없습니다