[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();
		}
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ