IT/Java

java - 파일입출력

노마드오브 2018. 11. 16. 07:26

package com.example;


import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;


public class Ex1 {


public static void main(String[] args) {

// 문자 스트림

// 입력 Reader - FileReader

// 출력 Writer

// 바이트 스트림

// 입력 InputStream

// 출력 OutputStream

FileReader fileReader = null;

try {

fileReader = new FileReader("C:\\Windows\\system.ini");

int c;

while ((c = fileReader.read()) != -1) {

System.out.print((char) c);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

fileReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}


}


}




package com.example;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ex2 {

public static void main(String[] args) {
InputStreamReader reader = null;
FileInputStream fis = null;
try {
fis = new FileInputStream("D:\\docs\\hangul.txt");
reader = new InputStreamReader(fis, "MS949");
System.out.println("인코딩 문자집합은 " + reader.getEncoding());
int c; 
while ((c = reader.read()) != -1) {
System.out.print((char) c);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}

}

}




package com.example;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Ex3 {

public static void main(String[] args) {
// 입력스트림 준비 - 키보드 입력
Scanner scanner = new Scanner(System.in);
// 출력스트림 준비 - 파일 출력
FileWriter writer = null;
  try {
writer = new FileWriter("D:\\docs\\test.txt");
// 키보드 입력 받은 데이터를 파일로 출력
while (true) {
String line = scanner.nextLine();  // 줄바꿈 문자인 \r\n를 포함하지 않음
if (line.length() == 0) {  // Enter키를 입력한 경우
break;
}
// writer.write(line + "\r\n");   
writer.write(line, 0, line.length());
writer.write("\r\n", 0, 2);
}
scanner.close();  // 입력스트림 닫기
writer.close();  // 출력스트림 닫기
  } catch (IOException e) {
e.printStackTrace();
}
} // main()

}



package com.example;

import java.io.BufferedOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Ex4 {

public static void main(String[] args) {
// 입력스트림 준비
FileReader fileReader = null;
// 출력스트림 준비
BufferedOutputStream bos = null; 
try {
fileReader = new FileReader("D:\\docs\\test2.txt");
bos = new BufferedOutputStream(System.out, 5);
int c;
while ((c = fileReader.read()) != -1) {
bos.write(c);
}
new Scanner(System.in).nextLine();
//bos.flush();
bos.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
} // main()

}



package com.example;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Ex5 {

public static void listDirectory(File dir) {
System.out.println("-----" + dir.getPath() + "의 서브 리스트입니다. ----");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
File[] subFiles = dir.listFiles();
for (File f : subFiles) {
long t = f.lastModified();
Date date = new Date(t);
System.out.print(f.getName());
System.out.print("\t파일크기: " + f.length());
System.out.println("\t" + sdf.format(date));
}
}
    public static void main(String[] args) {
        File f1 = new File("C:\\Windows\\system.ini");
        System.out.println(f1.getPath() + ", " + f1.getParent() + ", " + f1.getName());
        
        String res = "";
        if (f1.isFile()) {
            res = "파일";
        } else if (f1.isDirectory()) {
            res = "디렉토리";
        }
        System.out.println(f1.getPath() + "은 " + res + "입니다.");
        
        
        File f2 = new File("D:\\docs\\java_sample");
        if (!f2.exists()) {
            f2.mkdir();
        }
        
        f2.renameTo(new File("D:\\docs\\javasample"));
        //f2.delete();
        
        listDirectory(new File("D:\\docs"));
        
    } // main()

}