Advertisement
Guest User

Untitled

a guest
Sep 4th, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. package com.strongant.base;
  2.  
  3. import com.sun.tools.javac.util.ArrayUtils;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.BufferedWriter;
  7. import java.io.FileInputStream;
  8. import java.io.IOException;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12. import java.time.LocalDate;
  13. import java.time.LocalDateTime;
  14. import java.util.List;
  15. import java.util.regex.Pattern;
  16. import java.util.stream.Collectors;
  17. import java.util.stream.Stream;
  18.  
  19. /**
  20. * TODO
  21. *
  22. * @author <a href="mailto:strongant1994@gmail.com">strongant</a>
  23. * @see
  24. * @since 2017/8/20
  25. */
  26. public class BaseTest {
  27.  
  28. public static void main(String[] args) throws IOException {
  29.  
  30. //foobar();
  31.  
  32. //joining();
  33.  
  34. //collector();
  35.  
  36. //patternCount();
  37.  
  38.  
  39. //toUnsignedString();
  40.  
  41. //integerOverflow();
  42.  
  43. //toIntExact();
  44.  
  45. //filesList();
  46.  
  47. //fileCount();
  48.  
  49. //filesWalk();
  50.  
  51. //filesWrite();
  52.  
  53. //streamWriteToFile();
  54.  
  55. //bufferedReaderRead();
  56.  
  57. //writeToFile();
  58.  
  59. //linesCount();
  60.  
  61.  
  62. //tryWithResources();
  63.  
  64. //reflectiveOperationException();
  65.  
  66. LocalDateTime localDate = LocalDateTime.now();
  67. System.out.println(localDate);
  68.  
  69. }
  70.  
  71. private static void reflectiveOperationException() {
  72. try {
  73.  
  74. Class<?> clazz = Class.forName("com.biezhi.apple.User");
  75.  
  76. } catch (ReflectiveOperationException e){
  77.  
  78. e.printStackTrace();
  79.  
  80. }
  81. }
  82.  
  83. private static void tryWithResources() {
  84. try {
  85.  
  86. Thread.sleep(1000);
  87.  
  88. FileInputStream fis = new FileInputStream("a/b.txt");
  89.  
  90. fis.read();
  91.  
  92. } catch (InterruptedException | IOException e) {
  93.  
  94. e.printStackTrace();
  95.  
  96. }
  97. }
  98.  
  99. private static void linesCount() throws IOException {
  100. Path path = Paths.get("res/nashorn1.js");
  101. try (BufferedReader reader = Files.newBufferedReader(path)) {
  102. long countPrints = reader
  103. .lines()
  104. .filter(line -> line.contains("print"))
  105. .count();
  106. System.out.println(countPrints);
  107. }
  108. }
  109.  
  110. private static void writeToFile() throws IOException {
  111. Path path = Paths.get("res/output.js");
  112. try (BufferedWriter writer = Files.newBufferedWriter(path)) {
  113. writer.write("print('Hello World');");
  114. }
  115. }
  116.  
  117. private static void bufferedReaderRead() throws IOException {
  118. Path path = Paths.get("res/nashorn1.js");
  119. try (BufferedReader reader = Files.newBufferedReader(path)) {
  120. System.out.println(reader.readLine());
  121. }
  122. }
  123.  
  124. private static void streamWriteToFile() {
  125. try (Stream<String> stream = Files.lines(Paths.get("res/nashorn1.js"))) {
  126. stream
  127. .filter(line -> line.contains("print"))
  128. .map(String::trim)
  129. .forEach(System.out::println);
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. private static void filesWrite() throws IOException {
  136. List<String> lines = Files.readAllLines(Paths.get("res/nashorn1.js"));
  137. lines.add("print('foobar');");
  138. Files.write(Paths.get("res/nashorn1-modified.js"), lines);
  139. }
  140.  
  141. private static void filesWalk() {
  142. Path start = Paths.get("");
  143. int maxDepth = 10;
  144. try (Stream<Path> stream = Files.walk(start, maxDepth)) {
  145. String joined = stream
  146. .map(String::valueOf)
  147. .filter(path -> path.endsWith(".js"))
  148. .sorted()
  149. .collect(Collectors.joining("; "));
  150. System.out.println("walk(): " + joined);
  151. } catch (IOException e) {
  152. e.printStackTrace();
  153. }
  154. }
  155.  
  156. private static void fileCount() {
  157. Path start = Paths.get("");
  158. int maxDepth = 10;
  159. try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) ->
  160. String.valueOf(path).endsWith(".js"))) {
  161. String joined = stream
  162. .sorted()
  163. .map(String::valueOf)
  164. .collect(Collectors.joining("; "));
  165. System.out.println("Found: " + joined);
  166. } catch (IOException e) {
  167. e.printStackTrace();
  168. }
  169. }
  170.  
  171. private static void filesList() {
  172. try (Stream<Path> stream = Files.list(Paths.get(""))) {
  173. String joined = stream
  174. .map(String::valueOf)
  175. .filter(path -> !path.startsWith("."))
  176. .sorted()
  177. .collect(Collectors.joining("; "));
  178. System.out.println("List: " + joined);
  179. } catch (Exception e){
  180. e.printStackTrace();
  181. }
  182. }
  183.  
  184. private static void toIntExact() {
  185. try {
  186. Math.toIntExact(Long.MAX_VALUE);
  187. }
  188. catch (ArithmeticException e) {
  189. System.err.println(e.getMessage());
  190. // => integer overflow
  191. }
  192. }
  193.  
  194. private static void integerOverflow() {
  195. try {
  196. Math.addExact(Integer.MAX_VALUE, 1);
  197. }
  198. catch (ArithmeticException e) {
  199. System.err.println(e.getMessage());
  200. // => integer overflow
  201. }
  202. }
  203.  
  204. private static void toUnsignedString() {
  205. long maxUnsignedInt = (1l << 32) - 1;
  206.  
  207. System.out.println(maxUnsignedInt);
  208.  
  209. String string = String.valueOf(maxUnsignedInt);
  210.  
  211. System.out.println(string);
  212.  
  213. int unsignedInt = Integer.parseUnsignedInt(string, 10);
  214. String string2 = Integer.toUnsignedString(unsignedInt, 2);
  215.  
  216. System.out.println(string2);
  217. }
  218.  
  219. private static void patternCount() {
  220. Pattern pattern = Pattern.compile(".*@gmail\\.com");
  221. long count = Stream.of("bob@gmail.com", "alice@hotmail.com")
  222. .filter(pattern.asPredicate())
  223. .count();
  224.  
  225. System.out.println(count);
  226. }
  227.  
  228. private static void collector() {
  229. String result = Pattern.compile(":")
  230. .splitAsStream("foobar:foo:bar")
  231. .filter(s -> s.contains("bar"))
  232. .sorted()
  233. .collect(Collectors.joining(":"));
  234.  
  235. System.out.println(result);
  236. }
  237.  
  238. private static void joining() {
  239. String foobarStr = "foo:foo:bar:foobar";
  240.  
  241. String result = foobarStr.chars()
  242. .distinct()
  243. .mapToObj(c -> String.valueOf((char)c))
  244. .sorted()
  245. .collect(Collectors.joining());
  246.  
  247. System.out.println(result);
  248. }
  249.  
  250. private static void foobar() {
  251. String strs = String.join(":", "foobar", "foo", "bar");
  252.  
  253. System.out.println(strs);
  254. }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement