12
23
45
public static void main(String[] args) {
int COUNT = 5;
String pathname = "E:/浙江营销平台/2.txt";
log.info("上传文件路劲为:"+pathname);
//读文件
File file = new File("E:/浙江营销平台/1.txt");
//写文件
File file1 = new File(pathname);
FileReader fr= null;
FileReader fr2= null;
BufferedReader br = null;
BufferedReader br2 = null;
try {
fr=new FileReader(file);
fr2=new FileReader(file);
br=new BufferedReader(fr);
br2=new BufferedReader(fr2);
String mobileNumber ="";
// Set<String> set = new HashSet<>();
int fileCount = 0;
while ((mobileNumber = br.readLine())!=null) {
fileCount++;
}
log.info("行数:"+fileCount);
if(fileCount<=COUNT){
log.info("==============进入到if ===============");
Set<String> set = MarketeeTaskService.insertRedisBuf( br2);
writeMobilesFile(set, file1, pathname);
}if(fileCount > COUNT){
log.info("==============进入到else=================");
for (int i = 0; i < (fileCount / COUNT) + 1; i++) {
log.info("==========for===========");
Set<String> set = MarketeeTaskService.insertRedisBuf( br2, i);
Iterator<String> setValueLs = set.iterator();
while(setValueLs.hasNext()){
log.info("set==" + setValueLs.next());
}
writeMobilesFile(set, file1, pathname);
}
}
}catch(Exception e){
log.error("出错");
}finally{
}
log.info("完成!!!");
}
public static Set<String> insertRedisBuf(BufferedReader br){
String mobileNumber = "";
Set<String> set = new HashSet<String>();
try {
while ((mobileNumber = br.readLine())!=null) {
// redisConnection.sAdd(key.getBytes("UTF-8"),mobileNumber.getBytes("UTF-8"));
log.info("mobileNumber====" + mobileNumber);
set.add(mobileNumber);
}
return set;
} catch (IOException e) {
e.printStackTrace();
}
return set;
}
public static Set<String> insertRedisBuf(BufferedReader br, int line ){
log.info("============insertRedisBuf=================");
String mobileNumber = "";
Set<String> set = new HashSet<String>();
try {
int count = line * 5;
while ((mobileNumber = br.readLine())!=null) {
if((count == line * 5 || count > line * 5) && count < (line+1) * 5){
log.info("mobileNumber====" + mobileNumber);
set.add(mobileNumber);
if(count==((line+1) * 5 -1)){
break;
}
}else{
break;
}
count++;
}
return set;
} catch (IOException e) {
e.printStackTrace();
}
return set;
}
private static Map<String,Object> writeMobilesFile(Set<String> finalSet, File file, String filePath) throws Exception {
Map<String,Object> mobileMap = new HashMap<>();
WirterUtils.writeTxt(finalSet, filePath, file);
mobileMap.put("rows","22");
mobileMap.put("filePath",file.getAbsolutePath());
return mobileMap;
}
WirterUtils
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class WirterUtils {
public static void writeTxt(Set rows, String fileName, File file) throws IOException {
// 写入txt
try {
Writer fw = null;
if(file == null){
file = new File(fileName);
}
try {
fw = new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8");
PrintWriter pw = new PrintWriter(fw);
for (Object line : rows) {
String lineMap = (String) line;
StringBuffer linebuffer = new StringBuffer();
linebuffer.append(lineMap);
linebuffer.append("\r\n");
pw.printf(linebuffer.toString(), "\r\n");
}
pw.flush();
pw.close();
} catch (Exception e) {
throw e;
}finally {
if(fw!=null) {
fw.close();
}
}
} catch (Exception e) {
throw e;
}
}
}