public class Test {
public static int cut;
static String s = "D:\\java\\Ai-effect-testing-platform-heads_feature_replay_0620\\Ai-effect-testing-platform\\1.txt" ;
public static void main ( String [ ] args) throws ExecutionException , InterruptedException , IOException {
FileOutputStream_FileInputStream_byte ( ) ;
}
private static void FileOutputStream_FileInputStream_byte ( ) throws IOException {
File file = new File ( s) ;
if ( ! file. exists ( ) ) {
file. createNewFile ( ) ;
}
FileOutputStream fos = new FileOutputStream ( file) ;
String ss = "zhan yuan5764764654654" ;
fos. write ( ss. getBytes ( ) ) ;
fos. close ( ) ;
FileInputStream fis = new FileInputStream ( file) ;
byte [ ] bytes = new byte [ ss. length ( ) ] ;
while ( fis. read ( bytes, 0 , bytes. length) != - 1 ) {
System . out. println ( new String ( bytes) ) ;
}
}
private static void BufferedWriter_BufferedReader_string ( ) throws IOException {
File file = new File ( s) ;
if ( ! file. exists ( ) ) {
file. createNewFile ( ) ;
}
BufferedWriter bw = new BufferedWriter ( new FileWriter ( file) ) ;
bw. write ( "{\n" +
" \"code\": 200,\n" +
" \"msg\": \"操作成功\",\n" +
" \"data\": [\n" +
" {\n" +
" \"id\": 87,\n" +
" \"type\": \"auth\",\n" +
" \"name\": \"api网关鉴权\",\n" +
" \"domain\": \"http://172.31.7.247:6060\",\n" +
" \"room\": \"华为云\",\n" +
" \"createTime\": 1675842227000,\n" +
" \"updateTime\": 1676447929000,\n" +
" \"creater\": \"clmeng\",\n" +
" \"updater\": \"jingliu23\"\n" +
" },\n" +
" {\n" +
" \"id\": 77,\n" +
" \"type\": \"auth\",\n" +
" \"name\": \"高防演练鉴权\",\n" +
" \"domain\": \"http://hwgf-aiplat.changyan.com/auth/v2/token/\",\n" +
" \"room\": \"华为云\",\n" +
" \"createTime\": 1670310281000,\n" +
" \"updateTime\": 1670329444000,\n" +
" \"creater\": \"clmeng\",\n" +
" \"updater\": \"clmeng\"\n" +
" },\n" +
" {\n" +
" \"id\": 71,\n" +
" \"type\": \"auth\",\n" +
" \"name\": \"混沌测试鉴权\",\n" +
" \"domain\": \"http://chaos-aiplat.changyan.com/auth/v2/token/\",\n" +
" \"room\": \"华为云\",\n" +
" \"createTime\": 1668476969000,\n" +
" \"updateTime\": 1668564563000,\n" +
" \"creater\": \"clmeng\",\n" +
" \"updater\": \"clmeng\"\n" +
" },\n" +
" {\n" +
" \"id\": 69,\n" +
" \"type\": \"auth\",\n" +
" \"name\": \"AI诊断联调鉴权\",\n" +
" \"domain\": \"http://prehwai.changyan.com/auth/\",\n" +
" \"room\": \"华为云\",\n" +
" \"createTime\": 1668077914000,\n" +
" \"updateTime\": 1668077954000,\n" +
" \"creater\": \"clmeng\",\n" +
" \"updater\": \"clmeng\"\n" +
" },\n" +
" {\n" +
" \"id\": 26,\n" +
" \"type\": \"auth\",\n" +
" \"name\": \"测试环境token\",\n" +
" \"domain\": \"http://test-aiplat.changyan.com/auth/\",\n" +
" \"room\": \"酒仙桥\",\n" +
" \"createTime\": 1656387522000,\n" +
" \"updateTime\": 1661492485000,\n" +
" \"creater\": \"kunfang5\",\n" +
" \"updater\": \"kunfang5\"\n" +
" },\n" +
" {\n" +
" \"id\": 33,\n" +
" \"type\": \"auth\",\n" +
" \"name\": \"阿里云鉴权\",\n" +
" \"domain\": \"http://xxzy-aiplat.changyan.com/auth/\",\n" +
" \"room\": \"阿里云\",\n" +
" \"createTime\": 1656989654000,\n" +
" \"updateTime\": 1656989797000,\n" +
" \"creater\": \"ganghuang4\",\n" +
" \"updater\": \"yuanzhan\"\n" +
" },\n" +
" {\n" +
" \"id\": 29,\n" +
" \"type\": \"auth\",\n" +
" \"name\": \"酒仙桥现网鉴权\",\n" +
" \"domain\": \"http://aiplat.changyan.com/auth/\",\n" +
" \"room\": \"酒仙桥\",\n" +
" \"createTime\": 1656986644000,\n" +
" \"updateTime\": 1656986644000,\n" +
" \"creater\": \"ganghuang4\",\n" +
" \"updater\": \"ganghuang4\"\n" +
" }\n" +
" ]\n" +
"}" ) ;
bw. flush ( ) ;
bw. close ( ) ;
BufferedReader br = new BufferedReader ( new FileReader ( file) ) ;
String line;
while ( ( line = br. readLine ( ) ) != null ) {
System . out. println ( line) ;
}
br. close ( ) ;
}
private static void FileWriter_FileReader_char ( ) throws IOException {
File file = new File ( s) ;
if ( ! file. exists ( ) ) {
file. createNewFile ( ) ;
}
FileWriter writer = new FileWriter ( file) ;
writer. write ( "zhan yuan" ) ;
writer. flush ( ) ;
writer. close ( ) ;
FileReader reader = new FileReader ( file) ;
char [ ] ch = new char [ 9 ] ;
reader. read ( ch) ;
for ( char c: ch) {
System . out. println ( c) ;
}
reader. close ( ) ;
}
private static void fun2_6 ( ) throws InterruptedException {
ExecutorService es = Executors . newFixedThreadPool ( 500 ) ;
CountDownLatch latch = new CountDownLatch ( 300 ) ;
Runnable task = ( ) -> {
for ( int i = 0 ; i< 10 ; i++ ) {
System . out. println ( "cut =" + ++ cut) ;
}
latch. countDown ( ) ;
} ;
Long benin = System . currentTimeMillis ( ) ;
for ( int i = 0 ; i< 300 ; i++ ) {
es. submit ( task) ;
}
latch. await ( ) ;
Long end = System . currentTimeMillis ( ) ;
System . out. println ( "time = " + ( end- benin) ) ;
System . err. println ( cut) ;
}
private static void Spilp ( ) {
String data = "6&三-1号柜^7&三-2号柜^8&客服中心" ;
List < Pair < String , String > > list = new ArrayList < > ( ) ;
String [ ] el = data. split ( "\\^" ) ;
for ( String elme : el) {
String [ ] parts = elme. split ( "&" ) ;
String id = parts[ 0 ] ;
String name = parts[ 1 ] ;
System . out. println ( id) ;
System . out. println ( name) ;
list. add ( new Pair < > ( id, name) ) ;
}
}
private static void Schedule ( ) {
int cpuNubmer = Runtime . getRuntime ( ) . availableProcessors ( ) ;
System . out. println ( cpuNubmer) ;
ExecutorService executorService = Executors . newFixedThreadPool ( cpuNubmer) ;
for ( int i = 1 ; i< 10 ; i++ ) {
int fin = i;
executorService. execute ( ( ) -> {
try {
System . out. println ( fin) ;
Thread . sleep ( 2000 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
} ) ;
}
executorService. shutdown ( ) ;
}
private static void Submit ( ) throws ExecutionException , InterruptedException {
ExecutorService executorService = Executors . newFixedThreadPool ( 3 ) ;
for ( int i = 1 ; i< 10 ; i++ ) {
int fin = i;
Future < ? > sub = executorService. submit ( ( ) -> {
System . out. println ( fin) ;
try {
Thread . sleep ( 2000 ) ;
} catch ( InterruptedException e) {
throw new RuntimeException ( e) ;
}
} ) ;
System . out. println ( "result=" + sub. get ( ) ) ;
}
executorService. shutdown ( ) ;
}
}
package com. zhixue. utils ;
import com. alibaba. dubbo. common. utils. StringUtils ;
import com. alibaba. fastjson. JSONObject ;
import com. github. difflib. DiffUtils ;
import com. github. difflib. UnifiedDiffUtils ;
import com. github. difflib. algorithm. DiffException ;
import com. github. difflib. patch. AbstractDelta ;
import com. github. difflib. patch. Patch ;
import java. io. File ;
import java. io. IOException ;
import java. nio. file. Files ;
import java. util. Arrays ;
import java. util. List ;
import java. util. Scanner ;
public class StringDiffUtils {
public static void main ( String [ ] args) throws IOException , DiffException {
String s1 = "121324" ;
String s2 = "121324" ;
List < String > targetS = Arrays . asList ( s1) ;
List < String > sourceS = Arrays . asList ( s2) ;
Patch < String > diff = DiffUtils . diff ( targetS, sourceS) ;
System . out. println ( diff) ;
for ( AbstractDelta < String > delta : diff. getDeltas ( ) ) {
System . out. println ( delta) ;
}
}
}
package com. zhixue. utils ;
import com. github. difflib. UnifiedDiffUtils ;
import com. github. difflib. algorithm. DiffException ;
import com. github. difflib. patch. Patch ;
import java. io. BufferedWriter ;
import java. io. File ;
import java. io. FileWriter ;
import java. io. IOException ;
import java. nio. file. Files ;
import java. util. ArrayList ;
import java. util. HashMap ;
import java. util. List ;
import java. util. Map ;
import java. util. stream. Collectors ;
public class DiffHandleUtils {
public static void main ( String [ ] args) throws DiffException {
List < String > diffString = DiffHandleUtils . diffString ( "D:\\java\\Ai-effect-testing-platform-heads_feature_replay_0620\\Ai-effect-testing-platform\\1.txt" , "D:\\java\\Ai-effect-testing-platform-heads_feature_replay_0620\\Ai-effect-testing-platform\\2.txt" ) ;
DiffHandleUtils . generateDiffHtml ( diffString, "D:\\java\\Ai-effect-testing-platform-heads_feature_replay_0620\\Ai-effect-testing-platform\\diff.html" ) ;
}
public static List < String > diffString ( List < String > original, List < String > revised) throws DiffException {
return diffString ( original, revised, null , null ) ;
}
public static List < String > diffString ( List < String > original, List < String > revised, String originalFileName, String revisedFileName) throws DiffException {
originalFileName = originalFileName == null ? "原始文件" : originalFileName;
revisedFileName = revisedFileName == null ? "对比文件" : revisedFileName;
Patch < String > patch = com. github. difflib. DiffUtils. diff ( original, revised) ;
List < String > unifiedDiff = UnifiedDiffUtils . generateUnifiedDiff ( originalFileName, revisedFileName, original, patch, 0 ) ;
int diffCount = unifiedDiff. size ( ) ;
if ( unifiedDiff. size ( ) == 0 ) {
unifiedDiff. add ( "--- " + originalFileName) ;
unifiedDiff. add ( "+++ " + revisedFileName) ;
unifiedDiff. add ( "@@ -0,0 +0,0 @@" ) ;
} else if ( unifiedDiff. size ( ) >= 3 && ! unifiedDiff. get ( 2 ) . contains ( "@@ -1," ) ) {
unifiedDiff. set ( 1 , unifiedDiff. get ( 1 ) ) ;
unifiedDiff. add ( 2 , "@@ -0,0 +0,0 @@" ) ;
}
List < String > original1 = original. stream ( ) . map ( v -> " " + v) . collect ( Collectors . toList ( ) ) ;
return insertOrig ( original1, unifiedDiff) ;
}
public static List < String > diffString ( String filePathOriginal, String filePathRevised) throws DiffException {
List < String > original = null ;
List < String > revised = null ;
File originalFile = new File ( filePathOriginal) ;
File revisedFile = new File ( filePathRevised) ;
try {
original = Files . readAllLines ( originalFile. toPath ( ) ) ;
revised = Files . readAllLines ( revisedFile. toPath ( ) ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
return diffString ( original, revised, originalFile. getName ( ) , revisedFile. getName ( ) ) ;
}
public static void generateDiffHtml ( List < String > diffString, String htmlPath) {
StringBuilder builder = new StringBuilder ( ) ;
for ( String line : diffString) {
builder. append ( line) ;
builder. append ( "\n" ) ;
}
String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css" ;
String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css" ;
String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js" ;
String template = "\n" +
"\n" +
" \n" +
" \n" +
" + githubCss + "\" />\n" +
" + diff2htmlCss + "\" />\n" +
" \n" +
" \n" +
" \n" +
" \n" +
"
\n" +
" \n" +
"" ;
template = template. replace ( "temp" , builder. toString ( ) ) ;
FileWriter f = null ;
try {
f = new FileWriter ( htmlPath) ;
BufferedWriter buf = new BufferedWriter ( f) ;
buf. write ( template) ;
buf. close ( ) ;
f. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
public static List < String > insertOrig ( List < String > original, List < String > unifiedDiff) {
List < String > result = new ArrayList < > ( ) ;
List < List < String > > diffList = new ArrayList < > ( ) ;
List < String > d = new ArrayList < > ( ) ;
for ( int i = 0 ; i < unifiedDiff. size ( ) ; i++ ) {
String u = unifiedDiff. get ( i) ;
if ( u. startsWith ( "@@" ) && ! "@@ -0,0 +0,0 @@" . equals ( u) && ! u. contains ( "@@ -1," ) ) {
List < String > twoList = new ArrayList < > ( ) ;
twoList. addAll ( d) ;
diffList. add ( twoList) ;
d. clear ( ) ;
d. add ( u) ;
continue ;
}
if ( i == unifiedDiff. size ( ) - 1 ) {
d. add ( u) ;
List < String > twoList = new ArrayList < > ( ) ;
twoList. addAll ( d) ;
diffList. add ( twoList) ;
d. clear ( ) ;
break ;
}
d. add ( u) ;
}
for ( int i = 0 ; i < diffList. size ( ) ; i++ ) {
List < String > diff = diffList. get ( i) ;
List < String > nexDiff = i == diffList. size ( ) - 1 ? null : diffList. get ( i + 1 ) ;
String simb = i == 0 ? diff. get ( 2 ) : diff. get ( 0 ) ;
String nexSimb = nexDiff == null ? null : nexDiff. get ( 0 ) ;
insert ( result, diff) ;
Map < String , Integer > map = getRowMap ( simb) ;
if ( null != nexSimb) {
Map < String , Integer > nexMap = getRowMap ( nexSimb) ;
int start = 0 ;
if ( map. get ( "orgRow" ) != 0 ) {
start = map. get ( "orgRow" ) + map. get ( "orgDel" ) - 1 ;
}
int end = nexMap. get ( "revRow" ) - 2 ;
insert ( result, getOrigList ( original, start, end) ) ;
}
int start = ( map. get ( "orgRow" ) + map. get ( "orgDel" ) - 1 ) ;
start = start == - 1 ? 0 : start;
if ( simb. contains ( "@@ -1," ) && null == nexSimb && map. get ( "orgDel" ) != original. size ( ) ) {
insert ( result, getOrigList ( original, start, original. size ( ) - 1 ) ) ;
} else if ( null == nexSimb && ( map. get ( "orgRow" ) + map. get ( "orgDel" ) - 1 ) < original. size ( ) ) {
insert ( result, getOrigList ( original, start, original. size ( ) - 1 ) ) ;
}
}
return result;
}
public static void insert ( List < String > result, List < String > noChangeContent) {
for ( String ins : noChangeContent) {
result. add ( ins) ;
}
}
public static Map < String , Integer > getRowMap ( String str) {
Map < String , Integer > map = new HashMap < > ( ) ;
if ( str. startsWith ( "@@" ) ) {
String [ ] sp = str. split ( " " ) ;
String org = sp[ 1 ] ;
String [ ] orgSp = org. split ( "," ) ;
map. put ( "orgRow" , Integer . valueOf ( orgSp[ 0 ] . substring ( 1 ) ) ) ;
map. put ( "orgDel" , Integer . valueOf ( orgSp[ 1 ] ) ) ;
String [ ] revSp = org. split ( "," ) ;
map. put ( "revRow" , Integer . valueOf ( revSp[ 0 ] . substring ( 1 ) ) ) ;
map. put ( "revAdd" , Integer . valueOf ( revSp[ 1 ] ) ) ;
}
return map;
}
public static List < String > getOrigList ( List < String > original1, int start, int end) {
List < String > list = new ArrayList < > ( ) ;
if ( original1. size ( ) >= 1 && start <= end && end < original1. size ( ) ) {
for ( ; start <= end; start++ ) {
list. add ( original1. get ( start) ) ;
}
}
return list;
}
}
< dependency>
< groupId> io.github.java-diff-utils< /groupId>
< artifactId> java-diff-utils< /artifactId>
< version> 4 .5 < /version>
< /dependency>