package com.csdn.study.test;
import java.io.*;
import java.util.*;
import java.util.stream.Stream;
public class shuangSeQiuTest {
private static List<int[]> shuangSeQiu(){
Random random = new Random();
List<int[]> list = new ArrayList<>();
for (int i = 0; i < 20 ; i++) {
int blueBall = 0;
int redBall = 0;
Set blueSet = new HashSet();
do {
blueBall = random.nextInt(33);
if(blueBall != 0) {
blueSet.add(blueBall);
}
}while (blueBall == 0 || blueSet.size() < 6);
do {
redBall = random.nextInt(16);
}while (redBall == 0);
Object[] objects = blueSet.toArray();
int[] intArray = new int[objects.length + 1];
for (int j = 0; j < objects.length; j++) {
intArray[j] = (int)objects[j];
}
intArray[intArray.length - 1] = redBall;
QuickSort.quickSort(intArray,0,intArray.length-2);
for (int j = 0; j < intArray.length; j++) {
System.out.print(intArray[j] + " ");
}
System.out.println();
list.add(intArray);
}
return list;
}
/**
* 写入txt文件
*
* @param fileName
* @return
*/
private static String writeDataHubData(List<int[]> list, String fileName) {
System.out.println("==============开始写入文件===============");
long start = System.currentTimeMillis();
String filePath = "D:\\temp\\txt";
StringBuilder content = new StringBuilder();
BufferedWriter out = null;
try {
if (list != null && !list.isEmpty() && (fileName != null && !"".equals(fileName))) {
fileName += "_" + System.currentTimeMillis() + ".txt";
File pathFile = new File(filePath);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
String relFilePath = filePath + File.separator + fileName;
File file = new File(relFilePath);
if (!file.exists()) {
file.createNewFile();
}
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "gbk"));
for (int i = 0; i < list.size(); i++) {
int[] ints = list.get(i);
for (int i1 = 0; i1 < ints.length; i1++) {
System.out.print(ints[i1] + " ");
out.write(ints[i1] + " ");
if((i1 + 1) % 7 == 0) {
out.newLine();
}
}
System.out.println();
}
System.out.println("写入文件耗时:*********************************" + (System.currentTimeMillis() - start) + "毫秒");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return fileName;
}
}
/**
* 读txt文件
* @return
*/
private static boolean radDataHubData(String fileName) {
System.out.println("==============开始读取文件===============");
String filePath = "D:\\temp\\txt\\" + fileName;
File file = new File(filePath);
if (!file.exists()) return false;
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
Stream<String> lines = br.lines();
lines.forEach(l->{
System.out.println(l);
});
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
}
public static void main(String[] args) {
//生成 写入文件
String fileName = writeDataHubData(shuangSeQiu(), "shuangseqiu");
//读文件
if(fileName != null && !"".equals(fileName))
radDataHubData(fileName);
}
}
效果: