์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- ์ค๋ผํดDB
- ๋ฐฑ์ค
- Python
- ํ์ด์ฌ ์ฒผ๋ฆฐ์ง
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- Reversing
- ์๊ณ ๋ฆฌ์ฆ
- ์ฐ๋ถํฌ
- C
- java
- ๋ฌ์คํธ ํ๋ก๊ทธ๋๋ฐ ๊ณต์ ๊ฐ์ด๋
- ๋ฌ์คํธ
- ํ์ด์ฌ ์ฑ๋ฆฐ์ง
- ์ค๋ผํด
- ์๋ฐ ๊ธฐ์ด
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Python challenge
- ๋ฐฑ์ค ๋ฌ์คํธ
- ํ์ด์ฌ
- ๋ฌ์คํธ ์์
- ์ด์์ฒด์
- ๋ฐ์ดํฐ ํต์
- OS
- Rust
- ์๋ฐ ๊ฐ๋
- ์๋ฐ
- data communication
- Database
- Operating System
- ubuntu
Archives
- Today
- Total
IT’s Portfolio
[Java] Lecture - Day 18 ๋ณธ๋ฌธ
728x90
๋ฐ์ํ
๐ฅ Basic Java - Day 18
๐๐ป [Chapter 13] ์ ์ถ๋ ฅ ์คํธ๋ฆผ๊ณผ ํ์ผ ์ ์ถ๋ ฅ
ํ์ผ ํ์๊ธฐ: FileExplorer.java
- ๋ช
๋ น์ด
- ํ์ผ ๋ชฉ๋ก ๋ณด๊ธฐ: dir
- ํ์ ๋๋ ํ ๋ฆฌ๋ก ์ด๋: cd ..
- ๋ถ๋ชจ ๋๋ ํ ๋ฆฌ๋ก ์ด๋: cd ๋๋ ํ ๋ฆฌ๋ช
- ๋๋ ํ ๋ฆฌ ๋ง๋ค๊ธฐ: mkdir ๋๋ ํ ๋ฆฌ๋ช
- ์ด๋ฆ ๋ฐ๊พธ๊ธฐ: rename {Old ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ๋ช } {New ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ๋ช }
- ์ญ์ : del {ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ๋ช
}
- ์๋ธ ๋๋ ํ ๋ฆฌ๋ ํ์ผ์ด ์กด์ฌํ์ง ์๋ ๋น์ด์๋ ๋๋ ํ ๋ฆฌ๋ง ์ญ์ ํจ
- ํ์ผ ๋ณต์ฌ: copy srcํ์ผ๋ช
destํ์ผ๋ช
- ๋๋ ํ ๋ฆฌ๋ ๋ณต์ฌํ ์ ์์
- ์ข ๋ฃ: exit
- ์ ์ฝ์ฌํญ
- ๊ณต๋ฐฑ์ด ์๋ ํ์ผ๋ช ๋๋ ๋๋ ํ ๋ฆฌ๋ช ์ ์ฌ์ฉํ ์ ์์
- ๋ฌดํ ๋ฃจํ๋ฅผ ๋๋ฉด์ ์ฌ์ฉ์์ ์
๋ ฅ์ ๋ฐ์๋ค์
- ์ฌ์ฉ์ ์ ๋ ฅ์ ๋ถ์ํ์ฌ ๋ช ๋ น์ด๋ฅผ ์์๋
- ๋ช ๋ น์ด์ ๋ฐ๋ผ ์ด๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฉ์๋ ํธ์ถ
- ๋ฌดํ ๋ฃจํ๋ exit๋ฅผ ์ ๋ ฅํ๋ฉด ์ข ๋ฃ๋จ
package explorer;
import java.io.*;
import java.text.*;
import java.util.*;
public class FileExplorer {
File current = new File("C:\\");
public FileExplorer() { }
public void start() {
Scanner sc = new Scanner(System.in);
System.out.println("<< ํ์ผ ํ์๊ธฐ ๋ช
๋ น์ด >>");
System.out.println("1. ํ์ผ ๋ชฉ๋ก ๋ณด๊ธฐ: dir");
System.out.println("2. ํ์ ๋๋ ํ ๋ฆฌ๋ก ์ด๋: cd ๋๋ ํ ๋ฆฌ๋ช
");
System.out.println("3. ๋ถ๋ชจ ๋๋ ํ ๋ฆฌ๋ก ์ด๋: cd ..");
System.out.println("4. ๋๋ ํ ๋ฆฌ ๋ง๋ค๊ธฐ: mkdir ๋๋ ํ ๋ฆฌ๋ช
");
System.out.println("5. ์ด๋ฆ ๋ฐ๊พธ๊ธฐ: rename {Old ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ๋ช
} {New ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ๋ช
}");
System.out.println("6. ์ญ์ : del ํ์ผ ๋๋ ๋๋ ํ ๋ฆฌ๋ช
");
System.out.println("7. ํ์ผ ๋ณต์ฌ: copy srcํ์ผ๋ช
destํ์ผ๋ช
");
System.out.println("8. ์ข
๋ฃ: exit\n");
while(true) {
System.out.print("\n"+current.getPath()+"> ");
String input = sc.nextLine();
StringTokenizer st = new StringTokenizer(input);
boolean chk = false;
try {
switch(st.nextToken()) {
case "dir" -> list();
case "cd" -> changeDir(st.nextToken());
case "mkdir" -> makeDir(st.nextToken());
case "rename" -> rename(st.nextToken(), st.nextToken());
case "del" -> delete(st.nextToken());
case "copy" -> copy(st.nextToken(), st.nextToken());
case "exit" -> chk = true;
default -> System.out.println("์๋ชป๋ ๋ช
๋ น์ด ํ์์
๋๋ค.");
}
} catch(NoSuchElementException e) {
System.out.println("๋ช
๋ น์ด ์ฌ์ฉ๋ฒ ์ค๋ฅ...");
e.printStackTrace();
}
if(chk) break;
}
sc.close();
}
public void list() {
File[] l = current.listFiles();
if(l==null||l.length==0) {
System.out.println("No files and No directories");
return;
}
for(File t:l) {
Date lM = new Date(t.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy๋
MM์ dd์ผ HH:mm:ss");
System.out.println(sdf.format(lM)+"\t");
if(t.isDirectory()) System.out.printf("<DIR>%24s\t", "");
else {
DecimalFormat docFormat = new DecimalFormat("###,###");
System.out.printf("%24s \t", docFormat.format(t.length()));
}
System.out.println(t.getName());
}
}
public void changeDir(String dir) {
if(dir.equals("..")) {
changeToParent();
return;
}
File f = new File(current.getPath(), dir);
if(f.exists()&&f.isDirectory()) current = f;
else System.out.println(dir+" ๋๋ ํ ๋ฆฌ๋ ์กด์ฌํ์ง ์์ต๋๋ค.");
}
public void changeToParent() {
if(current.getPath().equals("C:\\")) {
System.out.println("C:\\๋ ๋ฃจํธ ๋๋ ํ ๋ฆฌ์
๋๋ค.");
return;
}
current = new File(current.getParent());
}
public void makeDir(String dir) {
File f = new File(current.getPath(), dir);
if(!f.exists()) f.mkdir();
else System.out.println(dir+"๋(์) ์ด๋ฏธ ์กด์ฌํ๋ ๋๋ ํ ๋ฆฌ์
๋๋ค.");
}
public void rename(String src, String dest) {
File srcf = new File(current.getPath(), src);
File destf = new File(current.getPath(), dest);
if(!srcf.exists()) {
System.out.println(src+"๋(์) ์กด์ฌํ์ง ์์ต๋๋ค.");
return;
} else if(destf.exists()) {
System.out.println(dest+"๋(์) ์ด๋ฏธ ์กด์ฌํฉ๋๋ค.");
return;
}
srcf.renameTo(destf);
}
public void delete(String name) {
File f = new File(current.getPath(), name);
if(f.exists()) f.delete();
else System.out.println(name+"๋(์) ์กด์ฌํ์ง ์์ต๋๋ค.");
}
public void copy(String src, String dest) {
File srcf = new File(current.getPath(), src);
File destf = new File(current.getPath(), dest);
if(!srcf.exists()) {
System.out.println(src+"๋(์) ์กด์ฌํ์ง ์์ต๋๋ค.");
return;
} else if(destf.exists()) {
System.out.println(dest+"๋(์) ์ด๋ฏธ ์กด์ฌํฉ๋๋ค.");
return;
} else if(srcf.isDirectory()) {
System.out.println(src+"๋(์) ๋๋ ํ ๋ฆฌ์
๋๋ค.");
return;
}
BlockBinaryCopy bbc = new BlockBinaryCopy();
bbc.copy(srcf, destf);
}
public static void main(String[] args) {
FileExplorer exp = new FileExplorer();
exp.start();
}
}
- FileExplorer.java
package explorer;
import java.io.*;
public class BlockBinaryCopy {
public BlockBinaryCopy() { }
public void copy(File src, File dest) {
try {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dest);
byte[] buf = new byte[1024*10];
while(true) {
int n = fis.read(buf);
fos.write(buf, 0, n);
if(n<buf.length) break;
}
fis.close();
fos.close();
System.out.println(
src.getPath() + "๋ฅผ "
+ dest.getPath() + "๋ก ๋ณต์ฌํ์์ต๋๋ค."
);
} catch(IOException e) {
System.out.println(e);
e.printStackTrace();
}
}
}
- BlockBinaryCopy.java
Data, SimpleDateFormat ํด๋์ค
- ๋ ์ง์ ์๊ฐ์ ๋ค๋ฃจ๊ธฐ ์ํ ํด๋์ค
- Date ํด๋์ค
- ๊ธฐ๋ฅ ๋น์ฝ
- java.util.Date
Date today = new Date()
- Calendar ํด๋์ค
- Date ํด๋์ค๋ณด๋ค๋ ๊ธฐ๋ฅ์ด ๋ง์ง๋ง ๋จ์ ๋ค์ด ์กด์ฌ
- java.util.Calendar
Calendar today = Calendar.getInstance()
- java.time ํจํค์ง
- Date ํด๋์ค
- SimpleDateFormat
- ๋ ์ง ๋ฐ์ดํฐ๋ฅผ ์ํ๋ ํํ๋ก ๋ค์ํ๊ฒ ์ถ๋ ฅํ๊ธฐ ์ํ์ฌ ๋ค์ํ ํฌ๋งท์ ์ง์
- java.text.SimpleDateFormat
Date td = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String re = sdf.format(td);
System.out.println(re);
SimpleDateFormat sdf = new SimpleDateFormat("yy๋
M์ d์ผ E์์ผ, H์ m๋ถ s์ด, a");
String re = sdf.format(td);
System.out.println(re);
long time = td.getTime(); // 1970๋
1์ 1์ผ 0์:0๋ถ:0์ด ๋ถํฐ millisecond๋ก ์๊ฐ ์ ์ฅ
System.out.println(sdf.format(time));
DecimalFormat ํด๋์ค
- ์ซ์ ๋ฐ์ดํฐ์ ์์ํ๋ฅผ ์ ์ฉํ๊ธฐ ์ํด ์ฌ์ฉ
- ์ซ์ ๋ฐ์ดํฐ๋ฅผ ๋ค์ํ๊ฒ ํํํ๊ธฐ ์ํ ํฌ๋งท์ ์ ๊ณตํจ
- java.text.DecimalFormat
DecimalFormat df = new DecimalFormat("###,###"); // #: 10์ง์ ,: ๋จ์ ๊ตฌ๋ถ์
DecimalFormat df2 = new DecimalFormat("000.00"); //.: ์์์ 0: ๊ฐ์ด ์์ ๊ฒฝ์ฐ 0
DecimalFormat df3 = new DecimalFormat("#.#-"); // -: ์์๋ถํธ
DecimalFormat df4 = new DecimalFormat("#.#%"); // %: ํผ์ผํธ
String re1 = df.format(100000000); // 100,000,000
String re2 = df2.format(49.5); // 049.50
System.out.println(re1);
System.out.println(re2);
System.out.println(df3.format(314.43)); // 314.43-
System.out.println(df4.format(0.739)); // 73.9%
728x90
๋ฐ์ํ
'Development Study > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java] Lecture - Day 19 (0) | 2024.06.07 |
---|---|
[Java] Lecture - Day 17 (1) | 2024.06.06 |
[Java] Lecture - Day 16 (1) | 2024.05.17 |
[Java] Lecture - Day 15 (0) | 2024.04.26 |
[Java] Lecture - Day 14 (0) | 2024.04.05 |
Comments