最近开始接触python,发现python的语法真的是简单,下面是读取文件的实例对比:
Java
package com.ztesoft.oss.iominterface.httpServlet.toIOM;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
public class test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
//C:\Users\Adminitrator\Desktop\1\a.txt
String encoding = "GBK";
File file = new File("C:/Users/Adminitrator/Desktop/1/a.txt");
if(file.isFile() && file.exists()){
try {
try {
InputStreamReader in = new InputStreamReader(new FileInputStream(file),encoding);
BufferedReader br = new BufferedReader(in);
String lines = null;
while( (lines = br.readLine()) != null ){
System.out.println(lines);
}
br.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
}
Python
f = open('C:/Users/Adminitrator/Desktop/1/a.txt','r',encoding='UTF-8')
print(f.read())
f.close()
通过以上对比,Java需要40行的代码Python只要4行就可实现,而且现在火热的大数据算法Python是首选,所以有机会一定要花时间去了解Python,Python是未来。