1. package test0411;
    2. import java.io.*;
    3. import java.util.ArrayList;
    4. import java.util.Iterator;
    5. import java.util.List;
    6. import java.util.Scanner;
    7. public class demo5 {
    8. public static void main(String[] args) {
    9. // 1.获取文件
    10. FileInputStream fis = null;
    11. FileOutputStream fos = null;
    12. try {
    13. fis = new FileInputStream("D:\\phonenumber.txt");
    14. fos = new FileOutputStream("D:\\phonenumber.txt",true);
    15. // add(fos);
    16. // delete(fis);
    17. // change(fis);
    18. // search(fis);
    19. } catch (IOException e) {
    20. e.printStackTrace();
    21. } finally {
    22. if (fis != null) {
    23. try {
    24. fis.close();
    25. } catch (IOException e) {
    26. e.printStackTrace();
    27. }
    28. }
    29. if (fos != null) {
    30. try {
    31. fos.close();
    32. } catch (IOException e) {
    33. e.printStackTrace();
    34. }
    35. }
    36. if (fos != null) {
    37. try {
    38. fos.flush();
    39. } catch (IOException e) {
    40. e.printStackTrace();
    41. }
    42. }
    43. }
    44. // 2.查功能
    45. // 3.增
    46. // 4.改
    47. // 5.删
    48. }
    49. private static void change(FileInputStream fis) throws IOException {
    50. String name = "张三";
    51. List list = new ArrayList();
    52. Scanner sc = new Scanner(fis);
    53. while (sc.hasNext()){
    54. list.add(sc.next());
    55. }
    56. int i=0;
    57. Object s = null;
    58. String newnum = "9998";
    59. while (i<list.size()){
    60. s = list.get(i);
    61. if (s.toString().equals(name)){
    62. list.set(i+1,newnum);
    63. break;
    64. }
    65. i+=1;
    66. }
    67. FileWriter fw = new FileWriter("D:\\phonenumber.txt");
    68. while(i<list.size()){
    69. String str = list.get(i).toString()+" "+list.get(i+1)+"\n";
    70. System.out.println(str);
    71. fw.write(str);
    72. i+=2;
    73. }
    74. fw.flush();
    75. fw.close();
    76. }
    77. private static void search(FileInputStream fis) {
    78. String name = "张三";
    79. List list = new ArrayList();
    80. Scanner sc = new Scanner(fis);
    81. while (sc.hasNext()){
    82. list.add(sc.next());
    83. }
    84. int i=0;
    85. Object s = null;
    86. while (i<list.size()){
    87. s = list.get(i);
    88. if (s.toString().equals(name)){
    89. System.out.println(list.get(i+1));
    90. break;
    91. }
    92. i+=1;
    93. }
    94. }
    95. private static boolean delete(FileInputStream fis) throws IOException {
    96. String name = "李四";
    97. boolean flat = false;
    98. List list = new ArrayList();
    99. Scanner sc = new Scanner(fis);
    100. while (sc.hasNext()){
    101. list.add(sc.next());
    102. }
    103. System.out.println(list);
    104. int i=0;
    105. Object s = null;
    106. while (i<list.size()){
    107. s = list.get(i);
    108. if (s.toString().equals(name)){
    109. list.remove(i);
    110. list.remove(i);
    111. break;
    112. }
    113. i+=1;
    114. }
    115. System.out.println(list);
    116. i=0;
    117. FileWriter fw = new FileWriter("D:\\phonenumber.txt");
    118. while(i<list.size()){
    119. String str = list.get(i).toString()+" "+list.get(i+1)+"\n";
    120. System.out.println(str);
    121. fw.write(str);
    122. i+=2;
    123. }
    124. fw.flush();
    125. fw.close();
    126. return flat;
    127. }
    128. private static void add(FileOutputStream fos) throws IOException {
    129. StringBuilder inf = new StringBuilder("");
    130. String name = "赵六";
    131. String num = "888888";
    132. inf.append("\n");
    133. inf.append(name);
    134. inf.append(" ");
    135. inf.append(num);
    136. fos.write(inf.toString().getBytes());
    137. }
    138. }