← Back to feed
Renewal·서른의 생활코딩

ex10) java.io.*

NS
normalstory
cover image

/*
import java.io.*;
 
public class BufferedReaderTest1 {
public static void main( String[] args ) {
InputStream is = System.in;      // byte
InputStreamReader isr = new InputStreamReader( is );   // byte
BufferedReader br = new BufferedReader( isr );   // -> "char"
System.out.print( "Input Data : " );
try {
String inputString = br.readLine(); // read line by line (when the Enter key creates a line separator)
System.out.println();
System.out.println( "Input String = " + inputString );
br.close();
} catch ( IOException io ) {
System.out.println( io.getMessage() );
}
}
} // bring it into the buffer all at once -> then pull it line by line from the buffer.

**/ 







import java.io.*;

public class BufferedReaderTest2 {
public static void main( String[] args ) {
try {
// create the File and stream
FileReader fr = new FileReader( "C:\\java_pm\\study\\d_day01\\test\\read2.txt" );
// ObjectInputString has a separate `readUTF` function. (ANSI is the default when saving.)
// no input buffer has been created yet, but the main memory device receives in char units.
// create a BufferedReader object that receives a FileReader object as its argument
BufferedReader br = new BufferedReader( fr ); // the input buffer takes part of main memory.
// data is first stored in the BufferedReader, then pulled from there immediately, improving speed,
                // and the buffer size can be defined right here.
// however, because it uses allocated main memory, it is better not to make it too large.
// read one line of the data stored in the file
String readString = br.readLine(); // when it is buffered
// until all of the data stored in the file has been read
while( readString != null ) {
System.out.println( "Read String = " + readString );
// read the next line
readString = br.readLine();
}
// release stream resources
br.close();
} catch ( Exception e ) {
e.printStackTrace();
}
}
}

This English version was translated by Codex.

친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.