본문 바로가기

프로그래밍/Java

파일 io

TestIO1.java

 

package io;

 

import java.io.*;

 

public class TestIO1 {

 public static void main(String[] args) {
  File f  = new File("D:/Wan/7.2.txt");
  //f.exists(); 존재 여부
  try {
   FileInputStream fis = new FileInputStream(f);
   byte [] b = new byte[1024]; // 0.5 Kb
   
   int n = 0;
   while((n = fis.read(b)) != -1)
    System.out.write(b,0,n);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

 

 

 

 

TestIO02.java

 

package io;

 

import java.io.*;

 

public class TestIO2 {

 public static void main(String[] args) {
  // 원본 파일 선택 > c:/java.java 엔터치면
  // c:/java 사본.java 으로 저장이 되게끔


  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

  // File 객체 만들고 존재하지 않으면
  // 그런파일은 없습니다. 라고 출력하고 다시 입력 받으세요.
  try {
   String org = null;
   File f = null;

   while (true) {
         System.out.print("파일 명 > ");
         org = br.readLine();
         System.out.println(org);
         f = new File(org);
      

     if (!f.exists()) {
         System.out.println("파일이 존재하지않습니다.");
      continue;
     }
       break;
   }
        String name = org.substring(0, org.lastIndexOf('.'));
        String ext = org.substring(3, org.lastIndexOf('.'));
        FileInputStream fis = new FileInputStream(f);
        FileOutputStream fos = new FileOutputStream(name + "사본" + ext);
        byte[] b = new byte[1024];
        int n = 0;

        while ((n = fis.read(b)) != -1) {
             fos.write(b, 0, n);
         }
         fis.close();
         fos.close();
  } catch (IOException e) {
   e.printStackTrace();

  }

 }

}

 

 

 

 

 

 

'프로그래밍 > Java' 카테고리의 다른 글

this() super()  (0) 2012.07.19
Method의 상속(오버라이딩)  (0) 2012.07.09
Data Type  (0) 2012.07.02
this  (0) 2012.06.29
Varags  (0) 2012.06.28