Ex80
File ํ์ผ/๋๋ ํ ๋ฆฌ ์กฐ์, ํ์ผ ์ ์ถ๋ ฅ
ํ์ผ ์ ๋ณด
//ํ์ผ ๊ฒฝ๋ก
String path = "C:\\class\\code\\java\\file\\test.txt";
//ํ์ผ ์ฐธ์กฐ ๊ฐ์ฒด > java.io.File ํด๋์ค
File file = new File(path);
//ํด๋น ๊ฒฝ๋ก์ ํ์ผ์ด ์ค์ ๋ก ์กด์ฌํ๋์ง?
//System.out.println(file.exists());
if (file.exists()) {
//ํ์ผ ์กฐ์
System.out.println("ํ์ผ ์์");
System.out.println(file.getName()); //test.txt
System.out.println(file.isFile()); //true > ํ์ผ์ธ์ง
System.out.println(file.isDirectory()); //false > ํด๋์ธ์ง
System.out.println(file.length()); //19 > ํ์ผ ํฌ๊ธฐ(๋ฐ์ดํธ)
System.out.println(file.getAbsolutePath()); //C:\class\code\java\file\test.txt > ๊ฒฝ๋ก
//-------------------------------------------------์ด ์๋ก ์์ฃผ์
System.out.println(file.lastModified()); //1677219006485 > ์์ ํ ๋ ์ง(ํฑ๊ฐ)
System.out.println(file.isHidden()); //false
System.out.println(file.getParent()); //C:\class\code\java\file
//tick > Calendar๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ
Calendar c1 = Calendar.getInstance();
System.out.println(c1.getTimeInMillis());
c1.setTimeInMillis(file.lastModified());
System.out.printf("%tF %tT\n", c1, c1);
} else {
System.out.println("ํ์ผ ์์");
}
ํด๋(Folder), ๋๋ ํ ๋ฆฌ(Directory) ์ ๋ณด
//ํด๋ ๊ฒฝ๋ก
String path = "C:\\class\\code\\java\\file";
//ํด๋ ์ฐธ์กฐ ๊ฐ์ฒด
//- ๋๋ ํ ๋ฆฌ๋ ํ์ผ์ ์ผ์ข
์ด๋ค.
File dir = new File(path);
if (dir.exists()) {
System.out.println("ํด๋ ์์.");
System.out.println(dir.getName()); // file
System.out.println(dir.isFile()); //false
System.out.println(dir.isDirectory()); //true
//*** ํด๋๋ ํฌ๊ธฐ๊ฐ ํญ์ 0์ด๋ค.
System.out.println(dir.length()); //0 > ํด๋์ ํฌ๊ธฐ
System.out.println(dir.getAbsolutePath()); //C:\class\code\java\file
//-------------------------------------------------
System.out.println(dir.lastModified()); //1677218977226
System.out.println(dir.isHidden()); //false
System.out.println(dir.getParent()); //C:\class\code\java
} else {
System.out.println("ํด๋ ์์.");
}
ํ์ผ ์กฐ์
-์ ํ์ผ ๋ง๋ค๊ธฐ
String path = "C:\\class\\code\\java\\file\\์์
.txt";
File file = new File(path);
try {
System.out.println(file.createNewFile());
} catch (IOException e) {
e.printStackTrace();
}
-ํ์ผ๋ช ์์ ํ๊ธฐ
//ํ์ผ ์กฐ์
//- ํ์ผ๋ช
์์ ํ๊ธฐ
//- ์์
.txt > class.txt
String path = "C:\\class\\code\\java\\file\\์์
.txt";
File file = new File(path);
//์์ ํ
String path2 = "C:\\class\\code\\java\\file\\class.txt";
File file2 = new File(path2);
if (file.exists()) {
boolean result = file.renameTo(file2);
System.out.println(result);
}
-ํ์ผ ์ด๋ํ๊ธฐ
//ํ์ผ ์กฐ์
//- ํ์ผ ์ด๋ํ๊ธฐ
String path = "C:\\class\\code\\java\\file\\์์
.txt";
File file = new File(path);
//๊ฒฝ๋ก์ ํ์ผ์ด๋ฆ ๋์์ ๋ฐ๊ฟ ์ ์์
String path2 = "C:\\class\\code\\java\\move\\test.txt";
File file2 = new File(path2);
if (file.exists()) {
boolean result = file.renameTo(file2);
System.out.println(result);
}
-ํ์ผ ์ญ์ ํ๊ธฐ
//ํ์ผ ์กฐ์
//- ํ์ผ ์ญ์ ํ๊ธฐ
String path = "C:\\class\\code\\java\\file\\test.txt";
File file = new File(path);
if (file.exists()) {
//ํด์งํต ์ญ์ : "ํด์งํต" ํด๋๋ก ์ด๋ํ๊ธฐ
//์ง์ง ์ญ์ : ํ๋ ๋์คํฌ์์ ์๋ฉธ
boolean result = file.delete(); //์ง์ง ์ญ์ (๋ณต๊ตฌ ๋ถ๊ฐ๋ฅ)
System.out.println(result);
}
ํด๋ ์กฐ์
- ์ํด๋ ๋ง๋ค๊ธฐ
//ํด๋ ์กฐ์
//- ์ํด๋ ๋ง๋ค๊ธฐ
//String path = "C:\\class\\code\\java\\java\\file\\aaa";
//File dir = new File(path);
//boolean result = dir.mkdir();
//System.out.println(result);
String path = "C:\\class\\code\\java\\file\\bbb\\ccc\\ddd";
File dir = new File(path);
boolean result = dir.mkdirs(); //s๋ถ์ด๋ฉด ์๋ ํด๋ ํ๋ฒ์ ๋ง๋ค์ด์ค
System.out.println(result);
[์๊ตฌ์ฌํญ] ํ์๋ช ๋จ > ๊ฐ์ธ ํด๋ ์์ฑ
String[] member = { "ํ๊ธธ๋", "์๋ฌด๊ฐ", "ํํํ", "ํธํธํธ", "์ ์ฌ์", "๊ฐํธ๋", "์กฐ์ธํธ", "์ด์ด๊ฒฝ" };
for (int i=0; i<member.length; i++) {
//ํ์ 1๋ช
> 1๊ฐ ํด๋ ์์ฑ
String path = String.format("C:\\class\\code\\java\\file\\member\\[๊ฐ์ธํด๋]%s๋", member[i]);
File dir = new File(path);
dir.mkdir();
}
System.out.println("์ข
๋ฃ");
[์๊ตฌ์ฌํญ] ๋ ์ง๋ณ ํด๋ ์์ฑ
//- "2023-01-01" ~ "2023-12-31" ์ด 365๊ฐ ์์ฑ
//- C:\class\code\java\file\plan\2023-01-01
//๋ฃจํ + Calendar
Calendar c = Calendar.getInstance();
c.set(2023, 0, 1);
for (int i=0; i<365; i++) {
System.out.printf("%tF\n", c);
String path = "C:\\class\\code\\java\\file\\plan\\" + String.format("%tF", c);
File dir = new File(path);
dir.mkdir();
c.add(Calendar.DATE, 1);
}
- ํด๋๋ช ์์ ํ๊ธฐ + ์ด๋ํ๊ธฐ
//ํด๋ ์กฐ์
//- ํด๋๋ช
์์ ํ๊ธฐ + ์ด๋ํ๊ธฐ
//- aaa > fff
File dir1 = new File("C:\\class\\code\\java\\file\\ํ์");
File dir2 = new File("C:\\class\\code\\java\\move\\ํ์");
if (dir1.exists()) {
dir1.renameTo(dir2);
}
ํด๋ ์ญ์ ํ๊ธฐ(๋น ํด๋๋ง ์ญ์ ๊ฐ๋ฅ)
- ๋ด์ฉ ์๋ ํด๋ ์ญ์ ํ๋ ค๋ฉด > ํ์ผ ๋ชจ๋ ์ญ์ ํ ํด๋ ์ญ์ (์ง์ ๊ตฌํ)
File dir1 = new File("C:\\class\\code\\java\\file\\bbb");
if (dir1.exists()) {
boolean result = dir1.delete();
System.out.println(result);
}
ํด๋ ๋ด์ฉ๋ณด๊ธฐ
//ํด๋ ๋ด์ฉ ๋ณด๊ธฐ
String path = "C:\\class\\dev\\eclipse";
File dir = new File(path);
if (dir.exists()) {
File[] list = dir.listFiles();
// for (File file : list) {
// System.out.println(file.getName());
// System.out.println(file.isFile());
// System.out.println(file.isDirectory());
// System.out.println();
// }
for (File file : list) {
if(file.isDirectory()) {
System.out.printf("[%s]\n", file.getName());
}
}
'ํ๋ก๊ทธ๋๋ฐ ๊ณต๋ถ > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
LinkedList, TreeSet, TreeMap (0) | 2023.03.14 |
---|---|
์ต๋ช ํด๋์ค(Anonymous Class), Sort (0) | 2023.03.14 |
[18์ผ์ฐจ] HashMap, HashSet (0) | 2023.03.05 |
[17์ผ์ฐจ] ์ปฌ๋ ์ - ArrayList, Stack, Queue (0) | 2023.03.04 |
[16์ผ์ฐจ] ์ ๋ค๋ฆญ(Generic), ํฅ์๋ for๋ฌธ, ์์ธ์ฒ๋ฆฌ(Exception handeling) (0) | 2023.03.04 |