CodeGym /จาวาบล็อก /สุ่ม /BufferedReader และ BufferedWriter
John Squirrels
ระดับ
San Francisco

BufferedReader และ BufferedWriter

เผยแพร่ในกลุ่ม
BufferedReader และ BufferedWriter - 1คลาส ของ Java 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

นี่คือตัวอย่างการใช้BufferedReaderand 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

นี่คือตัวอย่างการใช้ Java BufferedReaderและ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();
        }
    }
}
ความคิดเห็น
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION