import java.io.*;
public class WriterTest {
String path = WriterTest.class.getResource("test.txt").getPath();
Writer writer;
Reader reader;
public WriterTest() throws IOException {
writer = new FileWriter(path);
reader = new FileReader(path);
}
public static void main(String[] args) throws IOException {
WriterTest test = new WriterTest();
// test.paramCharArray(); // char[] 파라미터
// test.paramInt(); // int 파라미터
test.parmString(); // String 파라미터
test.confirmResult(); // [결과 확인]
test.closing(); // [리소스 반납]
}
void paramCharArray() throws IOException { // char[] 파라미터
char[] cbuf = "문자배열테스트".toCharArray(); // 문자열을 문자배열로
writer.write(cbuf,2,3); // 2번위치 부터 길이 2만큼, 즉,['배','열','테']
writer.flush();
/*
writer.write(cbuf,0,cbuf.length)는 writer.write(cbuf)와 같다.
*/
}
void paramInt() throws IOException { //int 파라미터
for(int i=97;i<=105;i++){
writer.write(i); // 아스키 코드값을 문자로 저장
}
writer.flush();
}
void parmString() throws IOException { // String 파라미터
String str = "스트링 파라미터";
writer.write(str,2,3);// 2번 위치부터 길이 3만큼, 즉 {'링', ' ', '파'}
// 다음은 코드는 동일하다.
// writer.write(str);
// writer.write("스트링 파라미터",0,str.length());
writer.flush();
}
void confirmResult() throws IOException { // [결과 확인]
char[] cbuf = new char[100];
reader.read(cbuf);
System.out.println(new String(cbuf));
}
void closing() throws IOException { // [리소스 반납]
writer.close();
reader.close();
}
}
댓글 영역