package jp03.part0415.study;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileWriterTestFilter01 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = null, copyName = null, tmp = null;
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("파일이름을 입력하세요");
fileName = br.readLine();
br = new BufferedReader(new FileReader(new File(fileName)));
copyName = fileName + "_copy";
File file = new File(copyName);
file.createNewFile();
System.out.println(file.createNewFile());
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
while((tmp = br.readLine()) != null){
bw.write(tmp);
}
bw.flush();
bw.close();
br.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e1){
e1.printStackTrace();
}
}
}
[원본 : test.txt]
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
//한글이 있을 경우는????
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
[결과 1]
[결과 2]
///////////////////////////////////////////////////////////////////////////////
package jp03.part0415.study;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileWriterTestFilter02 {
/**
* @param args
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("문서에 작성할 글자를 입력하시오");
File file = new File("Test.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
file.createNewFile();
while(true){
String str = br.readLine();
if(str.equals("종료")){
break;
}
bw.write(str + "\n");
// bw.newLine();
// bw.flush();
}
bw.flush();
bw.close();
br.close();
}
}
[결과 1]
[결과 2]
'프로그램 > Java & Jsp' 카테고리의 다른 글
Java IO - (9) -> [ObjectInputStream] (0) | 2008.08.20 |
---|---|
Java IO - (8) -> [ObjectOutputStream] (0) | 2008.08.20 |
Java IO - (6) -> [BufferedReader] (0) | 2008.08.20 |
Java IO - (5) -> [FileReader] (0) | 2008.08.20 |
Java IO - (4) -> [FileInputStream] (0) | 2008.08.20 |