文章目录
1、Java版
1.1、获取本类信息:this.getClass
package a.b.c;
public class Hello {
public static void main(String[] args) {
(new Hello()).getThisClass();
}
private void getThisClass() {
Class c = this.getClass();
System.out.println(c);
// class a.b.c.Hello
System.out.println(c.getSimpleName());
// Hello
System.out.println(c.getName());
// a.b.c.Hello
System.out.println(c.getTypeName());
// a.b.c.Hello
System.out.println(c.getPackage());
// package a.b.c
System.out.println(c.getResource("/"));
// file:/C:/Users/yellow/IdeaProjects/JavaProject/out/production/JavaProject/
System.out.println(c.getResource(""));
// file:/C:/Users/yellow/IdeaProjects/JavaProject/out/production/JavaProject/a/b/c/
System.out.println(c.getResource("").getPath());
// /C:/Users/yellow/IdeaProjects/JavaProject/out/production/JavaProject/a/b/c/
}
}
1.2、文件:File
File f = new File("JavaProject.iml");
// 文件字节数
f.length();
// 是否存在这个文件
f.exists(); // true
// 是否是文件
f.isFile(); // true
// 是否文件夹
f.isDirectory();
// 绝对路径
f.getAbsolutePath(); // C:\Users\yellow\IdeaProjects\JavaProject\JavaProject.iml
// 文件名
f.getName(); // JavaProject.iml
// 是否绝对路径
f.isAbsolute(); // false
-
一些其它方法
-
createNewFile
:当且仅当具有该名称的文件尚不存在时,创建一个新的空文件
delete
:删除由此File表示的文件或目录。只能删除空目录
mkdir
:创建由此File表示的目录
list
:返回一个String数组,表示该File目录中的所有子文件或目录
listFiles
:返回一个File数组,表示该File目录中的所有的子文件或目录。
1.2.1、递归打印目录树
import java.io.File;
public class Hello {
public static void main(String[] args) {
Hello hello = new Hello();
hello.tree("C:\\Users\\yellow\\IdeaProjects\\javaSE");
hello.tree("C:\\Users\\yellow\\IdeaProjects\\javaSE\\javaSE.iml");
hello.tree("C:/Users/yellow/IdeaProjects/javaSE");
hello.tree("/");
}
public void tree(File rootDir) {
System.out.println(rootDir);
if (rootDir.isDirectory()) {
File[] files = rootDir.listFiles();
if (files != null) {
for (File file : files) {
tree(file);
}
}
}
}
public void tree(String fileName) {
tree(new File(fileName));
}
}
1.2.2、文件名拼接+获取同级目录文件全名(还没验证Maven安装后的)
1.2.2.1、待验
import java.io.File;
public class Dict {
public static void main(String[] args) {
Dict d = new Dict();
File f = d.getLateralFile("dict.txt");
System.out.println(f);
System.out.println(f.isFile());
System.out.println(f.exists());
}
// 获取平级文件
public File getLateralFile(String child) {
String parent = this.getClass().getResource("").getPath();
return new File(parent, child);
}
}
1.2.2.2、Maven安装后,别的工程导入依赖可用(已初验,未报错)
BufferedReader bR = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("dict.txt"), StandardCharsets.UTF_8));
1.3、按行读写文件
// 写数据
BufferedWriter bW = new BufferedWriter(new FileWriter("a.txt"));
bW.write("落水无花\n拉");
bW.write("屎无痕\n");
bW.close();
// 读数据(按行)
BufferedReader bR = new BufferedReader(new FileReader("a.txt"));
String line1 = bR.readLine();
String line2 = bR.readLine();
String line3 = bR.readLine();
String line4 = bR.readLine();
System.out.println(line1);
System.out.println(line2);
System.out.println(line3);
System.out.println(line4);
bR.close();
打印结果
2、Scala版
2.1、相对路径读文件
2.2、getClass
println(this.getClass)
// class a.b.c.Hello$
println(this.getClass.getPackage)
// package a.b.c
println(this.getClass.getName)
// a.b.c.Hello$
println(this.getClass.getResource(""))
// file:/C:/Users/yellow/IdeaProjects/mavenP/target/classes/a/b/c/
println(this.getClass.getResource("/"))
// file:/C:/Users/yellow/IdeaProjects/mavenP/target/classes/
println(this.getClass.getResource("").getPath)
// /C:/Users/yellow/IdeaProjects/mavenP/target/classes/a/b/c/
println(this.getClass.getResource("/").getPath)
// /C:/Users/yellow/IdeaProjects/mavenP/target/classes/
2.3、文件名拼接+获取同级目录文件全名(待验)
package a.b.c
import scala.io.{BufferedSource, Source}
object Hello {
def main(args: Array[String]): Unit = {
val path:String = this.getClass.getResource("a.txt").getPath
println(path)
val bufferedSource: BufferedSource = Source.fromFile(path)
val wholeText: String = bufferedSource.mkString
print(wholeText)
}
}
2.4、按行读取
val path:String = this.getClass.getResource("a.txt").getPath
val bufferedSource: BufferedSource = Source.fromFile(path)
val lines: Iterator[String] = bufferedSource.getLines()
lines.foreach(println)
3、Python版
文件名拼接+获取同级目录文件全名
from os import path
PATH = path.dirname(__file__) # 当前文件父级目录的绝对路径
def get_absolute_path(file_name, directory_name=PATH):
return path.join(directory_name, file_name)
def read_lines(file_name):
with open(file_name, encoding='utf-8') as f:
return f.read().strip().split('\n')
if __name__ == '__main__':
print(read_lines(get_absolute_path("a.txt")))