with open "xx/A" as A:
content_a = A.readlines()
with open "xx/B" as B:
content_b = B.readlines()
content_c = sorted(content_a+content_b)
C=open( "xx/C",w)
result =c.write(content_c)
A.close()
B.close()
C.close()
python
lis_A = open('a.txt').readlines()
lis_B = open('b.txt').readlines()
lis_C = zip(lis_A,lis_B)
file_C = open('c.txt', 'w')
for txt in lis_C:
s = ''.join(txt).replace('\n', '')
file_C.write(''.join(sorted(s)) + '\n')
file_C.close()
abcdef
with open(__file__,'r') as file_a:
with open(__file__,'r') as file_b:
with open('C','w') as file_c:
t=file_a.readline()[:4]+file_b.readline()[:4]
file_c.write(''.join(sorted(t)))
如果在一个文件里,取with
试一下
BufferedReader fisa = new BufferedReader(new FileReader("D:\\a.txt"));
BufferedReader fisb = new BufferedReader(new FileReader("D:\\b.txt"));
BufferedWriter bwc = new BufferedWriter(new FileWriter("D:\\c.txt"));
String line = null;
char[] arr;
List<Character> list = new ArrayList<>();
while((line = fisa.readLine()) != null) {
arr = line.toCharArray();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
}
fisa.close();
while((line = fisb.readLine()) != null) {
arr = line.toCharArray();
for (int i = 0; i < arr.length; i++) {
list.add(arr[i]);
}
}
fisb.close();
Object[] array = list.toArray();
Arrays.sort(array);
for (Object object : array) {
bwc.write((char)object);
}
bwc.close();
人生苦短,还是 python 简洁
会两把刷子比较厉害;因为用 py 开发的 rd 还是少的,否则可以做白盒 ,其他搞了半天,本质还是黑盒;
我就一条腿走路...
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
import string
fp = open('test1.txt')
a = fp.read()
fp.close()
fp = open('test2.txt')
b = fp.read()
fp.close()
fp = open('test3.txt','w')
l = list(a + b)
l.sort()
s = ''
s = s.join(l)
fp.write(s)
fp.close()
package test01;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class TestThree {
public static void main(String[] args) {
// TODO Auto-generated method stub
String a = readFile("E:/a.txt");
String b = readFile("E:/b.txt");
StringBuffer sb = new StringBuffer();
sb.append(a).append(b);
char[] aa = sb.toString().toCharArray();
Arrays.sort(aa);
writeFile(String.valueOf(aa));
}
public static String readFile(String filepath){
File file = new File(filepath);
StringBuilder sb = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String s = null;
try {
while((s=br.readLine())!=null){
sb.append(s);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return sb.toString();
}
public static void writeFile(String data){
File file = new File("E:/20180420test.txt");
if(!(file.exists())){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fo = new FileOutputStream(file);
byte[] by = data.getBytes();
try {
fo.write(by);
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
亲测有效哦~Java 代码量还是比 Python 多好多~
def func(a,b,c):
with open(a,'r') as f:
stra = f.readline()
with open(b,'r') as f:
strb = f.readline()
new = ''.join(reversed([stra,strb]))
with open(c,'w') as f:
f.write(new)
假设这 2 个文件每个都有 50G 的大小,但是你的电脑却只有 4G 内存,那上面的代码全部都没用,如果是这种情况怎么办?
"""
2018-05-14 Created by arrow.
文件 A + 文件 B -> 排序的文件C
ref: https://testerhome.com/topics/12900
"""
import os
from collections import OrderedDict
BASE_DIR = 'd:\\practice\\'
FILE_A = 'file_a.txt'
FILE_B = 'file_b.txt'
RESULT = 'result.txt'
# 用 顺序字典 来存放26个文件句柄
HANDLER = OrderedDict()
for letter in range(97, 123):
HANDLER[chr(letter)] = open(os.path.join(BASE_DIR, chr(letter) + '.txt'), 'w', encoding='utf-8')
for file in [FILE_A, FILE_B]:
with open(os.path.join(BASE_DIR, FILE_A), encoding='utf-8') as fs:
while True:
content = fs.read(1)
letter = content.lower()
if not content or letter not in HANDLER: # 排除异常字符
break
HANDLER[letter].write(content)
# 关闭文件流
for k, v in HANDLER.items():
v.close()
# 读取26个文件
for letter in range(97, 123):
HANDLER[chr(letter)] = open(os.path.join(BASE_DIR, chr(letter) + '.txt'), encoding='utf-8')
# 合并26个文件
with open(os.path.join(BASE_DIR, RESULT), 'w', encoding='utf-8') as fs:
for k, v in HANDLER.items():
while True:
content = v.read(1024)
if not content:
break
fs.write(content)
v.close()
# 删除产生的临时文件
for letter in range(97, 123):
os.remove(os.path.join(BASE_DIR, chr(letter) + '.txt'))
with open('a.txt') as a, open('b.txt')as b,open('c.txt','w') as c:c.write(''.join(sorted(list(a.read().strip()+b.read().strip()),key=lambda i:ord(i.lower()))))
Python 一行代码系列