Java์
ํ์ผ ์ฝ๊ธฐ:
ํ์ผ์ ์ฐ๊ธฐ:
BufferedReader
ํด๋์ค๋ ๊ธฐํธ ์คํธ๋ฆผ์์ ํ
์คํธ๋ฅผ ์ฝ๊ณ ๊ธฐํธ๋ฅผ ๋ฒํผ๋งํ์ฌ ๋ฌธ์, ๋ฐฐ์ด ๋ฐ ๋ฌธ์์ด์ ํจ์จ์ ์ผ๋ก ์ฝ์ต๋๋ค. ๋ฒํผ ํฌ๊ธฐ๋ฅผ ์์ฑ์์ ๋ ๋ฒ์งธ ์ธ์๋ก ์ ๋ฌํ ์ ์์ต๋๋ค. ์์ฑ์:
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();
}
}
}
๋ ์ฝ์ด๋ณด๊ธฐ: |
---|
GO TO FULL VERSION