You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.renchao;
import org.junit.Test;
import sun.net.www.http.HttpClient;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.StringJoiner;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Test01 {
public static void main(String[] args) throws InterruptedException {
long l = System.nanoTime();
// String format = String.format("%s-%s", "a", "b");
// String format = "a" + "-" +"b";
StringBuilder stringBuilder = new StringBuilder();
String s = stringBuilder.append("a").append("-").append("b").toString();
long duration = System.nanoTime() - l;
System.out.println(duration);
System.out.println(TimeUnit.NANOSECONDS.toMillis(duration));
new Thread(Test01::t2).start();
Thread.sleep(10000000);
}
private static void t2() {
System.out.println("aa-t1-t2");
t1();
}
private static void t1() {
int i = 1;
int k = i + 5;
System.out.println(k);
try {
Thread.sleep(10000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void test02() {
List<String> ALLOW_PYTHON_EXT = Arrays.asList("zip", "tar", "gz", "bz2");
List<String> ALLOW_DATA_EXT = Arrays.asList("zip", "tar", "gz", "csv", "txt", "xls", "xlsx");
String fileType = "PYTHONs";
String fileExt = "zip";
boolean isPython = "PYTHON".equals(fileType) && ALLOW_PYTHON_EXT.contains(fileExt);
boolean isData = "DATA".equals(fileType) && ALLOW_DATA_EXT.contains(fileExt);
if (!isPython && !isData) {
System.out.println("文件类型错误");
} else {
System.out.println("文件类型正确====");
}
}
@Test
public void test03() throws IOException {
String ipAddress = "8.8.8.8"; // 要查询的IP地址
String apiKey = "YOUR_API_KEY"; // 在https://ipinfo.io/signup获取您的免费API密钥
String apiUrl = "http://whois.pconline.com.cn/ipJson.jsp?ip=112.64.187.2&json=true";
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8.name()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
String jsonResponse = response.toString();
System.out.println(jsonResponse); // 打印响应,包含地理位置等信息
// 在这里您可以解析jsonResponse并提取所需的地理位置信息
}
@Test
public void test07() {
int numRecords = 5;
String csvFileName = "C:\\Users\\RENCHAO\\Desktop\\temp-sss\\data.csv";
try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(csvFileName, true), StandardCharsets.UTF_8)) {
// 写入CSV文件的列名
// writer.append("line_code,line_name,line_status,company_code,company_name,service_type,metric_value\n");
writer.write('\uFEFF');
// 生成随机数据并写入CSV文件
for (int i = 1; i <= numRecords; i++) {
String lineCode = "Line" + i;
String lineName = "名字" + i;
String lineStatus = "测试" + i;
String companyCode = "Company" + i;
String companyName = "CompanyName" + i;
String serviceType = "T" + i;
int metricValue = new Random().nextInt(1000); // 假设metric_value是0到999之间的整数
writer.append(lineCode + "," + lineName + "," + lineStatus + "," + companyCode + ","
+ companyName + "," + serviceType + "," + metricValue + "\n");
}
System.out.println(numRecords + " 条数据已生成并保存到 " + csvFileName + " 文件中。");
System.out.println("系统默认编码:" + Charset.defaultCharset().displayName());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 按照csv格式保存为文件
* StringJoiner按照指定分隔符拼串
*/
@Test
public void test08() throws IOException {
FileWriter writer = new FileWriter("C:\\Users\\RENCHAO\\Desktop\\test\\data22.csv", true);
StringJoiner sj1 = new StringJoiner(",");
sj1.add("sss").add("ccc");
writer.write(sj1 + "\n");
StringJoiner sj2 = new StringJoiner(",");
sj2.add("kk").add("z,zz");
writer.write(sj2 + "\n");
writer.close();
}
/**
* 读取文件行数
*/
@Test
public void test11() {
String uri = "C:\\Users\\RENCHAO\\Desktop\\test\\data.csv";
// long l = System.currentTimeMillis();
// try {
// long lineCount = Files.lines(Paths.get(uri)).count();
// System.out.println("行数:" + lineCount);
// } catch (IOException e) {
// e.printStackTrace();
// }
// System.out.println("耗时:" + (System.currentTimeMillis() - l));
long l = System.currentTimeMillis();
try (BufferedReader reader = new BufferedReader(new FileReader(uri))) {
long lineCount = 0;
while (reader.readLine() != null) {
lineCount++;
}
System.out.println("行数:" + lineCount);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("耗时:" + (System.currentTimeMillis() - l));
}
/**
* 对比list直接迭代与stream的效率
*/
@Test
public void test13() {
List<Map<String, Object>> list = new ArrayList<>();
for (int i = 0; i < 100000; i++) {
Map<String, Object> map = new HashMap<>();
map.put("id", i);
map.put("group", "aa");
map.put("name", "aa" + i * 2);
list.add(map);
}
for (int i = 100000; i < 200000; i++) {
Map<String, Object> map = new HashMap<>();
map.put("id", i);
map.put("group", "bb");
map.put("name", "bb" + i * 2);
list.add(map);
}
long l = System.currentTimeMillis();
Map<String, List<Map<String, Object>>> group = list.stream().collect(Collectors.groupingBy(m -> m.get("group").toString()));
System.out.println("耗时:" + (System.currentTimeMillis() - l));
Set<String> strings = group.keySet();
System.out.println(strings);
for (String s : strings) {
System.out.println(group.get(s).size());
}
}
/**
* 读取大文件最后一行
*/
@Test
public void test15() throws IOException {
String path = "C:\\Users\\RENCHAO\\Desktop\\test\\data.csv";
// RandomAccessFile file = new RandomAccessFile(path, "r");
// file.seek(file.length() - 2);
// String theLastLine = file.readLine();
// System.out.println(theLastLine);
long l = System.currentTimeMillis();
System.out.println(readLastLine2(path));
System.out.println("耗时:" + (System.currentTimeMillis() - l));
System.out.println(Integer.parseInt("77"));
}
public static String readLastLine(String filePath) throws IOException {
try (RandomAccessFile file = new RandomAccessFile(filePath, "r")) {
StringBuilder lastLine = new StringBuilder();
for (long pointer = file.length() - 1; pointer >= 0; pointer--) {
file.seek(pointer);
int currentByte = file.read();
if (currentByte == '\n' || currentByte == '\r') {
// Found the end of the last line
break;
}
lastLine.insert(0, (char) currentByte);
}
return lastLine.toString();
}
}
public static String readLastLine2(String filePath) throws IOException {
try (RandomAccessFile file = new RandomAccessFile(filePath, "r")) {
for (long pointer = file.length() - 1; pointer >= 0; pointer--) {
file.seek(pointer);
int currentByte = file.read();
if (currentByte == '\n' || currentByte == '\r') {
break;
}
}
return file.readLine();
}
}
/**
* 文件压缩
*/
private void compression(File file) throws IOException {
FileOutputStream outputStream = new FileOutputStream(file.toString().replace("csv", "zip"));
ZipOutputStream zipOut = new ZipOutputStream(outputStream);
zipOut.putNextEntry(new ZipEntry(file.getName()));
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = inputStream.read(bytes)) >= 0) {
zipOut.write(bytes, 0, length);
}
inputStream.close();
zipOut.close();
outputStream.close();
}
/**
* 遍历目录,获取所有文件列表
*/
private void getFileList(File dir, List<File> fileList) {
if (!dir.exists()) {
return;
}
File[] files = dir.listFiles();
if (files == null) {
return;
}
for (File file : files) {
if (file.isDirectory()) {
getFileList(file, fileList);
} else {
fileList.add(file);
}
}
}
@Test
public void test16() {
}
}