-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chenyuqun
committed
Apr 13, 2021
1 parent
8094757
commit 7f126bc
Showing
8 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package java_basic.io.io2.file; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* @author chenyuqun | ||
* @date 2021/4/13 10:50 上午 | ||
*/ | ||
public class FileList { | ||
public static void main(String[] args) { | ||
File file = new File("./src/main/java/java_basic/io"); | ||
printFile(file); | ||
} | ||
|
||
public static void printFile(File file) { | ||
if (file.isFile()) { | ||
System.out.println(" " + file.getPath()); | ||
return; | ||
} | ||
if (!file.isDirectory()) { | ||
throw new RuntimeException("文件既不是file,又不是directory: filePath: " + file.getPath()); | ||
} | ||
System.out.println(file.getPath()); | ||
File[] files = file.listFiles(); | ||
assert files != null; | ||
for (File fileInDirectory : files) { | ||
printFile(fileInDirectory); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package java_basic.io.io2.file; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author chenyuqun | ||
* @date 2021/4/13 10:08 上午 | ||
*/ | ||
public class FileMethod { | ||
|
||
public static void main(String[] args) throws IOException { | ||
checkMethod(); | ||
createMethod(); | ||
} | ||
|
||
public static void checkMethod() { | ||
File file = new File("."); | ||
System.out.println(file.isDirectory()); | ||
System.out.println(file.isFile()); | ||
System.out.println(file.canRead()); | ||
System.out.println(file.canWrite()); | ||
System.out.println(file.canExecute()); | ||
} | ||
|
||
public static void createMethod() throws IOException { | ||
File file = new File("./src/main/java/java_basic/io/io2/createNewFile.txt"); | ||
if (file.createNewFile()) { | ||
System.out.println("create file success"); | ||
} | ||
|
||
if (file.delete()) { | ||
System.out.println("delete file success"); | ||
} | ||
System.out.println(file.getCanonicalPath()); | ||
|
||
File file2 = File.createTempFile("cyq-temp-", ".txt"); | ||
file2.deleteOnExit(); | ||
System.out.println(file2.getCanonicalPath()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package java_basic.io.io2.file; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author chenyuqun | ||
* @date 2021/4/13 9:29 上午 | ||
*/ | ||
public class FilePath { | ||
public static void main(String[] args) throws IOException { | ||
File file = new File(".."); | ||
// 构造方法传入的路径 | ||
System.out.println(file.getPath()); | ||
// 绝对路径 | ||
System.out.println(file.getAbsolutePath()); | ||
// 规范化路径 | ||
System.out.println(file.getCanonicalPath()); | ||
System.out.println(); | ||
|
||
File file2 = new File("../chenyuqun"); | ||
System.out.println(file2.getPath()); | ||
System.out.println(file2.getAbsolutePath()); | ||
System.out.println(file2.getCanonicalPath()); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package java_basic.io.io2.file; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* @author chenyuqun | ||
* @date 2021/4/13 9:33 上午 | ||
*/ | ||
public class FileSeparator { | ||
public static void main(String[] args) { | ||
// 多个路径的分隔符 | ||
System.out.println(File.pathSeparator); | ||
// 路径中的分隔符 | ||
System.out.println(File.separator); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package java_basic.io.io2.file; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* @author chenyuqun | ||
* @date 2021/4/13 11:27 上午 | ||
*/ | ||
public class PathTest { | ||
public static void main(String[] args) { | ||
Path path = Paths.get(".","src","main","java","java_basic","io"); | ||
System.out.println(path); | ||
// 转换为绝对路径 | ||
System.out.println(path.toAbsolutePath()); | ||
// 转换为规范路径 | ||
System.out.println(path.normalize()); | ||
// 转换为File对象 | ||
System.out.println(path.toFile().getName()); | ||
|
||
// 直接遍历path | ||
System.out.println("path: "); | ||
for (Path path1 : Paths.get(".").toAbsolutePath()) { | ||
System.out.println(path1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package java_basic.io.io2.file; | ||
|
||
/** | ||
* 参考: | ||
* https://www.liaoxuefeng.com/wiki/1252599548343744/1298069154955297 | ||
* File对象: | ||
* 1、文件三个的路径:传入的路径,绝对路径,规范化路径, {@link java_basic.io.io2.file.FilePath} | ||
* 2、文件分隔符:pathSeparator, separator, {@link java_basic.io.io2.file.FileSeparator} | ||
* 3、文件方法;is、can 方法, create方法, {@link java_basic.io.io2.file.FileMethod} | ||
* 4、Path对象:创建、转换成别的路径、转换成File、直接遍历path, {@link java_basic.io.io2.file.PathTest} | ||
* 5、小demo:列举出一个目录下的所有文件名 {@link java_basic.io.io2.file.FileList} | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters