[关闭]
@snail-lb 2017-02-28T15:22:24.000000Z 字数 22929 阅读 759

经常查询的代码

java进阶


一、二叉树的常见操作

  1. package com.cn;
  2. import java.lang.reflect.Array;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class BinaryTree<E> {
  6. /** 根节点 **/
  7. Node<E> rootNode;
  8. /** 树深度 **/
  9. int depth;
  10. class Node<E> {
  11. /** 节点数据 **/
  12. E date;
  13. /** 左子树 **/
  14. Node<E> left;
  15. /** 右子树 **/
  16. Node<E> right;
  17. Node(E date) {
  18. this.date = date;
  19. this.left = null;
  20. this.right = null;
  21. }
  22. Node(E date, Node<E> left, Node<E> right) {
  23. this.date = date;
  24. this.left = left;
  25. this.right = right;
  26. }
  27. }
  28. public BinaryTree() {
  29. }
  30. /**
  31. * 使用数组构造一个完全二叉树
  32. *
  33. * @param o
  34. */
  35. public BinaryTree(E[] o) {
  36. this();
  37. Node<E> root = null;
  38. this.rootNode = createFullBinaryTree(root, o, 0);
  39. }
  40. /**
  41. * 创建满二叉树函数
  42. *
  43. * @param root
  44. * @param o
  45. * @param index
  46. * @return
  47. */
  48. private Node<E> createFullBinaryTree(Node<E> root, E[] o, int index) {
  49. if (index >= o.length) {
  50. return null;
  51. }
  52. root = new Node<E>(o[index]);
  53. // 数组是从0开始的,所以左节点的序号为父节点的两倍多一个,右节点为父节点的两倍多两个
  54. root.left = createFullBinaryTree(root.left, o, 2 * index + 1);
  55. root.right = createFullBinaryTree(root.right, o, 2 * index + 2);
  56. return root;
  57. }
  58. /**
  59. * 前序遍历输出数组
  60. *
  61. * @return E[]
  62. */
  63. public E[] preorderTraversal(Node<E> node) {
  64. List<E> list = new ArrayList<E>();
  65. preorderTraversalRealize(node,list);
  66. @SuppressWarnings("unchecked")
  67. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  68. return list.toArray(e);
  69. }
  70. /**
  71. * 前序遍历输出实现
  72. *
  73. * @param node
  74. */
  75. private void preorderTraversalRealize(Node<E> node,List<E> list) {
  76. if (node != null) {
  77. list.add(node.date);
  78. preorderTraversalRealize(node.left,list);
  79. preorderTraversalRealize(node.right,list);
  80. }
  81. }
  82. /**
  83. * 前序遍历输出数组(非递归实现)
  84. *
  85. * @return E[]
  86. */
  87. public E[] preorderTraversalNoRecursion(Node<E> node) {
  88. if(node == null)
  89. return null;
  90. @SuppressWarnings("unchecked")
  91. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  92. Stack<Node<E>> stack = new Stack<Node<E>>();
  93. List<E> list = new ArrayList<E>();
  94. stack.push(node);
  95. while(!stack.empty()){
  96. node = stack.pop();
  97. list.add(node.date);
  98. if(node.right != null){
  99. stack.push(node.right);
  100. }
  101. if(node.left != null){
  102. stack.push(node.left);
  103. }
  104. }
  105. return list.toArray(e);
  106. }
  107. /**
  108. * 中序遍历输出数组
  109. *
  110. * @return E[]
  111. */
  112. public E[] inorderTraversal(Node<E> node) {
  113. List<E> list = new ArrayList<E>();
  114. inorderTraversalRealize(node,list);
  115. @SuppressWarnings("unchecked")
  116. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  117. return list.toArray(e);
  118. }
  119. /**
  120. * 中序遍历输出实现
  121. *
  122. * @param node
  123. */
  124. private void inorderTraversalRealize(Node<E> node,List<E> list) {
  125. if (node != null) {
  126. inorderTraversalRealize(node.left,list);
  127. list.add(node.date);
  128. inorderTraversalRealize(node.right,list);
  129. }
  130. }
  131. /**
  132. * 中序遍历输出,非递归实现
  133. * @return
  134. */
  135. public E[] inorderTraversalNoRecursion(Node<E> node){
  136. if(node == null)
  137. return null;
  138. @SuppressWarnings("unchecked")
  139. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  140. Stack<Node<E>> stack = new Stack<Node<E>>();
  141. List<E> list = new ArrayList<E>();
  142. while(node != null || !stack.empty()){
  143. //存在左子树时
  144. while(node != null){
  145. stack.push(node);
  146. node = node.left;
  147. }
  148. //栈非空时
  149. if(!stack.empty()){
  150. node = stack.pop();
  151. list.add(node.date);
  152. node = node.right;
  153. }
  154. }
  155. return list.toArray(e);
  156. }
  157. /**
  158. * 后序遍历输出数组
  159. *
  160. * @return E[]
  161. */
  162. public E[] postorderTraversal(Node<E> node) {
  163. List<E> list = new ArrayList<E>();
  164. postorderTraversalRealize(node,list);
  165. @SuppressWarnings("unchecked")
  166. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  167. return list.toArray(e);
  168. }
  169. /**
  170. * 后序遍历输出实现
  171. *
  172. * @param node
  173. */
  174. private void postorderTraversalRealize(Node<E> node,List<E> list) {
  175. if (node != null) {
  176. postorderTraversalRealize(node.left,list);
  177. postorderTraversalRealize(node.right,list);
  178. list.add(node.date);
  179. }
  180. }
  181. /**
  182. * 后续遍历(非递归输出)
  183. * @param node
  184. * @return
  185. */
  186. public E[] postorderTraversalNoRecursion(Node<E> node){
  187. if(node == null)
  188. return null;
  189. @SuppressWarnings("unchecked")
  190. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  191. Stack<Node<E>> stack = new Stack<Node<E>>();
  192. List<E> list = new ArrayList<E>();
  193. Node<E> prv = node; //记录之前遍历的右结点
  194. while(node != null || !stack.empty()){
  195. //存在左子树时
  196. while(node != null){
  197. stack.push(node);
  198. node = node.left;
  199. }
  200. //栈非空时
  201. if(!stack.empty()){
  202. Node<E> nodeRight = stack.peek().right;
  203. /*如果右结点为空,或者右结点之前遍历过,获取根结点数据*/
  204. if(nodeRight == null || nodeRight == prv ){
  205. node = stack.pop();
  206. list.add(node.date);
  207. prv = node;
  208. node = null;
  209. }else{
  210. node = nodeRight;
  211. }
  212. }
  213. }
  214. return list.toArray(e);
  215. }
  216. /**
  217. * 广度优先搜索(分层遍历二叉树): 使用队列实现。队列初始化,将根节点压入队列。当队列不为空,
  218. * 进行如下操作:弹出一个节点,访问,若左子节点或右子节点不为空,将其压入队列。
  219. *
  220. * @param node
  221. * @return
  222. */
  223. public E[] layerTraversing(Node<E> node) {
  224. List<E> list = new ArrayList<E>();
  225. layerTraversingRealize(node, list);
  226. @SuppressWarnings("unchecked")
  227. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  228. return list.toArray(e);
  229. }
  230. /**
  231. * 分层遍历辅助函数
  232. * @param node
  233. * @param list
  234. */
  235. private void layerTraversingRealize(Node<E> node, List<E> list) {
  236. Queue<Node<E>> queue = new Queue<Node<E>>();
  237. queue.add(node);
  238. while (!queue.empty()) {
  239. Node<E> n = queue.poll();
  240. list.add(n.date);
  241. if (n.left != null) {
  242. queue.add(n.left);
  243. }
  244. if (n.right != null) {
  245. queue.add(n.right);
  246. }
  247. }
  248. }
  249. /**
  250. * 使用深度优先搜索遍历二叉树,这个结果和前序遍历是一样的
  251. *
  252. * @param node
  253. * @return
  254. */
  255. public E[] depthTraversing(Node<E> node) {
  256. List<E> list = new ArrayList<E>();
  257. depthTraversingRealize(node, list);
  258. @SuppressWarnings("unchecked")
  259. E[] e = (E[])Array.newInstance(node.date.getClass(), 0);//使用泛型数组进行记录
  260. return list.toArray(e);
  261. }
  262. private void depthTraversingRealize(Node<E> node, List<E> list) {
  263. if (node != null) {
  264. list.add(node.date);
  265. depthTraversingRealize(node.left, list);
  266. depthTraversingRealize(node.right, list);
  267. }
  268. }
  269. /**
  270. * 计算二叉树节点的个数
  271. *
  272. * @param node
  273. * @return 二叉树节点的个数
  274. */
  275. public int getNodeNumber(Node<E> node) {
  276. if (node == null) {
  277. return 0;
  278. }
  279. return getNodeNumber(node.left) + getNodeNumber(node.right) + 1;
  280. }
  281. /**
  282. * 求二叉树深度
  283. *
  284. * @return 二叉树深度
  285. */
  286. public int getDepth(Node<E> node) {
  287. if (node == null) {
  288. return 0;
  289. }
  290. int leftDepth = getDepth(node.left);
  291. int rightDepth = getDepth(node.right);
  292. return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;
  293. }
  294. /**
  295. * 求二叉树第K层的节点个数 递归解法: (1)如果二叉树为空或者k<1返回0 (2)如果二叉树不为空并且k==1,返回1
  296. * (3)如果二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树k-1层节点个数之和
  297. *
  298. * @param k
  299. * @return 二叉树第K层的节点个数
  300. */
  301. public int getNodeNumberInLay(Node<E> node, int k) {
  302. if (node == null || k < 1) {
  303. return 0;
  304. }
  305. if (k == 1) {
  306. return 1;
  307. }
  308. int leftNodeNum = getNodeNumberInLay(node.left, k - 1);
  309. int rightNodeNum = getNodeNumberInLay(node.right, k - 1);
  310. return leftNodeNum + rightNodeNum;
  311. }
  312. /**
  313. * 求二叉树中叶子节点的个数
  314. *
  315. * @param node
  316. * @return
  317. */
  318. public int getNodeNumberLeaf(Node<E> node) {
  319. if (node == null) {
  320. return 0;
  321. }
  322. if (node.left == null && node.right == null) {
  323. return 1;
  324. }
  325. int leftNodeNum = getNodeNumberLeaf(node.left);
  326. int rightNodeNum = getNodeNumberLeaf(node.right);
  327. return leftNodeNum + rightNodeNum;
  328. }
  329. /**
  330. * 判断两棵二叉树是否结构相同 不考虑数据内容。结构相同意味着对应的左子树和对应的右子树都结构相同。 递归解法: (1)如果两棵二叉树都为空,返回真
  331. * (2)如果两棵二叉树一棵为空,另一棵不为空,返回假 (3)如果两棵二叉树都不为空,如果对应的左子树和右子树都同构返回真,其他返回假
  332. *
  333. * @param node1
  334. * @param node2
  335. * @return
  336. */
  337. public boolean isStructureCmp(Node<E> node1, Node<E> node2) {
  338. if (node1 == null && node2 == null) {
  339. return true;
  340. } else if (node1 == null || node2 == null) {
  341. return false;
  342. } else {
  343. boolean leftCmp = isStructureCmp(node1.left, node2.left);
  344. boolean rightCmp = isStructureCmp(node1.right, node2.right);
  345. return leftCmp && rightCmp;
  346. }
  347. }
  348. /**
  349. * 判断是否是平衡二叉树
  350. *
  351. * @param node
  352. * @return
  353. */
  354. public boolean isAVL(Node<E> node) {
  355. if (node == null) {
  356. return true;
  357. }
  358. int leftHeight = getDepth(node.left);
  359. int rightHeight = getDepth(node.right);
  360. if (Math.abs(leftHeight - rightHeight) > 1) {
  361. return false;
  362. } else {
  363. return isAVL(node.left) && isAVL(node.right);
  364. }
  365. }
  366. /**
  367. * 判断是否完全二叉树 1.当发现有一个节点的左子树为空,右子树不为空时 直接返回false.
  368. * 2.当发现有一个节点的左子树不为空,右子树为空时,置标志位为1。 3.当发现有一个节点的左右子树均为空时,置标志位为1。
  369. *
  370. * @param node
  371. * @return
  372. */
  373. public boolean isCompleteBinaryTree(Node<E> node) {
  374. if (node == null) {
  375. return true;
  376. }
  377. Queue<Node<E>> queue = new Queue<Node<E>>();
  378. queue.add(node);
  379. int flag = 0;// 标记此节点以下的节点均应为叶子节点(没有左右孩子),否则此树为一棵非完全二叉树。
  380. while (!queue.empty()) {
  381. Node<E> n = queue.poll();
  382. if (n.left != null) {
  383. if (flag == 1) {
  384. return false;
  385. }
  386. queue.add(n.left);
  387. if (n.right != null) {
  388. queue.add(n.right);
  389. } else {
  390. flag = 1;
  391. }
  392. } else {
  393. if (n.right != null) {
  394. return false;
  395. }
  396. flag = 1;
  397. }
  398. }
  399. return true;
  400. }
  401. /**
  402. * 根据前序遍历结果和中序遍历结果重建二叉树
  403. *
  404. * @param preorderTraversalArray
  405. * 前序遍历结果
  406. * @param inorderTraversalArray
  407. * 中序便利结果
  408. * @return 二叉树
  409. */
  410. public BinaryTree<E> reBuildBinaryTree(E[] preorderTraversalArray, E[] inorderTraversalArray) {
  411. if (preorderTraversalArray == null || inorderTraversalArray == null) {
  412. return null;
  413. }
  414. Node<E> root = reBuildBinaryTreeRealize(preorderTraversalArray, 0, preorderTraversalArray.length - 1,
  415. inorderTraversalArray, 0, inorderTraversalArray.length - 1);
  416. BinaryTree<E> bt = new BinaryTree<E>();
  417. bt.rootNode = root;
  418. bt.depth = getDepth(root);
  419. return bt;
  420. }
  421. /**
  422. * 前序遍历的第一个节点一定是二叉树的根节点(以a记录),中序遍历以a为分界线,a左边的一定是左子树一边的(记录下左边的个数为x),且为左子树的中序遍历结果,
  423. * a右边的一定是右子树一边的(记录下右边的个数为y),且为右子树中序遍历的结果。再把前序遍历结果a后面x个数作为左子树的前序遍历,剩下的y个作为右子树的前序遍历,
  424. * 再一次进行递归建立,直到完全建立二叉树
  425. *
  426. * @param preOrder 前序遍历
  427. * @param startPreIndex 前序遍历起始位置
  428. * @param endPreIndex 前序遍历结束为止
  429. * @param inOrder 后序遍历
  430. * @param startInIndex 后续遍历起始位置
  431. * @param endInIndex 后序遍历结束位置
  432. * @return
  433. */
  434. public Node<E> reBuildBinaryTreeRealize(E[] preOrder, int startPreIndex, int endPreIndex, E[] inOrder,
  435. int startInIndex, int endInIndex) {
  436. Node<E> root = new Node<E>(preOrder[startPreIndex]);
  437. // 只有一个元素
  438. if (startPreIndex == endPreIndex) {
  439. if (startInIndex == endInIndex && preOrder[startPreIndex] == inOrder[startInIndex]) {
  440. return root;
  441. } else {
  442. throw new RuntimeException("出错");
  443. }
  444. }
  445. // 在中序遍历中找到根结点的索引
  446. int rootInIndex = startInIndex;
  447. while (rootInIndex <= endInIndex && inOrder[rootInIndex] != preOrder[startPreIndex]) {
  448. ++rootInIndex;
  449. }
  450. if (rootInIndex == endInIndex && inOrder[rootInIndex] != preOrder[startPreIndex]) {
  451. throw new RuntimeException("出错");
  452. }
  453. int leftLength = rootInIndex - startInIndex;
  454. int leftPreOrderEndIndex = startPreIndex + leftLength;
  455. if (leftLength > 0) {
  456. // 构建左子树
  457. root.left = reBuildBinaryTreeRealize(preOrder, startPreIndex + 1, leftPreOrderEndIndex, inOrder, startInIndex,
  458. rootInIndex - 1);
  459. }
  460. if (leftLength < endPreIndex - startPreIndex) {
  461. // 右子树有元素,构建右子树
  462. root.right = reBuildBinaryTreeRealize(preOrder, leftPreOrderEndIndex + 1, endPreIndex, inOrder, rootInIndex + 1,
  463. endInIndex);
  464. }
  465. return root;
  466. }
  467. }

二、常见排序算法

  1. package com.cn;
  2. import java.util.Arrays;
  3. /**
  4. * 经典排序复习
  5. * @author lvbiao
  6. *
  7. */
  8. public class Sort {
  9. /**
  10. * 交换数组中两个指定位置的数据
  11. * @param array
  12. * @param i
  13. * @param j
  14. */
  15. private void swap(int[] array, int i, int j) {
  16. int var;
  17. var = array[i];
  18. array[i] = array[j];
  19. array[j] = var;
  20. }
  21. public void display(int[] array){
  22. System.out.println(Arrays.toString(array));
  23. }
  24. /**
  25. * 冒泡排序
  26. * @param array
  27. */
  28. public void bubbleSort(int[] array){
  29. for(int i = 0; i < array.length; i++){
  30. for(int j = array.length-1; j > i; j--){
  31. if(array[j-1] > array[j]){
  32. swap(array,j-1,j);
  33. }
  34. }
  35. }
  36. }
  37. /**
  38. * 选择排序
  39. * @param array
  40. */
  41. public void selectSort(int[] array){
  42. int min;
  43. for(int i = 0; i < array.length; i++){
  44. min = i;
  45. for(int j = i+1; j < array.length; j++){
  46. if(array[min] > array[j]){
  47. min = j;
  48. }
  49. }
  50. if(min != i){
  51. swap(array, i, min);
  52. }
  53. }
  54. }
  55. /**
  56. * 插入排序
  57. * @param array
  58. */
  59. public void insertSort(int[] array){
  60. int j;
  61. for(int i = 1; i < array.length; i ++){
  62. if(array[i] < array[i-1]){
  63. int var = array[i];
  64. j = i - 1;
  65. while(j >= 0 && var < array[j]){
  66. array[j+1] = array[j];
  67. j--;
  68. }
  69. array[j+1] = var;
  70. }
  71. }
  72. }
  73. /**
  74. * 希尔排序
  75. * @param array
  76. */
  77. public void shellSort(int[] array){
  78. int length = array.length;
  79. int j;
  80. do{
  81. length = length/3 + 1;
  82. for(int i = length; i < array.length; i ++){
  83. if(array[i] < array[i-length]){
  84. int var = array[i];
  85. j = i -length;
  86. while(j >= 0 && var < array[j]){
  87. array[j+length] = array[j];
  88. j-=length;
  89. }
  90. array[j+length] = var;
  91. }
  92. }
  93. }while(length > 1);
  94. }
  95. /**
  96. * 推排序
  97. * @param array
  98. */
  99. public void headSort(int[] array){
  100. //将整个数组构建成一个大顶堆
  101. for(int i = (array.length-1)/2; i >= 0; i--){
  102. headBig(array,i,array.length-1);
  103. }
  104. //将大顶堆树根上的数据与数组末尾的交换,再将数组除开最后一个再构成一个大顶堆,一次循环知道完成排序
  105. for(int i = array.length-1; i > 0; i--){
  106. swap(array,0,i);
  107. headBig(array,0,i-1);
  108. }
  109. }
  110. /**
  111. * 将array调整为一个大顶堆
  112. * @param array
  113. * @param i
  114. * @param j
  115. */
  116. private void headBig(int[] array, int start, int stop) {
  117. int i;
  118. int var = array[start];
  119. for(i = 2*start; i <= stop; i*=2){
  120. if(i+1 >= array.length){
  121. if(array[i/2] < array[i]){
  122. array[i/2] = array[i];
  123. start = i;
  124. break;
  125. }else{
  126. break;
  127. }
  128. }
  129. if(array[i] < array[i+1] && i < stop){
  130. ++i;
  131. }
  132. if(var >= array[i]){
  133. break;
  134. }
  135. array[start] = array[i];
  136. start = i;
  137. }
  138. array[start] = var;
  139. }
  140. /**
  141. * 归并排序
  142. * @param array
  143. */
  144. public void mergeSort(int[] array){
  145. int[] temp = new int[array.length];
  146. mSort(array, temp, 0, array.length-1);
  147. }
  148. //msort函数是将list数组进行分割,merge函数是将分割后的list数组进行排序在组合
  149. private void mSort(int[] list, int[] temp, int left, int right){
  150. if(left == right){
  151. return;
  152. }else{
  153. int mid = (left + right) / 2;
  154. mSort(list,temp,left,mid);
  155. mSort(list,temp,mid+1,right);
  156. merge(list,temp,left,mid+1,right);
  157. }
  158. }
  159. private void merge(int[] list,int[] temp,int left,int mid,int right){
  160. int j = 0;
  161. int leftTemp = left;
  162. int midTemp = mid - 1;
  163. int n = right - left + 1; //当前temp数组中数的个数
  164. //左右数组第一个数进行比较 把较小的数如到temp数组中
  165. while(left <= midTemp && mid <= right){
  166. if(list[left] < list[mid]){
  167. temp[j++] = list[left++];
  168. }else{
  169. temp[j++] = list[mid++];
  170. }
  171. }
  172. //如果左边的数比右边的数多,就将剩下的入到temp数组中 j
  173. while(left <= midTemp){
  174. temp[j++] = list[left++];
  175. }
  176. //如果右边打数比左边的数多,就将右边剩下的数加入到temp数组当中去
  177. while(mid <= right){
  178. temp[j++] = list[mid++];
  179. }
  180. //将得到的temp数组加到list数组中
  181. for(j = 0; j < n; j++){
  182. list[leftTemp+j] = temp[j];
  183. }
  184. }
  185. /**
  186. * 快速排序
  187. * @param array
  188. */
  189. public void quickSort(int[] array){
  190. qSort(array, 0, array.length-1);
  191. }
  192. private void qSort(int[] array, int left, int right) {
  193. if(left < right){
  194. int mid = partition(array, left, right);
  195. qSort(array, left, mid-1);
  196. qSort(array, mid+1, right);
  197. }
  198. }
  199. //返回一个关键字,使得这个关键字左边的数都比他小,右边的数都比它大
  200. private int partition(int[] array, int left, int right) {
  201. int result = array[left];
  202. while(left < right){
  203. while(left < right && array[right] >= result){
  204. right--;
  205. }
  206. swap(array, left, right);
  207. while(left < right && array[left] <= result){
  208. left++;
  209. }
  210. swap(array, left, right);
  211. }
  212. array[left] = result;
  213. return left;
  214. }
  215. }

三、java反射

  1. package com.cn.reflect;
  2. import java.lang.reflect.Constructor;
  3. import java.lang.reflect.Field;
  4. import java.lang.reflect.InvocationTargetException;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.Modifier;
  7. /**
  8. * java反射
  9. *
  10. * @author lvbiao
  11. *
  12. */
  13. public class TestReflect {
  14. public static void main(String[] args) {
  15. Class test = null;
  16. try {
  17. test = Class.forName("com.cn.reflect.Student");
  18. } catch (ClassNotFoundException e) {
  19. e.printStackTrace();
  20. }
  21. //获取完整类名
  22. System.out.println("------获取完整类名----------");
  23. System.out.println("完整类名:" + getMothodName(test));
  24. //无参构造实例化对象
  25. System.out.println("------无参构造实例化对象----------");
  26. Student stu1 = (Student) getObjectNoParameters(test);
  27. stu1.display();
  28. //通过其他构造函数实例化对象
  29. System.out.println("------通过其他构造函数实例化对象-----");
  30. Student stu2 = (Student) getObjectHaveParameters(test);
  31. stu2.display();
  32. //获取类全部属性信息
  33. System.out.println("------获取类全部属性信息----------");
  34. AttributeInfo(test);
  35. //获取全部方法信息
  36. System.out.println("------获取全部方法信息-----------");
  37. MethodInfo(test);
  38. //调用类中无参方法
  39. System.out.println("------调用类中无参方法-----------");
  40. callMethodNoParameters(test,"display");
  41. //调用类中有参方法
  42. System.out.println("------调用类中有参方法-----------");
  43. callMethodHaveParameters(test,"say","hello");
  44. //通过反射来操作属性
  45. System.out.println("------通过反射来操作属性,设置名字为Bob-----------");
  46. setAttribute(test,"name","Bob");
  47. }
  48. // 获取完整类名
  49. public static String getMothodName(Class cla) {
  50. return cla.getName();
  51. }
  52. // 通过无参构造 实例化对象
  53. public static Object getObjectNoParameters(Class cla) {
  54. try {
  55. return cla.newInstance();
  56. } catch (InstantiationException e) {
  57. e.printStackTrace();
  58. } catch (IllegalAccessException e) {
  59. e.printStackTrace();
  60. }
  61. return null;
  62. }
  63. // 通过有参构造 实例化对象
  64. public static Object getObjectHaveParameters(Class cla) {
  65. Constructor<?> cons[] = cla.getConstructors();
  66. Object obj = null;
  67. try {
  68. obj = cons[1].newInstance("张三",23);
  69. } catch (InstantiationException e) {
  70. e.printStackTrace();
  71. } catch (IllegalAccessException e) {
  72. e.printStackTrace();
  73. } catch (IllegalArgumentException e) {
  74. e.printStackTrace();
  75. } catch (InvocationTargetException e) {
  76. e.printStackTrace();
  77. }
  78. return obj;
  79. }
  80. //获取类全部属性信息
  81. public static void AttributeInfo(Class cla){
  82. Field[] field = cla.getDeclaredFields();
  83. for (int i = 0; i < field.length; i++) {
  84. // 权限修饰符
  85. int mo = field[i].getModifiers();
  86. String priv = Modifier.toString(mo);
  87. // 属性类型
  88. Class<?> type = field[i].getType();
  89. System.out.println(priv + " " + type.getName() + " "
  90. + field[i].getName() + ";");
  91. }
  92. }
  93. //获取类中方法
  94. public static void MethodInfo(Class cla){
  95. Method[] method = cla.getDeclaredMethods();
  96. for (int i = 0; i < method.length; i++) {
  97. // 权限修饰符
  98. int mo = method[i].getModifiers();
  99. String priv = Modifier.toString(mo);
  100. //获取返回类型
  101. String retu = method[i].getReturnType().getName();
  102. // 属性类型
  103. String name = method[i].getName();
  104. System.out.println(priv + " " + retu + " "
  105. + name + "()");
  106. }
  107. }
  108. //调用无参方法
  109. public static void callMethodNoParameters(Class cla,String methodName){
  110. try {
  111. Method method1 = cla.getMethod(methodName);
  112. method1.invoke(cla.newInstance());
  113. } catch (NoSuchMethodException e) {
  114. e.printStackTrace();
  115. } catch (SecurityException e) {
  116. e.printStackTrace();
  117. } catch (IllegalAccessException e) {
  118. e.printStackTrace();
  119. } catch (IllegalArgumentException e) {
  120. e.printStackTrace();
  121. } catch (InvocationTargetException e) {
  122. e.printStackTrace();
  123. } catch (InstantiationException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. //调用有参方法
  128. public static void callMethodHaveParameters(Class cla,String methodName,String parameter){
  129. try {
  130. Method method2=cla.getMethod(methodName, String.class);
  131. method2.invoke(cla.newInstance(),parameter);
  132. } catch (NoSuchMethodException e) {
  133. e.printStackTrace();
  134. } catch (SecurityException e) {
  135. e.printStackTrace();
  136. } catch (IllegalAccessException e) {
  137. e.printStackTrace();
  138. } catch (IllegalArgumentException e) {
  139. e.printStackTrace();
  140. } catch (InvocationTargetException e) {
  141. e.printStackTrace();
  142. } catch (InstantiationException e) {
  143. e.printStackTrace();
  144. }
  145. }
  146. //通过反射来操作属性
  147. public static void setAttribute(Class cla,String key,String value){
  148. try {
  149. Object obj = cla.newInstance();
  150. Field field = cla.getDeclaredField(key);
  151. field.setAccessible(true);
  152. field.set(obj,value);
  153. //调用方法来验证是否设置成功
  154. Method method = cla.getMethod("display");
  155. method.invoke(obj);
  156. } catch (InstantiationException e) {
  157. e.printStackTrace();
  158. } catch (IllegalAccessException e) {
  159. e.printStackTrace();
  160. } catch (NoSuchFieldException e) {
  161. e.printStackTrace();
  162. } catch (SecurityException e) {
  163. e.printStackTrace();
  164. } catch (NoSuchMethodException e) {
  165. e.printStackTrace();
  166. } catch (IllegalArgumentException e) {
  167. e.printStackTrace();
  168. } catch (InvocationTargetException e) {
  169. e.printStackTrace();
  170. }
  171. }
  172. }

四、DOM4J读取xml文件

xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <四大名著>
  3. <西游记 id="x001">
  4. <作者>
  5. <姓名>吴承恩</姓名>
  6. <性别>男</性别>
  7. </作者>
  8. <作者>吴承恩2</作者>
  9. <作者>吴承恩3</作者>
  10. <朝代>明朝</朝代>
  11. </西游记>
  12. <红楼梦 id="x002">
  13. <作者>曹雪芹</作者>
  14. </红楼梦>
  15. </四大名著>

解析文件

  1. package com.cn.dom4j;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStreamWriter;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import org.dom4j.Attribute;
  8. import org.dom4j.Document;
  9. import org.dom4j.Element;
  10. import org.dom4j.io.OutputFormat;
  11. import org.dom4j.io.SAXReader;
  12. import org.dom4j.io.XMLWriter;
  13. import org.junit.Test;
  14. public class Dom4jTest{
  15. @Test
  16. public void test() throws Exception {
  17. // 创建saxReader对象
  18. SAXReader reader = new SAXReader();
  19. // 通过read方法读取一个文件 转换成Document对象
  20. Document document = reader.read(new File("src/com/cn/dom4j/sida.xml"));
  21. //获取根节点元素对象
  22. Element node = document.getRootElement();
  23. //遍历所有的元素节点
  24. listNodes(node);
  25. //介绍Element中的element方法和elements方法的使用
  26. elementMethod(node);
  27. // 获取四大名著元素节点中,子节点名称为红楼梦元素节点。
  28. Element element = node.element("红楼梦");
  29. //获取element的id属性节点对象
  30. Attribute attr = element.attribute("id");
  31. //删除属性
  32. element.remove(attr);
  33. //添加新的属性
  34. element.addAttribute("name", "作者");
  35. // 在红楼梦元素节点中添加朝代元素的节点
  36. Element newElement = element.addElement("朝代");
  37. newElement.setText("清朝");
  38. //获取element中的作者元素节点对象
  39. Element author = element.element("作者");
  40. //删除元素节点
  41. boolean flag = element.remove(author);
  42. //返回true代码删除成功,否则失败
  43. System.out.println(flag);
  44. //添加CDATA区域
  45. element.addCDATA("红楼梦,是一部爱情小说.");
  46. // 写入到一个新的文件中
  47. writer(document);
  48. }
  49. /**
  50. * 把document对象写入新的文件
  51. *
  52. * @param document
  53. * @throws Exception
  54. */
  55. public void writer(Document document) throws Exception {
  56. // 紧凑的格式
  57. // OutputFormat format = OutputFormat.createCompactFormat();
  58. // 排版缩进的格式
  59. OutputFormat format = OutputFormat.createPrettyPrint();
  60. // 设置编码
  61. format.setEncoding("UTF-8");
  62. // 创建XMLWriter对象,指定了写出文件及编码格式
  63. // XMLWriter writer = new XMLWriter(new FileWriter(new
  64. // File("src//a.xml")),format);
  65. XMLWriter writer = new XMLWriter(new OutputStreamWriter(
  66. new FileOutputStream(new File("src/com/cn/dom4j/newsida.xml")), "UTF-8"), format);
  67. // 写入
  68. writer.write(document);
  69. // 立即写入
  70. writer.flush();
  71. // 关闭操作
  72. writer.close();
  73. }
  74. /**
  75. * 遍历当前节点元素下面的所有(元素的)子节点
  76. *
  77. * @param node
  78. */
  79. public void listNodes(Element node) {
  80. System.out.println("当前节点的名称:" + node.getName());
  81. // 获取当前节点的所有属性节点
  82. List<Attribute> list = node.attributes();
  83. // 遍历属性节点
  84. for (Attribute attr : list) {
  85. System.out.println(attr.getText() + "-----" + attr.getName()
  86. + "---" + attr.getValue());
  87. }
  88. if (!(node.getTextTrim().equals(""))) {
  89. System.out.println("文本内容:" + node.getText());
  90. }
  91. // 当前节点下面子节点迭代器
  92. Iterator<Element> it = node.elementIterator();
  93. // 遍历
  94. while (it.hasNext()) {
  95. // 获取某个子节点对象
  96. Element e = it.next();
  97. // 对子节点进行遍历
  98. listNodes(e);
  99. }
  100. }
  101. /**
  102. * 介绍Element中的element方法和elements方法的使用
  103. *
  104. * @param node
  105. */
  106. public void elementMethod(Element node) {
  107. // 获取node节点中,子节点的元素名称为西游记的元素节点。
  108. Element e = node.element("西游记");
  109. // 获取西游记元素节点中,子节点为作者的元素节点(可以看到只能获取第一个作者元素节点)
  110. Element author = e.element("作者");
  111. System.out.println(e.getName() + "----" + author.getText());
  112. // 获取西游记这个元素节点 中,所有子节点名称为作者元素的节点 。
  113. List<Element> authors = e.elements("作者");
  114. for (Element aut : authors) {
  115. System.out.println(aut.getText());
  116. }
  117. // 获取西游记这个元素节点 所有元素的子节点。
  118. List<Element> elements = e.elements();
  119. for (Element el : elements) {
  120. System.out.println(el.getText());
  121. }
  122. }
  123. }
  1. package com.cn.dom4j;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.OutputStreamWriter;
  5. import org.dom4j.Document;
  6. import org.dom4j.DocumentHelper;
  7. import org.dom4j.Element;
  8. import org.dom4j.io.OutputFormat;
  9. import org.dom4j.io.SAXReader;
  10. import org.dom4j.io.XMLWriter;
  11. import org.junit.Test;
  12. public class Dom4jTest2 {
  13. @Test
  14. public void test() throws Exception {
  15. // 创建saxreader对象
  16. SAXReader reader = new SAXReader();
  17. // 读取一个文件,把这个文件转换成Document对象
  18. Document document = reader.read(new File("src/com/cn/dom4j/sida.xml"));
  19. // 获取根元素
  20. Element root = document.getRootElement();
  21. // 把文档转换字符串
  22. String docXmlText = document.asXML();
  23. System.out.println(docXmlText);
  24. System.out.println("---------------------------");
  25. // csdn元素标签根转换的内容
  26. String rootXmlText = root.asXML();
  27. System.out.println(rootXmlText);
  28. System.out.println("---------------------------");
  29. // 获取java元素标签 内的内容
  30. Element e = root.element("西游记");
  31. System.out.println(e.asXML());
  32. System.out.println("---------------------------");
  33. }
  34. /**
  35. * 创建一个document对象 往document对象中添加节点元素 转存为xml文件
  36. *
  37. * @throws Exception
  38. */
  39. @Test
  40. public void test2() throws Exception {
  41. Document document = DocumentHelper.createDocument();// 创建根节点
  42. Element root = document.addElement("csdn");
  43. Element java = root.addElement("java");
  44. java.setText("java班");
  45. Element ios = root.addElement("ios");
  46. ios.setText("ios班");
  47. writer(document);
  48. }
  49. /**
  50. * 把一个文本字符串转换Document对象
  51. *
  52. * @throws Exception
  53. */
  54. @Test
  55. public void test1() throws Exception {
  56. String text = "<csdn><java>Java班</java><net>Net班</net></csdn>";
  57. Document document = DocumentHelper.parseText(text);
  58. Element e = document.getRootElement();
  59. System.out.println(e.getName());
  60. writer(document);
  61. }
  62. /**
  63. * 把document对象写入新的文件
  64. *
  65. * @param document
  66. * @throws Exception
  67. */
  68. public void writer(Document document) throws Exception {
  69. // 紧凑的格式
  70. // OutputFormat format = OutputFormat.createCompactFormat();
  71. // 排版缩进的格式
  72. OutputFormat format = OutputFormat.createPrettyPrint();
  73. // 设置编码
  74. format.setEncoding("UTF-8");
  75. // 创建XMLWriter对象,指定了写出文件及编码格式
  76. // XMLWriter writer = new XMLWriter(new FileWriter(new
  77. // File("src//a.xml")),format);
  78. XMLWriter writer = new XMLWriter(new OutputStreamWriter(
  79. new FileOutputStream(new File("src/com/cn/dom4j/newsida2.xml")), "UTF-8"), format);
  80. // 写入
  81. writer.write(document);
  82. // 立即写入
  83. writer.flush();
  84. // 关闭操作
  85. writer.close();
  86. }
  87. }
  1. package com.cn.dom4j;
  2. import java.io.File;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import org.dom4j.Attribute;
  6. import org.dom4j.Document;
  7. import org.dom4j.DocumentException;
  8. import org.dom4j.Element;
  9. import org.dom4j.io.SAXReader;
  10. public class MyTest {
  11. public static void main(String[] args) {
  12. try {
  13. // 创建saxReader对象
  14. SAXReader reader = new SAXReader();
  15. // 通过read方法读取一个文件转换成Document对象
  16. Document document = reader.read(new File("src/com/cn/dom4j/sida.xml"));
  17. //获取根节点元素对象
  18. Element node = document.getRootElement();
  19. listNodes(node);
  20. } catch (DocumentException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. private static void listNodes(Element node) {
  25. //遍历所有元素节点
  26. System.out.println("++++++当前节点名称:" + node.getName() + "+++++++++++");
  27. //获取当前节点所有属性节点
  28. List<Attribute> list = node.attributes();
  29. //遍历属性节点
  30. for(Attribute attr : list){
  31. System.out.println(attr.getText() + " " + attr.getName() +
  32. ":" + attr.getValue() + " " + attr.getPath() +
  33. " " + attr.getStringValue());
  34. }
  35. if(!(node.getTextTrim().equals(""))) {
  36. System.out.println("文本内容:" + node.getText());
  37. }
  38. //当前节点下面子节点迭代器
  39. Iterator<Element> it = node.elementIterator();
  40. //遍历
  41. while (it.hasNext()) {
  42. //获取某个子节点对象
  43. Element e = it.next();
  44. //对子节点进行遍历
  45. listNodes(e);
  46. }
  47. }
  48. }

五、nio

  1. package com.cn.nio;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.nio.ByteBuffer;
  6. import java.nio.channels.FileChannel;
  7. public class WriteFileNio {
  8. public static void main(String[] args){
  9. try {
  10. //写入文件
  11. String str = "I am a boy.";
  12. FileChannel fc = new FileOutputStream("E:/1.txt").getChannel();
  13. fc.write(ByteBuffer.wrap(str.getBytes()));
  14. fc.close();
  15. //读取文件
  16. FileChannel fcin = new FileInputStream("E:/1.txt").getChannel();
  17. ByteBuffer bb = ByteBuffer.allocate(1024);
  18. fcin.read(bb);
  19. bb.flip();
  20. while(bb.hasRemaining()){
  21. System.out.print((char)bb.get());
  22. }
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注