[Day28] Java 28 [9/19]
๋ณด์กฐ์คํธ๋ฆผ
Data[InputStream]/ Data[OutputSteam]
* ใด ๋ฐ์ดํธ์คํธ๋ฆผ
* ใด Filter[InputStream] / Filter[OutputStream] ๋ถ๋ชจ
* ๋ณด์กฐ์คํธ๋ฆผ
* ใด byte ๋จ์๊ฐ ์๋ ๊ธฐ๋ณธ์๋ฃํ 8 ๊ฐ์ง๋ก ์ฝ๊ธฐ/์ฐ๊ธฐ๊ฐ ๊ฐ๋ฅํ ๋ฐ์ดํธ์คํธ๋ฆผ
Filter[InputStream] / Filter[OutputStream]
ใด ๋ฐ์ดํธ ์คํธ๋ฆผ
ใด ๋ชจ๋ ๋ณด์กฐ์คํธ๋ฆผ์ ์กฐ์์ด๋ค.
// BufferedInputStream / BufferedOutputStream ๋ณด์กฐ์คํธ๋ฆผ
String name = "์ํํ";
int kor=98, eng=89, mat = 88;
int tot = kor + eng + mat;
double avg = (double)tot/3;
boolean gender = true;
// student.txt ํ์ผ ์์ ํ์์ ์ ๋ณด๋ฅผ ์ ์ฅ.
// [FileReader/Writer] ๋๋ FileInputStream/FileOutputStream
String fileName = ".\\src\\days28\\student.txt";
try (
FileWriter fw = new FileWriter(fileName);
BufferedWriter bw = new BufferedWriter(fw);
) {
/*
fw.write(name); // String
fw.write(kor); // int
fw.write(eng);
fw.write(mat);
fw.write(tot);
fw.write( avg +"" ); // double X -> String
fw.write( gender +"" ); // boolean X -> String
*/
String data = String.format("%s,%d,%d,%d,%d,%f,%b\n"
, name, kor, eng, mat, tot, avg, gender);
bw.write(data);
bw.flush();
// close() -> flush()
System.out.println(" end ");
} catch (Exception e) {
e.printStackTrace();
}
SequenceInputStream
* ์์ฐจ์
* ใด ์ฌ๋ฌ ๊ฐ์ ์ ๋ ฅ์คํธ๋ฆผ์ ์ฐ์์ ์ผ๋ก ์ฐ๊ฒฐํด์ ํ๋์ ์คํธ๋ฆผ์ผ๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ณ ์
* ํ ๋ ์ฌ์ฉํ๋ค.
byte [] arr1 = { 0,1,2};
byte [] arr2 = { 3,4,5};
byte [] arr3 = { 6,7,8};
byte [] outSrc = null;
/*
byte [] outSrc = new byate[ arr1.length + arr2.length + arr3.length] ;
for (int i = 0; i < arr1.length; i++) {
outSrc[i] = arr1[i];
}
for (int i = 0; i < arr2.length; i++) {
outSrc[i+3] = arr1[i];
}
for (int i = 0; i < arr3.length; i++) {
outSrc[i+6] = arr1[i];
}
*/
// Vector ์ปฌ๋ ์
ํด๋์ค ?
// ใด List , ์์ ์ ์ง O, ์ค๋ณต ํ์ฉ O
// ใด ArrayList
// ใด ๋ฉํฐ ์ค๋ ๋์ ์์ == ๋๊ธฐํ ์ฒ๋ฆฌ O
Vector<ByteArrayInputStream> v = new Vector<>();
// ByteArrayInputStream ? ๋ฉ๋ชจ๋ฆฌ ์คํธ๋ฆผ
v.add( new ByteArrayInputStream(arr1) );
v.add( new ByteArrayInputStream(arr2) );
v.add( new ByteArrayInputStream(arr3) );
Enumeration<ByteArrayInputStream> en = v.elements();
// SequenceInputStream sis = new SequenceInputStream( v.get(0), v.get(2));
SequenceInputStream sis = new SequenceInputStream( en );
int data = -1;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while( ( data = sis.read() ) != -1 ) {
output.write(data);
}// while
outSrc = output.toByteArray();
System.out.println( Arrays.toString( outSrc ) );
} catch (IOException e) {
e.printStackTrace();
}
PrintStream // ๋ฐ์ดํธ ์คํธ๋ฆผ์ ์ถ๋ ฅํ๋ ๋ ๋ค์ํ ๋ฉ์๋ ์กด์ฌ
PrintStream ps = System.out;
String name = "ํ๊ธธ๋";
int age = 20;
/*
ps.print();
ps.println();
ps.append(0);
ps.printf(name, args)
*/
๋ฌธ์์คํธ๋ฆผ
898 ๋ฌธ์ ์คํธ๋ฆผ
* Reader, Writer , FileReader, FileWriter
* p 901 PipedReader/ PipedWriter
* : ์ฐ๋ ๋ ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ๋ ์ฌ์ฉํ๋ ํด๋์ค
* p 903 StringReader / StringWriter ๋ฉ๋ชจ๋ฆฌ ์คํธ๋ฆผ
* CharArrayReader/ CharArrayWriter ๋ฉ๋ชจ๋ฆฌ ์คํธ๋ฆผ
* p 904 BufferedReader / BufferedWriter ๋ณด์กฐ ์คํธ๋ฆผ
* p 905 InputStreamReader ๋ณด์กฐ์คํธ๋ฆผ / OuputStreamWriter
*
* p 907 ํ์ค ์ ์ถ๋ ฅ๊ณผ File
* ํ์ค ์ ์ถ๋ ฅ
* ใด InputStream System.in
* ใด PrintStream System.out
* ใด PrintStream System.err
System.out.println("A");// ์ฝ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅ
System.out.println("B");
System.out.println("C");
System.out.println("D");
System.out.println("E");
// ๋ค๋ฅธ ์ค๋ ๋(์ผ๊พผ) ์ถ๋ ฅ ๋ด๋น..
System.err.println("F"); // ์ฝ์๋ก ์๋ฌ ๋ฐ์ดํฐ๋ฅผ ์ถ๋ ฅ
RandomAccessFile
* ๊ทธ๋ฆผ 15-6 : Object -> InputStream, OutputStream X
* -> DataInput, DataOutput ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๊ธฐ ๋๋ฌธ์ + [ ์ฝ๊ธฐ, ์ฐ๊ธฐ ๊ฐ๋ฅ ]
* ใด ํ์ผ์ ์ด๋ ์์น์๋ ์ฝ๊ธฐ/์ฐ๊ธฐ ๊ฐ๋ฅํ ์ฅ์ ์ด ์๋ ์คํธ๋ฆผ
* ใด seek( pos )
* write~()
* read~()
String s = "I Love normal Java";
String q = "javabook";
String name = ".\\src\\days28\\random.txt";
String mode = "rw"; // read, write
try ( RandomAccessFile raf = new RandomAccessFile(name, mode) ){
raf.writeBytes( s ); // "I Love normal Java"
} catch (Exception e) {
e.printStackTrace();
} // try
System.out.println("> ์ํฐ ์น๋ฉด ์งํํฉ๋๋ค. ");
System.in.read();
System.in.skip( System.in.available() ) ; // 13, 10 ์ ๊ฑฐ
try ( RandomAccessFile raf = new RandomAccessFile(name, mode) ){
raf.seek(7); // ํ์ผ ํฌ์ธํฐ( file-pointer ) == ํ์ผ ์์น == FP
raf.writeBytes(q); // "javabook";
// I Love normal Java";
// I Love javabookava
} catch (Exception e) {
e.printStackTrace();
} // try
System.out.println("> ์ํฐ ์น๋ฉด ์งํํฉ๋๋ค. ");
System.in.read();
System.in.skip( System.in.available() ) ; // 13, 10 ์ ๊ฑฐ
try ( RandomAccessFile raf = new RandomAccessFile(name, mode) ){
raf.seek(2);
String line = raf.readLine();
System.out.println( line ); // Love javabookava
} catch (Exception e) {
e.printStackTrace();
} // try
System.out.println(" end ");
File ๊ฐ์ฒด ~944
* 1) ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ์ ์ถ๋ ฅ ๋์ ( ์ค์ )
* 2) File ํด๋์ค๋ ํ์ผ + ํด๋(๋๋ ํ ๋ฆฌ) ๋ฅผ ๋ค๋ฃธ
* File ์ธ์คํด์ค๋ ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ ์ผ ์๋ ์๋ค. isFile(), isDirectory()
* 3) ํ 15-26 , 27
// Properties ์ปฌ๋ ์
ํด๋์ค ํน์ง
// 1) Map
// 2) key + value = entry
// 3) String String ์ธ Hashtable
// 4) setProperty() , getProperty(key)
// 5) store(), load()
// 6) ํ๊ฒฝ ์ค์ ๊ฐ ์ ์ฅ/์ฝ๊ธฐ
String currentDirectory = System.getProperty("user.dir");
// C:\Class\WorkSpace\JavaClass\javaPro
// System.out.println( currentDirectory );
// String child = "src\\days28\\Ex01.java";
// File f = new File(currentDirectory, child);
// File f = new File(currentDirectory);
File f = new File(".\\src\\days28\\Ex01.java"); // ์๋๊ฒฝ๋ก
//System.out.println( f.isDirectory() ); // true , false
//System.out.println( f.isFile() ); // false , true
// .\src\days28\Ex01.java
System.out.println( f.getPath() );
//C:\Class\WorkSpace\JavaClass\javaPro\.\src\days28\Ex01.java
System.out.println( f.getAbsolutePath() ); // ์ ๋ ๊ฒฝ๋ก
// C:\Class\WorkSpace\JavaClass\javaPro\src\days28\Ex01.java
System.out.println( f.getCanonicalPath() ); // ์ ๊ท ๊ฒฝ๋ก
// ์๋์ฐ( ; ) OS์์ ์ฌ์ฉ๋๋ ๊ฒฝ๋ก ๊ตฌ๋ถ์
// ์ ๋์ค ( : )
// ์ ์ ํ๋
System.out.println( File.pathSeparator ); // ";"
System.out.println( File.pathSeparatorChar); // ';'
// ์๋์ฐ ( \ )
// ์ ๋์ค ( / )
System.out.println( File.separator ); // src\days28\Ex01.java
System.out.println( File.separatorChar ); // src\days28\Ex01.java
String currentDirectory = System.getProperty("user.dir");
File parent = new File( currentDirectory );
// System.out.println( parent.isDirectory() );
// sub directory( ํ์ ํด๋ )
/*
String [] list = parent.list(); // ํ์ ํด๋ + ํ์ผ
for (int i = 0; i < list.length; i++) {
System.out.println( list[i] );
}
*/
String pattern = "yyyy-MM-dd a hh:mm";
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
File [] list = parent.listFiles();
for (int i = 0; i < list.length; i++) {
System.out.println( ( list[i].isDirectory() ? "[ํด๋]": "ํ์ผ" )
+" - " + list[i].getName()
+" - " + sdf.format( new Date( list[i].lastModified() ) )
+" - " + ( list[i].isFile() ? list[i].length() +"(bytes)" : "" )
);
}
file ๊ฐ์ฒด ๋ฉ์๋
String pathname = ".\\src\\days28\\Ex01.java";
File f = new File(pathname);
// boolean f.exists(); ํ์ผ๋๋ ํด๋์ ์กด์ฌ ์ ๋ฌด true/false
System.out.println( f.exists() ); // true , false
if( f.isFile() && f.exists() ) {
// ํ์ผ - ์์ฑ - ํน์ฑ
System.out.println( f.canRead() ); // ์ฝ๊ธฐ ๊ฐ๋ฅ true
System.out.println( f.canExecute() ); // ์คํ ๊ฐ๋ฅ true
System.out.println( f.canWrite() ); // ์ฐ๊ธฐ ๊ฐ๋ฅ true
System.out.println( f.isHidden() ); // ์จ๊น ํ์ผ false
}
String pathname = "c:\\upload";
File f = new File( pathname );
// upload ํด๋๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด false ๋ฐํ
// System.out.println( f.exists() );
if( !f.exists() ) {
// boolean mkdir() : make directory ํด๋ ์์ฑํ๋ ๋ฉ์๋
// ๋ฉ์๋ 3๊ฐ์ง : 1) ๊ธฐ๋ฅ 2) ๋งค๊ฐ๋ณ์ 3) ๋ฆฌํด๊ฐ(๋ฆฌํด์ํ)
System.out.println( f.mkdir() ); // true
}else {
// ํด๋ ์ญ์
// delete() ํ์ผ ์ญ์ , ํ์ ํด๋ X
// deleteOnExit() ํ๋ก๊ทธ๋จ ์ข
๋ฃ ์ ํ์ผ ์ญ์ ( ์ฃผ๋ก ์์ํ์ผ ์ญ์ ์ ์ฌ์ฉ )
// JVM
System.out.println( f.delete() );
// f.deleteOnExit();
}
// [๋ฌธ์ ] days25 ํด๋ ์์ ์๋ ์๋ฐ ํ์ผ( .java )๋ง ์ถ๋ ฅ.
String [] list = f.list();
for (int i = 0; i < list.length; i++) {
String fileName = list[i];
// if( fileName.contains(".java") ) { // "[test.java].txt"
if( fileName.endsWith(".java") ) {
System.out.println( fileName );
}
}
String [] list = f.list( new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
// System.out.println( dir + " : " + name);
return name.endsWith(".java") ;
}
} );
String [] list = f.list( (dir, name) -> name.endsWith(".java") );
for (int i = 0; i < list.length; i++) {
String fileName = list[i];
System.out.println( fileName );
}
// days25 ํด๋ ์์ "๋ก๋" ๋ฌธ์์ด์ ํฌํจํ๊ณ ์๋ .java ํ์ผ ๋ชฉ๋ก ์ถ๋ ฅ.
String keyword = "๋ก๋";
String [] list = f.list();
for (int i = 0; i < list.length; i++) {
// ".\\src\\days25" + "\\" + "Ex01.java"
String fileName = pathname + File.separator + list[i];
String line = null;
int lineNumber = 1;
try (
FileReader fr = new FileReader( fileName );
BufferedReader br = new BufferedReader(fr);
) {
while( (line = br.readLine()) != null ) {
if( line.contains(keyword) ) {
line = line.replaceAll(keyword, "<span style='color:red'>"+ keyword+"</span>");
System.out.printf("%s [%d] : %s \n", list[i] , lineNumber, line);
}
lineNumber++;
} //
} catch (Exception e) {
e.printStackTrace();
} //
/*
String content = "";
if( content.contains( keyword ) ) {
System.out.printf("%s\n", list[i]);
} // if
*/
} // for
ํ์ํด๋์ญ์
mkdir(), mkdirs() ์ฐจ์ด์ : https://dev-hell.tistory.com/28 (์ ์ = ์์๋๋ ํ ๋ฆฌ ํ์, ํ์ = ์์๋๋ ํ ๋ฆฌ ํ์ X ๋ค ์์ฑ)
public static void main(String[] args) {
/*
String pathname = "c:\\upload\\tmp";
File f = new File( pathname );
if( !f.exists() ) {
//System.out.println( f.mkdir() ); // false
System.out.println( f.mkdirs() ); // false + parent directories
}
*/
// ์ ๋ถ upload ํด๋๋ฅผ ์ญ์ ํ๊ณ ์ถ๋ค.
String pathname = "c:\\upload";
File f = new File( pathname );
if( f.exists() ) {
// upload ํด๋์ ํ์ ํด๋,ํ์ผ๊ฐ ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ์ญ์ ๋ฅผ ๋ชปํ๋ค.
// System.out.println( f.delete() ); // false
directoryDelete( f ); // upload+ ํ์ ํด๋ + ํ์ํด๋ ์ญ์
}
// ์ฌ๊ท ํจ์ - ์ฌ์ฉ X
} // main
public static void directoryDelete(File f) {
while( !f.delete() ) {
// ํ์ ํด๋ ๋ชจ๋ ์ญ์ .
File [] list = f.listFiles();
for (int i = 0; i < list.length; i++) {
if( list[i].delete() ) {
System.out.printf( "%s ์ญ์ ์๋ฃ.\n", list[i] );
}else {
// ํด๋ + ํ์ํด๋ ์กด์ฌ O
directoryDelete(list[i]);
}
} // if
} // while
System.out.printf("%s ํด๋+ํ์ํด๋ ์ญ์ ์๋ฃ!!! \n", f);
} // directoryDelete
(๋ด๊ฐ ๋ง๋ ์ฌ๊ท ๋ฉ์๋) printSarchFiles : ํน์ ํ์ผ ๋๋ ํ ๋ฆฌ์์์ ํค์๋๋ฅผ ์ฐพ์ ๊ฒ
ํ์ ํ์ผ์ ์ฐพ๊ธฐ ์ํด ํด๋์ผ ๊ฒฝ์ฐ ์ฌ๊ทํจ์๋ฅผ ํตํด ๊ณ์ํด์ ์์ผ๋ก ๋ค์ด๊ฐ๋ ๊ฒ
public static void main(String[] args) {
String pathname = ".\\src"; // ํ์ ํด๋ + ํ์ ํด๋
File f = new File( pathname );
// days25 ํด๋ ์์ "๋ก๋" ๋ฌธ์์ด์ ํฌํจํ๊ณ ์๋ .java ํ์ผ ๋ชฉ๋ก ์ถ๋ ฅ.
String keyword = "๋ก๋";
// ํด๋ , ํค์๋
File searchDir = new File(pathname);
printSarchFiles( searchDir , keyword );
} // main
public static void printSarchFiles(File searchDir, String keyword) {
// ํ์ ํด๋ ๋ฐ ํ์ผ
File [] list = searchDir.listFiles();
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) { // ํด๋
printSarchFiles( list[i], keyword);
} else { // ํ์ผ
// Ex07_05.java ๋ณต์ฌ ๋ถ์ด๊ธฐ.
String line = null;
int lineNumber = 1;
try (
FileReader fr = new FileReader( list[i] );
BufferedReader br = new BufferedReader(fr);
) {
while( (line = br.readLine()) != null ) {
if( line.contains(keyword) ) {
line = line.replaceAll(keyword, "<span style='color:red'>"+ keyword+"</span>");
System.out.printf("%s [%d] : %s \n", list[i] , lineNumber, line);
}
lineNumber++;
} //
} catch (Exception e) {
e.printStackTrace();
} //
} // if
} // for
}
// p 930 ์์ 15-36
// [๋ฌธ์ ] "C:\\upload" ํด๋ ์์ ๋ชจ๋ jpg ํ์ผ์ ์ด๋ฆ์ 4์๋ฆฌ ์ซ์ ํํ๋ก ์ด๋ฆ์ ๋ณ๊ฒฝํ์ธ์.
// 1.jpg -> 0001.jpg
// 89.jpg -> 0089.jpg
/*
String pathname = "C:\\upload\\1.jpg";
File f = new File(pathname );
// 1.jpg [ ํ์ผ์ด๋ฆ ๋ณ๊ฒฝ] => 129.jpg
// boolean renameTo( File dest )
// p 918 ํ 15-28
File dest = new File("C:\\upload", "0001.jpg");
System.out.println( f.renameTo(dest) ); // true
*/
// "C:\\upload" ํด๋
String pathname = "C:\\upload";
File f = new File(pathname);
File [] list = f.listFiles();
for (int i = 0; i < list.length; i++) {
// com.util.FileUtil.java
// list[i] ํ์ผ๋ช
1.jpg -> 1 ํ์ฅ์๋ฅผ ์ ์ธํ ํ์ผ๋ช
0001 + ํ์ฅ์
// System.out.println( list[i].getName() );
String fileName = FileUtil.getFileName(list[i]); // 1.jpg
String ext = FileUtil.getExtension(fileName); // ext
String baseName = FileUtil.getBaseName(fileName); // 1
// String.format(baseName, args)
// ํ์ํ ํด๋์ค
String pattern = "0000"; // ๋ฌด์กฐ๊ฑด ๋ค์๋ฆฌ๋ผ๋ ์๋ฆฌ
DecimalFormat df = new DecimalFormat(pattern);
String newBaseName = df.format( Long.parseLong(baseName) ); // "1"-> 1
String newFileName = String.format("%s%s", newBaseName , ext);
File dest = new File(pathname , newFileName);
System.out.println( list[i].renameTo(dest) );
}//
ํ์ผ ํฌ๊ธฐ ์๋ผ์ ๋๋๊ธฐ
// 4:38 + 3๋ถ
// 37,960 ๋ฐ์ดํธ
// 1024 * 10
String parent = "C:\\Users\\redke\\Desktop\\SS17 - 5๊ฐ์์ค(220808)";
String child = "์ผ์ ํ_(5๊ฐ์์ค)(๋์งํธ์ปจ๋ฒ์ ์ค)AWS ํด๋ผ์ฐ๋ ํ์ฉ ์๋ฐ(Java) Full-Stack ๊ฐ๋ฐ์ ์์ฑ ๊ณผ์ (I).xlsx";
final int VOLUME = 10 * 1024; // 10kb 4๊ฐ ํ์ผ
File f = new File(parent , child);
// > ํ์ผ ํฌ๊ธฐ : 37960 bytes
System.out.println("> ํ์ผ ํฌ๊ธฐ : " + f.length() +" bytes" );
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String baseName = FileUtil.getBaseName(child) ; // _1, _2, _3, _4
String ext = FileUtil.getExtension(child) ;
int data = 0 ; // ์ฝ์ด์จ 1๋ฐ์ดํธ ์ ์ฅ ๋ณ์
int i = 0; // ์ฝ์ด์จ ๋ฐ์ดํธ ์ ์นด์ดํ
ํ ๋ณ์ VOLUME ์ฒดํฌ
int number = 0; // _1, _2, _3, _4
try (
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
){
// 10kb _1.xlsx
// 10kb _2.xlsx
// 10kb _3.xlsx
// 7kb _4.xls
while( ( data = bis.read() ) != -1 ) {
if( i % VOLUME == 0 ) {
if( i != 0 ) bos.close(); // ์ฐ๊ธฐ ์๋ฃ..
child = String.format("%s_%d%s", baseName, ++number, ext);
File temp = new File(parent, child );
fos = new FileOutputStream(f);
bos = new BufferedOutputStream(fos);
}
bos.write(data);
i++;
} // while
System.out.println(" end " );
// 5:05 + 10 +
// 5:48
} catch (Exception e) {
e.printStackTrace();
}
์ต๊ทผ๋๊ธ