
Stream :
- ๋จ๋ฐฉํฅ (์์ผ๋ก ๋ง๋ค์ด์ ธ์์)
- ์ถ๋ ฅ : ๋ฐ์ดํฐ๋ฅผ๋ฐ๊นฅ์ผ๋ก ๋ด๋ณด๋ผ๋ -> OutStream
- ์ ๋ ฅ : ๋ฐ์ดํฐ๋ฅผ ๋ฐ์๋ค์ผ ๋ -> InStream
๐ Note Code
Test1
๐ป ์ ๋ ฅ
package com.day18;
import java.io.IOException;
//in (์
๋ ฅ)
public class Test1 {
public static void main(String[] args) throws IOException {
int data;
System.out.print("๋ฌธ์์ด ์
๋ ฅ");
while((data=System.in.read())!=-1) { //๋ฐ์ดํฐ๊ฐ ์๋ค๋ฉด
char ch = (char)data;
System.out.print(ch);
}
}
}
๋ฌธ์์ด ์
๋ ฅabc123
abc123
๐ Note Code
Test2
๐ป ์ ๋ ฅ
import java.io.OutputStream;
//out : ์ถ๋ ฅ์ฉ
public class Test2 {
public static void main(String[] args) {
try {
OutputStream os = System.out; //๊ธฐ๋ณธ์คํธ๋ฆผ์ ์์ํ์คํธ๋ฆผ์ผ๋ก ๊ฐ์ธ์ค
byte[] buffer = new byte[3]; //buffer์ ๋ด์์ ํ๋ฒ์ ๋ด๋ณด๋
buffer [0] = 65;
buffer [1] = 97;
buffer [2] = 122;
//65์ด์ง๋ง, 2byte๋ผ์ 1byte์ฉ ๊ฐ์ ธ์์ผ ํจ
os.write(buffer); //buffer์ ๋ด์ ๋ด์ฉ์ ์ถ๋ ฅ์คํธ๋ฆผ์ ์
//os.close(); //๋ฐ๋์ close
System.out.println();
System.out.println("์ถ๋ ฅ๊ฐ ๋ณด์ฌ?");
} catch (Exception e) {
// TODO: handle exception
}
}
}
Aaz
์ถ๋ ฅ๊ฐ ๋ณด์ฌ?
๐ Note Code
InputStreamReader :
โ ๋ฐ์ดํฐ ์ ์ฅ x -> ๊ทธ๋ฅ ํต๊ณผํ๋ ๋ค๋ฆฌ (BridgeStream
)
โก ์ฌ์ฉ์๊ฐ ํค๋ณด๋๋ก ์
๋ ฅํ(system.in์ด ์
๋ ฅํ ๊ฐ - ) 1byte ๋ฌธ์ ํ๋๋ฅผ 2byte๋ก ๋ณํํด์ฃผ๋ Stream
โข ํ์ผ / ๋คํธ์ํฌ ์์ผ์์ ์ฝ์ ๋ฐ์ดํฐ๋ฅผ ๋ฌธ์์ด๋ก ๋ณํํ ๋ ์ฌ์ฉ
Reader(๋ณด๊ธํ) : ์ ์ฅ๋ฅ๋ ฅ O- BufferedReader(๊ณ ๊ธํ) : ๋ชจ์๋ ๋ฐ์ดํฐ๋ค์ ๊ฐ์ฅ ํจ์จ์ ์ผ๋ก ์ ์ฅํ๋ค๊ฐ ์ฝ๊ฒ ์ ์ถ๋ ฅํ๊ฒ๋ ๋ง๋ ๋ฐ์ดํฐ ์ ์ฅ๊ณต๊ฐ
rd.read( ) : 1byte์ฉ ์ฝ์ด์ด
Test3
๐ป ์ ๋ ฅ
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
//InputStreamReader : Bridge
//Output StreamReader
//buffer
//bufferedReader : ์ ์ฅ๊ณต๊ฐ
public class Test3 {
public static void main(String[] args) throws IOException {
int data;
System.out.print("๋ฌธ์์ด?");
Reader rd = new InputStreamReader(System.in);
while((data=rd.read())!=-1){
char ch = (char)data;
System.out.print(ch);
}
}
}
๋ฌธ์์ด?ab12๊ฐ๋
ab12๊ฐ๋
๐ Note Code
-writer์ ๋ฐ๋์ flush๋ฅผ ์จ์ผ ๊ฐ์ ๋ก ๋ฐ์ดํฐ ๋ด๋ณด๋ด์ง
Test4
๐ป ์ ๋ ฅ
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
public class Test4 {
public static void main(String[] args) throws IOException {
int data;
System.out.print("๋ฌธ์์ด?");
Reader rd = new InputStreamReader(System.in);//ํค๋ณด๋ ์
๋ ฅ๋ ๊ฐ์ ํต๊ณผํ๋ ๋ค๋ฆฌ๋ฅผ ํตํด ์ ์ฅ์rd์ ์ ์ฅํด๋ผ
Writer wr = new OutputStreamWriter(System.out); //์ฝ์์ ๋ด๋ณด๋
while((data=rd.read())!=-1) {
wr.write(data);
wr.flush(); //writer -flush ์ง๊ฟ
}
}
}
๋ฌธ์์ด?ab123๊ฐ๋
ab123๊ฐ๋
๐ Note Code
Test5
๐ป ์ ๋ ฅ
import java.io.FileInputStream;
public class Test5 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("c:\\doc\\test.txt");
int data;
while((data=fis.read())!=-1){
//System.out.print((char)data);
System.out.write(data); //์ฝ์๋ก ์
๋ ฅ
System.out.flush(); //๋จ์ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
}
} catch (Exception e) {
}
}
}
//System.out.print((char)data);
์ด๋ ๊ฒ ์ถ๋ ฅํ๋ฉด ์ธ๊ณ์ด ์ก์ถ
?ยฟ?รยนยฐยฐ? ยน??????? ยธยถยธ?ยฐ? ยด???ยท?
??ยด?ยด??? ยบยธยฟ????? ยฟ?ยธยฎยณยชยถ? ยธยธยผยผ
ยน?ยฑ??ยญ ????ยธยฎ ?ยญยท?ยฐยญ??
------------------------------------------
๋ํด๋ฌผ๊ณผ ๋ฐฑ๋์ฐ์ด ๋ง๋ฅด๊ณ ๋ณ๋๋ก
ํ๋๋์ด ๋ณด์ฐํ์ฌ ์ฐ๋ฆฌ๋๋ผ ๋ง์ธ
๋ฌด๊ถํ ์ผ์ฒ๋ฆฌ ํ๋ ค๊ฐ์ฐ
------------------------------------------
//๋ง์ผ System.out.flush(); ๋จ์ ๋ฐ์ดํฐ๋ฅผ ์๋ถ๋ฌ๋ค์ด๋ฉด ์๋์ฒ๋ผ ์ถ๋ ฅ๋จ
๋ํด๋ฌผ๊ณผ ๋ฐฑ๋์ฐ์ด ๋ง๋ฅด๊ณ ๋ณ๋๋ก
ํ๋๋์ด ๋ณด์ฐํ์ฌ ์ฐ๋ฆฌ๋๋ผ ๋ง์ธ
๐ Note Code
Test6
๐ป ์ ๋ ฅ
import java.io.FileOutputStream;
//FileOutputStream
public class Test6 {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("c:\\doc\\out1.txt");
int data;
System.out.print("๋ฌธ์์ด ์
๋ ฅ");
//๋ช๊ธ์๋ ์
๋ ฅํ ์ง ๋ชจ๋ฅด๋ while + ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ฐ์ ์ฝ์ด๋ผ๊ฑฐ๋๊น System.in.read()
while((data=System.in.read())!=-1) {
fos.write(data);
}
fos.close(); // while๋ฌธ ์ฌ์ฉ์, ๋ฐ๋์ ๋ซ์์ค์ผํจ
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
๋ฌธ์์ด ์
๋ ฅab123 -> ๋ด๊ฐ ์ง์ ํ ์์น์ ํด๋นํ์ผ ์ด๋ฆ + ๋ด์ฉ ์ ์ฅ๋จ
๐ Note Code
โ Scanner ๋จผ์ ๊ฐ ๋ฐ๊ธฐ
โก ํ์ผ์ ์๋ ๋ด์ฉ ์ฝ์ด์ค๊ธฐ (FileInputStream) / ๋ด์ฉ ํ์ผ ์ ์ฅํ๊ธฐ (FileInputStream)
Test7
๐ป ์ ๋ ฅ
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
public class Test7 {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
String str1,str2;
int data;
System.out.print("์๋ณธํ์ผ?");
str1 = sc.next();
System.out.print("๋์ํ์ผ?");
str2 = sc.next();
//์ธ์์ผํจ!!!!!!!
FileInputStream fis = new FileInputStream(str1);
FileOutputStream fos = new FileOutputStream(str2);
while((data=fis.read())!=-1){
fos.write(data);
fos.flush();
}
fis.close();
fos.close();
System.out.println("๋ณต์ฌ์๋ฃ");
} catch (Exception e) {
}
}
}
์๋ณธํ์ผ?c:\\doc\\test.txt
๋์ํ์ผ?c:\\doc\\out2.txt = ๋ณต์ฌ
๋ณต์ฌ์๋ฃ
๐ Note Code
Test8
๐ป ์ ๋ ฅ
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Test8 {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("c:\\doc\\test.txt");
// ํด๋นํ์ผ์์ ์ฝ์ด์จ ๋ด์ฉ์ fis์ ๋ฃ๊ณ -> ๊ทธ๊ฑธ ์๋ inputstreamReader(fis)์ ํตํด 2byte๋ก ์ฝ์ด๋
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String str;
while ((str = br.readLine()) != null) { // string ๋ฐํ๊ฐ null
System.out.println(str);
}
fis.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
ั... //์์์๋ ๊ธ์ ์ก์ถ : ๋ฐ์ดํฐ์ธ์ด๊ธฐ๋๋ฌธ์
๐ Note Code
PrintStream ps = new PrintStream(new FileOutputStream("example.txt"));
ps.println("Hello, World!");
ps.close();
Test9
๐ป ์ ๋ ฅ
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
public class Test9 {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("c:\\doc\\out4.txt");
PrintStream ps = new PrintStream(fos);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));// ํค๋ณด๋์์ ์ ์ฅ๋ ๋ด์ฉ์ br์๋ค๊ฐ ์ ์ฅ
String str;
System.out.print("๋ฌธ์์ด ์
๋ ฅ?");
while ((str = br.readLine()) != null) // br์์ ์๋ str์ ์ฝ์ด๋ด๋ผ
ps.println(str); //line 17 / ps๊ฐ ๊ฐ์ง๊ณ ์๋๊ฑธ๋ก ๋ด๋ณด๋ด๋ผ => ๊ทผ๋ฐ ps๋ fos๋ด์ฉ์ ๋ด๊ณ ์์
ps.close();
fos.close();
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
๋ฌธ์์ด ์
๋ ฅab123 -> ๋ด๊ฐ ์ง์ ํ ์์น์ ํด๋นํ์ผ ์ด๋ฆ + ๋ด์ฉ ์ ์ฅ๋จ
๐ Note Code
Test10
๐ป ์ ๋ ฅ
import java.io.File;
import java.io.IOException;
import java.util.Date;
//File ํด๋์ค
public class Test10 {
public static void main(String[] args) throws IOException {
File f = new File("c:\\doc\\test.txt"); //์ด๋ฏธ ์กด์ฌํ๋ ํ์ผ์ ๋ด์๋๋๊ฒ
if(!f.exists()) { //ํ์ผ์กด์ฌ์ฌ๋ถํ์ธ
//false
System.out.println("ํ์ผ์ด ์๋ค");
return;
}//true
System.out.println("ํ์ผ๋ช
: "+f.getName());
System.out.println("ํ์ผ์ฌ์ด์ฆ : "+f.length());
//2byte * ๊ธ์์ + ์ํฐ ์ (2byte *๊ฐ์ - ๋ง์ง๋ง์ ์ํฐ์ํ๋ค๊ณ ๊ฐ์ ํ์ ๋) + ๋์ด์ฐ๊ธฐ = ํ์ผ์ฌ์ด์ฆ
//read (์คํ๊ฐ๋ฅ? ๋ถ๊ฐ๋ฅ?)๋ผ๋ ๊ธฐ๋ฅ์ด ์์ผ๋ฉด ๋๋ธํด๋ฆญํด๋ ์ฝ์ ์ ์์
System.out.println("ํ์ผ๊ฒฝ๋ก : "+f.getAbsolutePath());
System.out.println("ํ์ผํ์ค๊ฒฝ๋ก : "+f.getCanonicalPath()); //์์ ๋๋ฌธ์, ์์ธ์ฒ๋ฆฌ
//new Data๋ ๋ ์ง๋ง ๊ฐ์ง๊ณ ์ฌ ์ ์์ (๋ง์ง๋ง ๋ณ๊ฒฝ์ผ)
//Calendar๋ ํ์ฌ์๊ฐ์ ๊ฐ์ง๊ณ ์์ ๋ ์ง๋ฅผ ๋ณํ์ํฌ ์ ์์
System.out.println("๋ง๋ ๋ : "+new Date(f.lastModified()));
System.out.println("ํ์ผ๊ฒฝ๋ก2 : "+f.getParent());
System.out.println("์ฝ๊ธฐ์์ฑ : "+f.canRead());
System.out.println("์ฐ๊ธฐ์์ฑ : "+f.canWrite());
}
}
ํ์ผ๋ช
: test.txt
ํ์ผ์ฌ์ด์ฆ : 175
ํ์ผ๊ฒฝ๋ก : c:\doc\test.txt
ํ์ผํ์ค๊ฒฝ๋ก : C:\doc\test.txt
๋ง๋ ๋ : Fri Dec 08 01:42:55 KST 2023
ํ์ผ๊ฒฝ๋ก2 : c:\doc
์ฝ๊ธฐ์์ฑ : true
์ฐ๊ธฐ์์ฑ : true
๐ Note Code
Test11
๐ป ์ ๋ ฅ
import java.io.File;
import java.io.FileOutputStream;
public class Test11 {
public static void main(String[] args) {
//index
String str = "c:\\doc\\temp\\java\\test1.txt";
/*
๋ฐฉ๋ฒ1
*str.substring() : ๋ฌธ์์ด ์ถ์ถ
๋ค์์๋ถํฐ \\์ด๊ฑฐ ์ฐพ์ (์ธ๋ฑ์ค๋ฒํธ-1)
String path = str.substring(0, str.lastIndexOf("\\")); //c~java๊น์ง
System.out.println(path);
File f = new File(path); //outputStream์ด ๋ง๋ค์ด์ผํจ
if(!f.exists()) {
f.mkdirs(); // java๊น์ง์ ๊ฒฝ๋ก๋ฅผ ๋ง๋ฆ (path=f๋ผ์)
}
*/
//๋ฐฉ๋ฒ2 - ์ด ๋ฐฉ๋ฒ์ ์ ์
File f = new File(str);
if(!f.getParentFile().exists()) { /
f.getParentFile().mkdirs();
}
//๊ณต์ฉ
try {
FileOutputStream fos = new FileOutputStream(str);
System.out.println("๋ฌธ์์ด ์
๋ ฅ?");
int data;
while((data = System.in.read())!=-1) {
fos.write(data);
fos.flush();
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
๋ฌธ์์ด ์
๋ ฅab123 -> ๋ด๊ฐ ์ง์ ํ ์์น์ ํด๋นํ์ผ ์ด๋ฆ + ๋ด์ฉ ์ ์ฅ๋จ
๐ Note Code
FileFilter [Interface] :
โ implements FileFilter ํ๋๊ฒ ํฌ์ธํธ
โ ์ค๋ฒ๋ผ์ด๋ (accept) -> true/false ๋ฐํ
โก ๋์คํฌ ๋ด ๋ชจ๋ ํด๋์ ํ์ผ ๋ฆฌ์คํธ๋ฅผ ๋ณด์ฌ์ค
listRoots() :
๊ฐ ๋๋ผ์ด๋ธ(C:, D:) ๋ฑ์ ๋ปํจ
Test12
๐ป ์ ๋ ฅ
import java.io.File;
import java.io.FileFilter;
//FileFilter
class MyFileList implements FileFilter {
private File f;
//spring[framework์์ ๋ง์ด ์ฌ์ฉ] :์์กด์ฑ ์ฃผ์
- ์์ฑ์๋ฅผ ํตํด ์ธ๋ถ์์ ํ์ํ ๊ฐ์ฒด ์์ฑ
public MyFileList(String filePath) { //์์ฑ์ ์ค๋ฒ๋ก๋ฉ ํตํด์ ๊ฐ์ฒด ์์ฑ => ์ผ๋ฐ์ ์ผ๋ก ๊ฐ 1๊ฐ
//MyFileList๊ฐ์ฒด๋ฅผ ์์ฑํ ๋, ์๋ private๋ ๊ฐ์ฒด์์ฑํด์ค์ผํ๋ ์ด์ ? ํด๋์ค ๋ด ํ์ผ f ์ฌ์ฉํ๊ธฐ์ํด์
f = new File(filePath);
}
public void result() {
try {
if(!f.exists()) {
System.out.println("ํ์ผ์ด ์์ต๋๋ค!");
return;
}
System.out.println("์ ๋๊ฒฝ๋ก : "+ f.getAbsolutePath());
System.out.println("ํ์ผํฌ๊ธฐ : "+ f.length());
if(f.isDirectory()) {
File[] lists = f.listFiles(this); //์๊ธฐ์์ ์ file๋ค์ list๋ฅผ ๋ฃ์ด๋ผ => ๋ฐฐ์ด์ ํ์ผ์ด ๋ค์ด๊ฐ
//this = ์ฌ์ฉ์๊ฐ ์
๋ ฅํ ๊ฒฝ๋ก
System.out.println("\nํด๋ ๋ด์ฉ..."); // = ํ์ผ
for(int i =0; i<lists.length;i++) {
System.out.print(lists[i].getName());
System.out.println("\t"+lists[i].length());
}
}
System.out.println("๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ");//์ค์ ํด๋๋ค์ด ๋ณด์ฌ์ง๋ ๊ฒ
File[] root = File.listRoots();
for(int i=0;i<root.length;i++) {
System.out.println(root[i].getPath());
}
} catch (Exception e) {
}
}
//์คํํ ํ์ ์์ , result๋ฉ์๋๋ง ํธ์ถํ๋ฉด ๋จ
@Override
public boolean accept(File pathname) {
return pathname.isFile() || pathname.isDirectory(); //ํ์ผ๊ตฌ์กฐ + ํด๋๊ตฌ์กฐ ๋ณด๊ณ ์ถ์๋
//File[] lists = f.listFiles(this);
//๊ฒฝ๋ก๋ง ์ง์ ํด์ฃผ๋ฉด = ๊ฒฝ๋ก์ ์๋ ๋ด์ฉ์ (override๋ก) ์์์๋ ๊ตฌ์กฐ๋ฅผ ๋ค ์ฝ์ด๋ด๊ณ 41์๋ค๊ฐ ๋ฃ์ด์ค
// => reuslt ํธ์ถ
//return pathname.isFile(); //ํ์ผ๊ตฌ์กฐ๋ง ๋ณด๊ณ ์ถ์๋
}
}
public class Test12 {
public static void main(String[] args) {
MyFileList mf = new MyFileList("c:\\doc");
mf.result();
}
}
์ ๋๊ฒฝ๋ก : c:\doc
ํ์ผํฌ๊ธฐ : 4096
ํด๋ ๋ด์ฉ...
out1.txt 0
out2.txt 175
out4.txt 0
score.txt 424
score1.txt 156
score2.txt 285
score3.txt 305
temp 0
test.txt 175
๋๋ ํ ๋ฆฌ ๊ตฌ์กฐ
C:\