[IT]/java
java.문자열 파일 컨트롤
givemebro
2021. 3. 15. 14:48
반응형

package step3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class MenuService {
private String copyPath;
private String pastePath;
public MenuService(String copyPath, String pastePath) {
super();
this.copyPath = copyPath;
this.pastePath = pastePath;
// pastePath 상위 디렉토리가 없다면 새로 생성해준다
File dir = new File(pastePath).getParentFile();
if (dir.isDirectory() == false) {
dir.mkdirs();
}
}
public void copyAndPasteFile() throws IOException {
BufferedReader br = null;
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader(copyPath));
pw = new PrintWriter(new FileWriter(pastePath));
while (br.ready()) {
pw.println(br.readLine());
}
} finally {
if (br != null) {
br.close();
}
if (pw != null) {
pw.close();
}
}
}
}
package step3;
import java.io.IOException;
public class TestCopyAndPasteMenu {
public static void main(String[] args) {
String copyPath="C:\\4. kosta215\\iotest3\\menu.txt";
String pastePath="C:\\4. kosta215\\iotest4\\복사본-menu.txt";
MenuService service =new MenuService(copyPath, pastePath);
try {
service.copyAndPasteFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
반응형