FileInputStream/FileOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public void f_InOutStream() throws IOException{ FileInputStream in = null; FileOutputStream out = null; try{ in = new FileInputStream("C:\\a.txt"); out = new FileOutputStream("C:\\b.txt"); int c; int n = 0; while((c = in.read()) != -1){ out.write(c); n++; } System.out.println("InOutStream : " + n); } catch(Exception e){ e.setStackTrace(null); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
BufferedInputStream/BufferedOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public void f_BufferedInOutStream() throws IOException{ BufferedInputStream in = null; BufferedOutputStream out = null; try{ in = new BufferedInputStream(new FileInputStream("C:\\a.txt")); out = new BufferedOutputStream(new FileOutputStream("C:\\b.txt")); int c; int n = 0; while((c = in.read()) != -1){ out.write(c); n++; } System.out.println("BufferedInOutStream : " + n); } catch(Exception e){ e.setStackTrace(null); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
FileReader/FileWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
public void f_ReadWriter() throws IOException{ FileReader in = null; FileWriter out = null; try{ in = new FileReader("C:\\a.txt"); out = new FileWriter("C:\\b.txt"); int c; int n = 0; while((c = in.read()) != -1){ out.write(c); n++; } System.out.println("ReadWriter : " + n); } catch(Exception e){ e.setStackTrace(null); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
BufferedReader/BufferedWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public void f_BufferReadWriter() throws IOException{ BufferedReader in = null; BufferedWriter out = null; try{ in = new BufferedReader(new FileReader("C:\\a.txt")); out = new BufferedWriter(new FileWriter("C:\\b.txt")); int c; int n = 0; while((c = in.read()) != -1){ out.write(c); n++; } System.out.println("BufferedReadWriter : " + n); } catch(Exception e){ e.setStackTrace(null); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
utf8과 euckr 선택하여 읽고 쓰기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public void f_BufferReadWriterUTF8() throws IOException{ BufferedReader in = null; BufferedWriter out = null; try{ in = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\a.txt"), "UTF-8")); //EUCKR out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\b.txt"),"UTF-8")); //EUCKR int c; int n = 0; while((c = in.read()) != -1){ out.write(c); n++; } System.out.println("BufferedReadWriter : " + n); } catch(Exception e){ e.setStackTrace(null); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
Scanner/PrintWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
public void f_ScannerPrintWriter(){ Scanner in = null; PrintWriter out = null; try{ in = new Scanner(new BufferedReader(new FileReader("C:\\a.txt"))); out = new PrintWriter(new BufferedWriter(new FileWriter("C:\\b.txt"))); String c; int n = 0; while(in.hasNext()){ c = in.nextLine(); out.println(c); n++; } System.out.println("ScannerPrintWriter : " + n); } catch(Exception e){ System.out.println("error"); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
DataInputStream/DataOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public void f_DataInOutStream() throws IOException{ DataInputStream in = null; DataOutputStream out = null; try{ out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\b.txt"))); out.writeDouble(3.1415); out.writeInt(100); out.writeUTF("전주대학교"); out.flush(); in = new DataInputStream(new BufferedInputStream(new FileInputStream("C:\\b.txt"))); System.out.println(in.readDouble()); System.out.println(in.readInt()); System.out.println(in.readUTF()); } catch(Exception e){ System.out.println("error"); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
ObjectInputStream/ObjectOutputStream
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public void f_ObjectInOutStream() throws IOException{ ObjectInputStream in = null; ObjectOutputStream out = null; try{ out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("C:\\b.txt"))); out.writeObject(new Date()); out.flush(); in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("C:\\b.txt"))); Date d = (Date)in.readObject(); System.out.println(d); } catch(Exception e){ System.out.println("error"); } finally{ if(in != null){ in.close(); } if(out != null){ out.close(); } } } |
File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
public void f_FileInfo(){ //읽을 파일 이름 지정 String fname = "C:\\a.txt"; //File 객체 생성 File f = new File(fname); System.out.println("파일 " + f.getName() + " 정보:"); System.out.println(); System.out.println("\t패스: " + f.getPath()); System.out.println("\t절대패스: " + f.getAbsolutePath()); System.out.println("\t부모: " + f.getParent()); System.out.println("\t쓰기여부: " + f.canWrite()); System.out.println("\t읽기여부: " + f.canRead()); System.out.println("\t파일여부: " + f.isFile()); System.out.println("\t폴더여부: " + f.isDirectory()); System.out.println("\t수정일: " + new Date(f.lastModified())); System.out.println("\t파일크기: " + f.length()); } |