(34) [JAVA] 18์ผ ์ ์ถ๋ ฅ ์คํธ๋ฆผ(I/O Stream) - 2
FileWriter
์ถ๋ ฅํ ์ ๋์ฝ๋ ๋ฌธ์๋ฅผ ๋ํดํธ ๋ฌธ์ ์ธ์ฝ๋ฉ์ ๋ฐ์ดํธ๋ก ๋ณํํ์ฌ ํ์ผ ์์คํ ์ ํ์ผ์ ์ ์ฅํ๋ค. OutputStreamWriter ํด๋์ค์ ํ์ ํด๋์ค. ๊ธฐ๋ณธ์ ์ผ๋ก ํ์ผ์ด ์์ผ๋ฉด ์์ฑํ๊ณ , ์ด๋ฏธ ์กด์ฌํ๋ฉด ๊ทธ ํ์ผ์ ๋ฎ์ด์ฐ๋ฏ๋ก ๊ธฐ์กด ๋ด์ฉ์ ์ฌ๋ผ์ง๋ค.
package ch14.unit03;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Reader;
public class Ex21_FileWriter {
public static void main(String[] args) {
String pathname ="test.txt";
int data;
/*
- FileWriter
: ํ์ผ ์ถ๋ ฅ ๋ฌธ์ ์คํธ๋ฆผ
: ํ
์คํธ ํ์ผ๋ง ์ ์ฅ ๊ฐ๋ฅ. ์ด๋ฏธ์ง, 2์ง ํ์ผ์ ์ ์ฅ ๋ถ๊ฐ
*/
try (FileWriter fw=new FileWriter(pathname)){
System.out.println("๋ฌธ์์ด ์
๋ ฅ(์ข
๋ฃ:Ctrl+Z)...");
Reader rd=new InputStreamReader(System.in);
while((data=rd.read())!=-1) {
fw.write(data);
}
fw.flush();
System.out.println("ํ์ผ ์ ์ฅ ์๋ฃ!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
FileReader
ํ์ผ ์์คํ ์ ํ์ผ์ ์ ์ฅ๋ ๋ฐ์ดํธ๋ฅผ ์ ๋์ฝ๋ ๋ฌธ์๋ก ๋ณํํ์ฌ ์ฝ์ด ๋ค์ธ๋ค. InputStreamReader ํด๋์ค์ ํ์ ํด๋์ค ํ์ผ์ด ์กด์ฌํ์ง ์์ผ๋ฉด FileNotFoundException ์์ธ๊ฐ ๋ฐ์
package ch14.unit03;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class Ex22_FileReader {
public static void main(String[] args) {
String pathname="test.txt";
int data;
//FileReader: ํ์ผ ๋ฌธ์ ์
๋ ฅ ์คํธ๋ฆผ
try (FileReader fr=new FileReader(pathname)){
Writer wt=new OutputStreamWriter(System.out);
System.out.println("ํ์ผ ๋ด์ฉ...");
while((data=fr.read())!=-1){
wt.write(data);
}
wt.flush();
} catch (FileNotFoundException e) {
} catch (IOException e) {
e.printStackTrace();
}
}
}
package ch14.unit03;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Ex23_FileReader {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedReader fbr=null;
String pathname=null;
String s;
try {
System.out.println("๊ฒฝ๋ก๋ช
์ ํฌํจํ ํ์ผ๋ช
? :");
pathname=br.readLine();
fbr=new BufferedReader(new FileReader(pathname));
while((s=fbr.readLine())!=null) {
System.out.println(s);
}
} catch (FileNotFoundException e) {
System.out.println("["+pathname+"]ํ์ผ์ ์กด์ฌํ์ง ์์ต๋๋ค.");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
File
File ํด๋์ค๋ ํ์ผ ๋ฐ ๋๋ ํ ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ ์ ์๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํด ์ฃผ๋ ํด๋์ค. ๋๋ ํ ๋ฆฌ ์์ฑ ๋ฐ ์ ๊ฑฐ, ๋๋ ํ ๋ฆฌ ๋ฐ ํ์ผ๋ช ๋ณ๊ฒฝ, ํ์ผ ์ ๊ฑฐ, ๋๋ ํ ๋ฆฌ ๋๋ ํ์ผ์ ๋ํ ์ ๋ณด ๋ฑ์ ์ ๊ณตํ์ง๋ง, ํ์ผ์ ๋ด์ฉ์ ์ ์ถ๋ ฅ ํ๊ธฐ ์ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ์ง ์๋๋ค.
package ch14.unit03;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Ex31_File {
public static void main(String[] args) {
String appDir=System.getProperty("user.dir");
System.out.println("ํ ์์
๊ฒฝ๋ก:"+appDir);
String pathname=appDir+File.separator+"test.txt"; // separator
//ํ์ผ ์กด์ฌ ์ฌ๋ถ
File f =new File(pathname);
if(! f.exists()) {
System.out.println(pathname+" : ํ์ผ์ด ์์ต๋๋ค.");
System.exit(0);
}
try {
System.out.println("ํ์ผ์ ๋ณด~~~~~~~!");
System.out.println("ํ์ผ๋ช
:"+f.getName());
System.out.println("๊ฒฝ๋ก๋ฅผ ํฌํจํ ํ์ผ๋ช
: " + f.getPath()); // C:\๊ฒฝ๋ก\ํ์ผ๋ช
.ํ์ฅ์
System.out.println("์ ๋ ๊ฒฝ๋ก๋ช
: " + f.getAbsolutePath()); // C:\๊ฒฝ๋ก\ํ์ผ๋ช
.ํ์ฅ์
System.out.println("ํ์ค ๊ฒฝ๋ก๋ช
: " + f.getCanonicalPath()); // C:\๊ฒฝ๋ก\ํ์ผ๋ช
.ํ์ฅ์
System.out.println("๋ถ๋ชจ ๊ฒฝ๋ก๋ช
: " + f.getParent());//C:\๊ฒฝ๋ก
System.out.println("ํ์ผ ํฌ๊ธฐ(long): " + f.length());
System.out.println("ํ์ผ ์์ฑ์ผ : "+new Date(f.lastModified()));
SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
System.out.println("ํ์ผ ์์ฑ์ผ : "+sdf.format(new Date(f.lastModified())));
System.out.println("์ฝ๊ธฐ ์์ฑ : "+f.canRead());
System.out.println("์ฐ๊ธฐ ์์ฑ :"+f.canWrite());
} catch (Exception e) {
e.printStackTrace();
}
}
}
package ch14.unit03;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class Ex32_FileDelete {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String pathname;
try {
System.out.print("๊ฒฝ๋ก๋ฅผ ํฌํจํ ์ง์ธ ํ์ผ๋ช
?: ");
pathname = br.readLine();
File f=new File(pathname);
if(! f.exists()) {
System.out.println(pathname+": ์กด์ฌํ์ง ์์ต๋๋ค.");
System.exit(0);
}
// ํ์ผ์ด๋ ํด๋๋ ํ๋ฒ์ ํ๋์ฉ ์ญ์ ๊ฐ๋ฅ
// ํด๋๋ ๋น์ด ์์ง ์์ผ๋ฉด ์ญ์ ๋ถ๊ฐ
boolean b =f.delete();
if(b) {
System.out.println("์ญ์ ์๋ฃ!");
}else {
System.out.println("์ญ์ ๋ถ๊ฐ!!!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
package ch14.unit03;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Ex33_FileList {
public static void main(String[] args) {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s;
try {
System.out.print("ํ์ผ๋ช
๋๋ ๊ฒฝ๋ก? ");
s=br.readLine();
dirlist(s);
}catch (Exception e) {
e.printStackTrace();
}
}
public static void dirlist(String pathname) {
File file =new File(pathname);
File[]ff= file.listFiles();
if(ff==null||ff.length==0) {
return;
}
try {
SimpleDateFormat sdf=new SimpleDateFormat("YYYY-MM-DD hh:mm:ss");
String s;
for(File f:ff) {
s=sdf.format(new Date(f.lastModified()));
System.out.printf("%-40s\t%s\t%s\n", f.getName(), s);
if(f.isFile()) {
System.out.println(f.length());
}else if(f.isDirectory()) {
System.out.println("[ํด๋]");
}else {
System.out.println();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package ch14.unit03;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
public class Ex36_Remove {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s;
try {
System.out.println("์ญ์ ํ ํ์ผ ๋๋ ๊ฒฝ๋ก"); // C:\ex
s = br.readLine();
FileManager fm = new FileManager();
boolean b = fm.removerDir(s);
if (b) {
System.out.println("์ญ์ ์๋ฃ....");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class FileManager {
public boolean removerDir(String pathname) {
boolean b = false;
try {
File f = new File(pathname);
if (f.isDirectory()) {
removeSubDir(pathname);
}
b = f.delete();
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
private void removeSubDir(String pathname) throws Exception {
File[] ff = new File(pathname).listFiles();
try {
if (ff.length == 0) {
return;
}
for (File f : ff) {
if (f.isDirectory()) {
removeSubDir(f.getPath());
}
f.delete();
}
} catch (Exception e) {
throw e;
}
}
}