read()将文件内容从磁盘中全部读出,放到内存,再给cpu处理,性能低,如果文件量大,很容易内存溢出或卡死. 高效方式: 方式一:一般不用的,代码行多 f = open('users.txt','r',encoding='utf-8') #打开文件 #第一种方式: while True: #写个死循环,知道空字符串停止循环,也就是没有内容了(文件中空行也是有东西的,不代表是空字符串) line = f.readline() #line每次循环,都被替换,这个方法性能高 if line!='':…
1.文件操作(2) 代码 f = open('a.txt','a') # "a" 如果源文件不在,会自动创建 f.write('abc') result = f.read() print(result) f.close() # r read 能读,不能写,打开不存在的文件会报错 # w write 能写,不能读 # a add 能写,不会清空以前的内容,不能读…
public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { // 记录开始时间 long start = System.currentTimeMillis(); // 创建流对象 try ( BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk8.exe")…