import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamEx {
 public static void main(String[] args) {
  File f = new File("C:/noirjo/work1/alter_jari/src/month7_day9/FileTest.java"); //복사할 내용이 있는 파일..
  FileInputStream fis = null;
  FileOutputStream fos = null;
  byte[] bytes = new byte[256];
  int _byte=-1;
  
  try{
   fis = new FileInputStream(f);
   fos = new FileOutputStream("D:/test.txt"); //복사할 파일.....
   
   while((_byte = fis.read()) != -1){
    fos.write(_byte);
   }
   System.out.println("다 읽고 복사 완료!!");
   
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   try{
    if(fis!=null) fis.close();
    if(fos!=null) fos.close();
   } catch (IOException e){
    
   }
  }
 }
}

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamEx {
 public static void main(String[] args) {
  File f = new File("C:/noirjo/work1/alter_jari/src/month7_day9/FileTest.java"); //읽어들일 파일 경로....
  FileInputStream fis = null;
  byte[] bytes = new byte[256];
  int _byte=-1;
  
  try{
   fis = new FileInputStream(f);
   
// while문으로 읽을때...   
//   while((fis.read(bytes)) != -1){
//    System.out.println(new String(bytes));
//   }
   
   //1바이트로 다 읽어 들인다.
   while((_byte = fis.read()) != -1){
    System.out.print((char)_byte);
   }
   
   
  } catch(Exception e){
   e.printStackTrace();
  } finally{
   try{
    if(fis!=null) fis.close();
   } catch (IOException e){
    
   }
  }
 }
}

 

import java.io.File;
import java.io.IOException;

public class FileTest {
 public static void main(String[] args) {
  File f = new File("c:/");
  String contents[] = f.list();
  
  for(int i=0; i<contents.length; i++){
   File tf = new File(f, contents[i]);
   
   if(tf.isDirectory()){
    System.out.println(tf.getName()+" is Directory!!");
   } else {
    System.out.println(tf.getName()+"  File' length is " + tf.length()+" Bytes");
   }
   
  }
  
  
//  File f = new File("C:/selog.txt");
//  System.out.println("cna read?" + f.canRead());
//  System.out.println("cna write?" + f.canWrite());
//  System.out.println("cna execute?" + f.canExecute());
//  
//  File newFile = new File("d:/test.txt");
//  
//  try {
//   System.out.println("new File?" + f.createNewFile());
//  } catch (IOException e) {
//   e.printStackTrace();
//  }
//  
//  
//  try {
//   System.out.println("new File?" + newFile.createNewFile());
//  } catch (IOException e) {
//   e.printStackTrace();
//  }
 }

}

'2020년도 이전 > 입출력' 카테고리의 다른 글

파일 읽고, 쓰기 - 자바 파일 복사  (0) 2013.07.09
파일 읽기....  (0) 2013.07.09
알파벳 입력하면 아스키코드 출력  (0) 2013.07.09

import java.io.IOException;

public class InputStreamEx {
 public static void main(String[] args) throws IOException {
  System.out.println("입력하삼..");
  
  int _byte;
  
  while((_byte=System.in.read()) != -1){

   if(_byte == 10 || _byte ==13) continue;   //10 스페이스바, 13 엔터
   if(_byte == 113 || _byte ==81) break; //알파벳 대문자......
   char c = (char)_byte;
   System.out.printf("%s(%d)", c, _byte); 
  }

 }

}

+ Recent posts