Last active
September 29, 2022 08:23
-
-
Save zhanzhenchao/3351a1a1cac1bce5c78a to your computer and use it in GitHub Desktop.
Java Template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//逐行读取UTF-8文件 | |
private String readFile( String file ) throws IOException { | |
Reader readerFile = new InputStreamReader( new FileInputStream(file), "UTF-8"); | |
BufferedReader reader = new BufferedReader( readerFile ); | |
String line = null; | |
StringBuilder stringBuilder = new StringBuilder(); | |
String ls = System.getProperty("line.separator"); | |
while( ( line = reader.readLine() ) != null ) { | |
stringBuilder.append( line ); | |
stringBuilder.append( ls ); | |
} | |
return stringBuilder.toString(); | |
} | |
//删除文件 | |
public static void deleteFile(File file) { | |
File[] fileArray = file.listFiles(); | |
if (fileArray.length > 0) { | |
for (File element : fileArray) { | |
if (element.isFile()) { | |
if (element.delete()) { | |
System.out.println("Delete Successfully" + " in: " + element.getAbsolutePath()); | |
} else { | |
System.out.println("Delete False" + " in: " + element.getAbsolutePath()); | |
} | |
} else { | |
deleteFile(element); | |
} | |
} | |
} | |
if (file.delete()) { | |
System.out.println("Delete Successfully" + " in: " + file.getAbsolutePath()); | |
} else { | |
System.out.println("Delete False" + " in: " + file.getAbsolutePath()); | |
} | |
} | |
//压缩文件 | |
public static void zipMultiFile(String filepath, String zippath) { | |
try { | |
File file = new File(filepath);// 要被压缩的文件夹 | |
File zipFile = new File(zippath); | |
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); | |
zipOut.setEncoding("gbk"); | |
if (file.isDirectory()) { | |
File[] files = file.listFiles(); | |
for (File file2 : files) { | |
InputStream input = new FileInputStream(file2); | |
zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + file2.getName())); | |
int temp = 0; | |
while ((temp = input.read()) != -1) { | |
zipOut.write(temp); | |
} | |
input.close(); | |
} | |
} | |
zipOut.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// parse node tag | |
StringBuilder pureRecommend = new StringBuilder(); | |
Document doc = Jsoup.parse(htmlRecommend); | |
Element body = doc.body(); | |
List<Node> childNodes = body.childNodes(); | |
for (Node node : childNodes) { | |
if (node.nodeName().equalsIgnoreCase("#text") && StringUtils.isNotBlank(node.outerHtml())) { | |
String pureText = ((TextNode) node).text().replace("\u00a0", " "); | |
pureRecommend.append(pureText); | |
} | |
if (node.nodeName().equalsIgnoreCase("a") && StringUtils.isNotBlank(node.outerHtml())) { | |
pureRecommend.append(((Element)node).text()); | |
} | |
} | |
return pureRecommend.toString(); | |
// parse img tag | |
Document doc = Jsoup.connect("www.abc.com").get(); | |
Elements img = doc.getElementsByTag("img"); | |
for (Element el : img) { | |
String src = el.absUrl("src"); | |
System.out.println("Image Found!"); | |
System.out.println("src attribute is : "+src); | |
} | |
// replace &nbps | |
node.text().replace("\u00a0", " ") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TraderApplicationTest { | |
public static void main(String[] args) { | |
Lock lock = new Lock(); | |
Worker worker1 = new Worker(lock); | |
worker1.start(); | |
Worker worker2 = new Worker(lock); | |
worker2.start(); | |
} | |
public static class Lock { | |
private final List<TradeOrder> tradeOrders; | |
public Lock() { | |
this.tradeOrders = new ArrayList<>(); | |
} | |
public void plusOne() { | |
synchronized (tradeOrders) { | |
try { | |
System.out.println("开始"); | |
tradeOrders.add(new TradeOrder()); | |
Thread.sleep(3000); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
public void printLength() { | |
System.out.println("数量为" + tradeOrders.size()); | |
} | |
} | |
public static class Worker extends Thread { | |
private final Lock lock; | |
public Worker(Lock lock) { | |
this.lock = lock; | |
} | |
public void run() { | |
try { | |
lock.plusOne(); | |
lock.printLength(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) { | |
Integer[] arr = new Integer[10]; | |
for (int i = 0; i < arr.length; i++) { | |
arr[i] = i + 1; | |
} | |
Collections.shuffle(Arrays.asList(arr)); | |
System.out.println(Arrays.toString(arr)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment