Python/Java读取TXT文件

JAVA:

    public static void readTextFile(String filePath) {
try {
String encoding = "GBK";
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
System.out.println(lineTxt);
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}

Python

def readTextFile(filePath):
try:
with open(filePath) as f:
for line in f:
print(line, end='')
except Exception:
print('没有找到此文件')
上一篇:给Linux系统/网络管理员准备的Nmap命令的29个实用范例


下一篇:【面试虐菜】—— Oracle中CHAR、VARCHAR的区别