File ํด๋์ค
ํ์ผ์ ์ ๋ณด๋ฅผ ๋ค๋ฃจ๋ ํด๋์ค
File f = new File("์์น์ ๋ณด");
ํ์ผ ๊ฐ์ฒด ์์ฑ
์ด๋ฏธ ํ์ผ์ ์กด์ฌํ๋๋ฐ ๊ทธ ํ์ผ์ ๋ค๋ฃจ๊ธฐ ์ํด ํ์ผ "๊ฐ์ฒด"๋ฅผ ์์ฑํ๋ ๊ฑฐ์.
ํ์ผ ๊ตฌ๋ถํ๊ณ ํ์ผ ๊ฐ์ฒด ์์ฑ, ๋๋ ํ ๋ฆฌ ๋ง๋ค๊ธฐ, ํ์ผ ๋ฆฌ์คํธ ์ถ๋ ฅ
import java.io.File;
public class FileEx {
static void listDir(File dir) {
File[] sublist = dir.listFiles();
for(int i=0; i<sublist.length; i++) {
File f = sublist[i];
System.out.print(f.getName()+", "+f.length()+": ");
long t = f.lastModified();
System.out.printf("%tb %td %ta %tT\n", t,t,t,t);
}
}
public static void main(String[] args) {
File f = new File("C:\\Users\\AYU\\javaex\\Ch08");
System.out.println(f.getName()+", "+f.getPath()+", "+f.getParent());
if(f.isFile())
System.out.println(f.getName()+"๋ ํ์ผ์
๋๋ค!");
if(f.isDirectory())
System.out.println(f.getName()+"๋ ๋๋ ํ ๋ฆฌ์
๋๋ค!");
File f2 = new File("C:\\Users\\AYU\\javaex\\Ch08_1"); //์๋ ํ์ผ ๊ฒฝ๋ก
if(!f2.exists())
f2.mkdir();
listDir(f);
}
}
ํ ์คํธ ํ์ผ ๋ณต์ฌ
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TextFileCopy {
public static void main(String[] args) {
File src = new File("C:\\Users\\AYU\\javaex\\Ch08\\uni");
File dest = new File("C:\\Users\\AYU\\javaex\\Ch08\\uni2");
FileReader fin = null;
FileWriter fout = null;
try{
fin = new FileReader(src);
fout = new FileWriter(dest);
int c;
while((c = fin.read()) != -1)
fout.write(c);
System.out.println(src.getName()+"์ "+dest.getName()+"์ผ๋ก ๋ณต์ฌ๋จ!");
fin.close();
fout.close();
}catch(IOException e) {System.out.println("๋ณต์ฌ ์คํจ!!!");}
}
}
์ฌ์ง ํ์ผ ๋ณต์ฌ(์ฌ์ง์ ๋ฐ์ด๋๋ฆฌ ํ์ผ)
์์ ์ ํ๋ง ๋ฌ๋ผ์ง๋(์ฐ๋ ํจ์๋ง ๋ฌ๋ผ์ง๋)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BinaryCopy {
public static void main(String[] args) {
File src = new File("C:\\Users\\AYU\\javaex\\Ch08\\china.jpg");
File dest = new File("C:\\Users\\AYU\\javaex\\Ch08\\china2.jpg");
FileInputStream fin = null;
FileOutputStream fout = null;
try{
fin = new FileInputStream(src);
fout = new FileOutputStream(dest);
int c;
while((c = fin.read()) != -1)
fout.write(c);
System.out.println(src.getName()+"์ "+dest.getName()+"์ผ๋ก ๋ณต์ฌ๋จ!");
fin.close();
fout.close();
}catch(IOException e) {System.out.println("๋ณต์ฌ ์คํจ!!!");}
}
}
ํ์ง๋ง ๋๋ฆผ
๊ณ ์์ผ๋ก ๋ณต์ฌํ๋ ค๋ฉด?
๋๋ํ๊ฒ ๋ฒํผ ์ก์์ ํ๊บผ๋ฒ์ ์ฎ๊ฒจ๋ด๋ ๋ฐฉ๋ฒ
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BinaryCopy {
public static void main(String[] args) {
File src = new File("C:\\Users\\AYU\\javaex\\Ch08\\china.jpg");
File dest = new File("C:\\Users\\AYU\\javaex\\Ch08\\china2.jpg");
FileInputStream fin = null;
FileOutputStream fout = null;
try{
fin = new FileInputStream(src);
fout = new FileOutputStream(dest);
//int c;
//while((c = fin.read()) != -1)
// fout.write(c);
byte[] buf = new byte[1024*200];
int n = fin.read(buf);
fout.write(buf, 0, n);
System.out.println(src.getName()+"์ "+dest.getName()+"์ผ๋ก ๋ณต์ฌ๋จ!");
fin.close();
fout.close();
}catch(IOException e) {System.out.println("๋ณต์ฌ ์คํจ!!!");}
}
}
728x90
๋ฐ์ํ
0