[关闭]
@nextleaf 2018-08-20T13:52:14.000000Z 字数 15541 阅读 588

2018-08-20 工作日志

Java 工作日志 泛型 反射


上午

泛型

泛型与Map、ArrayList

Map中的键值对的类型是Entry

  1. package com.nl.sx817.generics.map;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Set;
  5. public class MapGenerics {
  6. public static void main(String[] args){
  7. Map<String,Integer> map=new HashMap<String,Integer>();
  8. map.put("A",1);
  9. map.put("B",2);
  10. map.put("C",3);
  11. map.put("D",4);
  12. //map中的键值对的类型是Entry
  13. Set<Map.Entry<String,Integer>> set=map.entrySet();
  14. for (Map.Entry<String,Integer> o:set){
  15. System.out.println(o.getKey()+"-----------"+o.getValue());
  16. }
  17. }
  18. }

Arrays工具类

compareTo()

自然顺序

  1. package com.nl.sx816.CollectionFramework.ArraysUtil;
  2. import com.nl.sx807.HeapStackPractice.Student;
  3. import java.util.Arrays;
  4. /**
  5. * Created with IntelliJ IDEA 2018.
  6. * Description: Arrays工具类
  7. *
  8. * @author: 黄昭鸿
  9. * @date: 2018-08-20
  10. * Time: 9:43
  11. */
  12. public class TestArrays {
  13. public static void main(String[] args) {
  14. //sort 将指定的数组按升序排序,可指定范围。
  15. //asList,返回由指定数组支持的固定大小的列表
  16. //List<String> stooges = Arrays.asList("Larry","Moe","Curly");
  17. // binarySearch使用二进制搜索算法搜索指定值的数组的范围
  18. //copyOfRange将指定数组的指定范围复制到新数组中
  19. //fill 将值分配给数组的指定范围的每个元素。
  20. //parallelSort(int[] a, int fromIndex, int toIndex) 将指定的数组范围按数字升序排序。
  21. //parallelSort(T[] a, Comparator<? super T> cmp) //根据指定比较器引发的顺序对指定的对象数组进行排序。
  22. //parallelSort(T[] a, int fromIndex, int toIndex) //根据元素的自然顺序(compareTo(T o)),将指定对象数组的指定范围按升序 排序。
  23. //spliterator(T[] array, int startInclusive, int endExclusive) 返回Spliterator覆盖指定数组的指定范围。
  24. int[] arrs = {12, 23, 45, 56, 78, 89, 34, 45, 56, 67, 89, 98};
  25. print(arrs);
  26. //对数组排序
  27. Arrays.sort(arrs);
  28. print(arrs);
  29. Student[] students = {
  30. new Student("张三丰", "1", 105),
  31. new Student("里斯", "2", 46),
  32. new Student("王武", "3", 99),
  33. new Student("赵莉丰", "4", 13)
  34. };
  35. for (Student s : students) {
  36. System.out.println(s);
  37. }
  38. //当需要对某个类的对象进行排序时,该类需要实现Comparable<T>接口 必须重写compareTo方法
  39. Arrays.sort(students);
  40. System.out.println();
  41. for (Student s : students) {
  42. System.out.println(s);
  43. }
  44. }
  45. public static void print(int[] ints) {
  46. for (int i : ints) {
  47. System.out.print(i + " ");
  48. }
  49. System.out.println();
  50. }
  51. }

反射

取得Class对象的方式

类对象的使用

通过 Class对象获取成员变量、成员方法、接口、超类、构造方法等:

getName():获得类的完整名字。
getFields():获得类的public类型的属性。
getDeclaredFields():获得类的所有属性。包括private 声明的和继承类
getMethods():获得类的public类型的方法。
getDeclaredMethods():获得类的所有方法。包括private 声明的和继承类
getMethod(String name, Class[] parameterTypes):获得类的特定方法,name参数指定方法的名字,parameterTypes参数指定方法的参数类型。
getConstructors():获得类的public类型的构造方法。
getConstructor(Class[] parameterTypes):获得类的特定构造方法,parameterTypes参数指定构造方法的参数类型。
newInstance():通过类的不带参数的构造方法创建这个类的一个对象。

  1. package com.nl.sx820.reflection;
  2. import cn.hutool.core.lang.Console;
  3. import com.nl.sx813.abstractclass.Person;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.TypeVariable;
  7. /**
  8. * Created with IntelliJ IDEA 2018.
  9. * Description: 反射
  10. * Java反射就是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;并且能改变它的属性。
  11. * 而这也是Java被视为动态(或准动态,一般而言的动态语言定义是程序运行时,允许改变程序结构或变量类型)语言的一个关键性质
  12. *
  13. * 取得Class对象的方式
  14. *
  15. * @author: 黄昭鸿
  16. * @date: 2018-08-20
  17. * Time: 10:53
  18. */
  19. public class ReflexDemo {
  20. public static void main(String[] args) {
  21. Class<?> c1 = null;
  22. Class<?> c2 = null;
  23. Class<?> c3 = null;
  24. Class<?> c4 = null;
  25. //取得Class对象的方式
  26. //方式一 getClass 获得Person的类对象
  27. Person person1 = new Person("孙兰芳", 16, '女');
  28. c1 = person1.getClass();
  29. //方式二,static method Class.forName()获得Person的类对象
  30. try {
  31. c2 = Class.forName("com.nl.sx813.abstractclass.Person");
  32. } catch (ClassNotFoundException e) {
  33. e.printStackTrace();
  34. }
  35. //方式三.class 获得Person的类对象
  36. c3 = Person.class;
  37. //方式四 获得类对象,如果时Java封装对象,使用TYPE语法
  38. c4 = Integer.TYPE;
  39. System.out.println(c1.getName() + "---" + c2.getName() + "---" + c3.getName() + "---" + c4.getName());
  40. //获得所有公有构造方法
  41. System.out.println("所有公有构造方法");
  42. for (Constructor<?> constructor : c1.getConstructors()) {
  43. System.out.println(constructor.toGenericString());
  44. }
  45. //获得所有公有方法
  46. System.out.println("所有公有方法");
  47. for (Method method : c3.getMethods()) {
  48. System.out.println(method.toGenericString());
  49. }
  50. }
  51. }

附:

  1. package com.nl.sx820.reflection;
  2. import cn.hutool.core.lang.Console;
  3. import com.nl.sx813.abstractclass.Person;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.Method;
  6. import java.lang.reflect.TypeVariable;
  7. /**
  8. * Created with IntelliJ IDEA 2018.
  9. * Description: 反射
  10. * Java反射就是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方法和属性;并且能改变它的属性。
  11. * 而这也是Java被视为动态(或准动态,一般而言的动态语言定义是程序运行时,允许改变程序结构或变量类型)语言的一个关键性质
  12. * 每个类都会产生一个对应的Class对象,Class对象仅在需要的时候才会加载
  13. *
  14. * 通过Class对象访问这个类的所有属性和方法
  15. * 取得Class对象的方式
  16. *
  17. * @author: 黄昭鸿
  18. * @date: 2018-08-20
  19. * Time: 10:53
  20. */
  21. public class ReflexDemo {
  22. public static void main(String[] args) {
  23. Class<?> c1 = null;
  24. Class<?> c2 = null;
  25. Class<?> c3 = null;
  26. Class<?> c4 = null;
  27. //取得Class对象的方式
  28. //方式一 getClass 获得Person的类对象
  29. Person person1 = new Person("孙兰芳", 16, '女');
  30. c1 = person1.getClass();
  31. //方式二,static method Class.forName()获得Person的类对象
  32. try {
  33. c2 = Class.forName("com.nl.sx813.abstractclass.Person");
  34. } catch (ClassNotFoundException e) {
  35. e.printStackTrace();
  36. }
  37. //方式三.class 获得Person的类对象
  38. //使用”.class”来创建Class对象的引用时,不会自动初始化该Class对象(例如不会初始化静态块),使用forName()会自动初始化该Class对象
  39. c3 = Person.class;
  40. //方式四 获得类对象,如果时Java封装对象,使用TYPE语法
  41. c4 = Integer.TYPE;
  42. System.out.println(c1.getName() + "---" + c2.getName() + "---" + c3.getName() + "---" + c4.getName());
  43. //获得所有公有构造方法
  44. System.out.println("所有公有构造方法");
  45. for (Constructor<?> constructor : c1.getConstructors()) {
  46. System.out.println(constructor.toGenericString());
  47. }
  48. //获得所有公有方法
  49. System.out.println("所有公有方法");
  50. for (Method method : c3.getMethods()) {
  51. System.out.println(method.toGenericString());
  52. }
  53. /*
  54. * getName():获得类的完整名字。
  55. * getFields():获得类的public类型的属性。
  56. * getDeclaredFields():获得类的所有属性。包括private 声明的和继承类
  57. * getMethods():获得类的public类型的方法。
  58. * getDeclaredMethods():获得类的所有方法。包括private 声明的和继承类
  59. * getMethod(String name, Class[] parameterTypes):获得类的特定方法,name参数指定方法的名字,parameterTypes参数指定方法的参数类型。
  60. * getConstructors():获得类的public类型的构造方法。
  61. * getConstructor(Class[] parameterTypes):获得类的特定构造方法,parameterTypes参数指定构造方法的参数类型。
  62. * newInstance():通过类的不带参数的构造方法创建这个类的一个对象。
  63. */
  64. }
  65. }

下午

Java 流(Stream)、文件(File)和IO

按流的角色不同,分为 节点流(对应文件),处理流。
按操作数据单位不同,分为 字节流(8bit),字符流。
IO流

File

创建文件夹以及文件

  1. package com.nl.sx820.io;
  2. import cn.hutool.core.lang.Console;
  3. import java.io.File;
  4. import java.io.IOException;
  5. /**
  6. * Created with IntelliJ IDEA 2018.
  7. * Description:文件
  8. * 创建目录
  9. * 创建文件
  10. *
  11. * @author: 黄昭鸿
  12. * @date: 2018-08-20
  13. * Time: 14:53
  14. */
  15. public class FileDemo {
  16. public static void main(String[] args) {
  17. //创建目录
  18. File parent = new File("E:" + File.separator + "Downloads" + File.separator + "java创建的文件夹");
  19. parent.mkdir();
  20. //创建文件
  21. try {
  22. new File(parent + File.separator + "通用分隔符(File.separator).txt").createNewFile();
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. createNewFileTest();
  27. }
  28. public static void createNewFileTest() {
  29. //在工作中用到通用分隔符File.separator
  30. File parent2 = new File("E:" + File.separator + "Downloads" + File.separator + "java创建的文件夹2Test" + File.separator + "子文件夹");
  31. File child = new File(parent2, "子File.txt");
  32. try {
  33. if (!parent2.exists()) {
  34. //parent.mkdir()只创建一个文件夹,parent.mkdirs()可创建多个文件夹
  35. parent2.mkdirs();
  36. child.createNewFile();
  37. System.out.println("文件目录及文件创建成功");
  38. } else {
  39. child.createNewFile();
  40. System.out.println("文件创建成功");
  41. }
  42. } catch (IOException e) {
  43. e.printStackTrace();
  44. }
  45. showInfo(parent2);
  46. showListFiles(parent2);
  47. showInfo(child);
  48. showListFiles(child);
  49. //1秒后删除
  50. //timing(child);
  51. //重命名
  52. reName(child, "E:\\Downloads\\java创建的文件夹2Test\\子文件夹\\新的名字.txt");
  53. }
  54. public static void timing(File child) {
  55. for (int i = 0; i < 10; i++) {
  56. try {
  57. //线程休眠
  58. Thread.sleep(1600);
  59. } catch (InterruptedException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63. child.delete();
  64. }
  65. public static void showInfo(File f) {
  66. System.out.println("\n属性:");
  67. System.out.println("文件绝对路径:" + f.getAbsolutePath());
  68. System.out.println("文件名:" + f.getName());
  69. System.out.println("父目录的路径:" + f.getParent());
  70. System.out.println("是否时文件夹:" + f.isDirectory());
  71. System.out.println("是否时文件:" + f.isFile());
  72. System.out.println("文件长度(字节):" + f.length());
  73. System.out.println("是否隐藏:" + f.isHidden());
  74. System.out.println();
  75. }
  76. public static void showListFiles(File f) {
  77. if (f.exists()) {
  78. if (f.isDirectory()) {
  79. //System.out.println("包含的文件名列表:");
  80. //Console.log(f.list());
  81. System.out.println("[" + f.getAbsolutePath() + "]文件列表:");
  82. char str = ' ';
  83. System.out.println("文件名\t\t\t大小\t\t\t隐藏");
  84. for (File file : f.listFiles()) {
  85. if (file.isHidden()) {
  86. str = '✔';
  87. } else {
  88. str = ' ';
  89. }
  90. System.out.println(file.getName() + "\t\t" + file.length() + "字节\t\t" + str);
  91. }
  92. } else {
  93. System.out.println("不是一个目录...");
  94. }
  95. } else {
  96. System.out.println("路径不存在");
  97. }
  98. }
  99. /**
  100. * Description:
  101. * 重命名
  102. * @date 2018/8/20 18:30
  103. */
  104. public static boolean reName(File file, String name) {
  105. if (file.exists()) {
  106. if (file.isFile()) {
  107. System.out.println("新名:" + name);
  108. return file.renameTo(new File(name));
  109. } else {
  110. System.out.println("不是一个文件");
  111. }
  112. } else {
  113. System.out.println("路径不存在");
  114. return false;
  115. }
  116. return false;
  117. }
  118. }

FileInputStream流和 FileOutputStream流

FileInputStream

FileInputStream流用于从文件读取数据

FileOutputStream

该类用来创建一个文件并向文件中写数据。
如果在打开文件进行输出前,目标文件不存在,那么该流会创建该文件。

问题

  1. //如果文件不存在会自动新建
  2. OutputStream fileOutputStream = new FileOutputStream("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.hello");
  3. //默认为操作系统默认编码,windows上是gbk
  4. OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "UTF-8");
  5. //使用line.separator系统变量来换行,如下
  6. String lineSeparator = System.getProperty("line.separator");
  7. fileOutputStream.write(lineSeparator.getBytes());
  8. // 写入到缓冲区
  9. writer.append("中文输入");
  10. writer.append(stringBuffer);

同一个方法内(如果不在同一个方法内,未知),代码1会影响代码2,原因未知:

代码1:

  1. //从文件输入流一次读取一个字节
  2. System.out.println("从文件输入流一次读取一个字节");
  3. byte byteData;
  4. while ((byteData = (byte) fileInputStream.read()) != -1) {
  5. //System.out.print((char) byteData);
  6. }
  7. System.out.println();

代码2:

  1. //读取到StringBuffer中
  2. InputStreamReader reader = new InputStreamReader(fileInputStream, "UTF-8");
  3. //StringBuffer stringBuffer = new StringBuffer();
  4. while (reader.ready()) {
  5. // 转成char加到StringBuffer对象中
  6. stringBuffer.append((char) reader.read());
  7. }
  8. System.out.println(stringBuffer.toString());
  9. //关闭读取流
  10. reader.close();

要么

  1. package com.nl.sx820.io.stream;
  2. import java.io.*;
  3. public class stream {
  4. public static void main(String[] args) {
  5. StringBuffer stringBuffer = new StringBuffer();
  6. try {
  7. //创建一个输入流对象来读取文件:(还可以使用一个文件对象来创建一个输入流对象来读取文件)
  8. InputStream fileInputStream = new FileInputStream("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.txt");
  9. //从文件输入流一次读取一个字节
  10. System.out.println("从文件输入流一次读取一个字节");
  11. byte byteData;
  12. while ((byteData = (byte) fileInputStream.read()) != -1) {
  13. System.out.print((char) byteData);
  14. stringBuffer.append((char)byteData);
  15. }
  16. System.out.println();
  17. } catch (FileNotFoundException e) {
  18. e.printStackTrace();
  19. } catch (IOException e) {
  20. e.printStackTrace();
  21. }
  22. //使用字符串类型的文件名来创建一个输出流对象(也可以使用一个文件对象来创建一个输出流来写文件)
  23. try {
  24. //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("your output file path"));
  25. //如果文件不存在会自动新建
  26. OutputStream fileOutputStream = new FileOutputStream("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.hello");
  27. //默认为操作系统默认编码,windows上是gbk
  28. OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "UTF-8");
  29. //使用line.separator系统变量来换行,如下
  30. String lineSeparator = System.getProperty("line.separator");
  31. fileOutputStream.write(lineSeparator.getBytes());
  32. // 写入到缓冲区
  33. writer.append("中文输入");
  34. writer.append(stringBuffer);
  35. //System.out.println(stringBuffer);
  36. //换行
  37. writer.append("\r\n");
  38. // 刷新缓存冲,写入到文件,直接close也会写入
  39. writer.flush();
  40. // 关闭写入流,同时会把缓冲区内容写入文件
  41. writer.close();
  42. // 关闭输出流,释放系统资源
  43. fileOutputStream.close();
  44. } catch (FileNotFoundException e) {
  45. e.printStackTrace();
  46. } catch (UnsupportedEncodingException e) {
  47. e.printStackTrace();
  48. } catch (IOException e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. }

要么

  1. package com.nl.sx820.io.stream;
  2. import java.io.*;
  3. public class stream {
  4. public static void main(String[] args) {
  5. StringBuffer stringBuffer = new StringBuffer();
  6. try {
  7. //创建一个输入流对象来读取文件:(还可以使用一个文件对象来创建一个输入流对象来读取文件)
  8. InputStream fileInputStream = new FileInputStream("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.txt");
  9. //读取到StringBuffer中
  10. InputStreamReader reader = new InputStreamReader(fileInputStream, "UTF-8");
  11. //StringBuffer stringBuffer = new StringBuffer();
  12. while (reader.ready()) {
  13. // 转成char加到StringBuffer对象中
  14. stringBuffer.append((char) reader.read());
  15. }
  16. System.out.println("啦啦啦"+stringBuffer.toString());
  17. //关闭读取流
  18. reader.close();
  19. } catch (FileNotFoundException e) {
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. }
  24. //使用字符串类型的文件名来创建一个输出流对象(也可以使用一个文件对象来创建一个输出流来写文件)
  25. try {
  26. //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("your output file path"));
  27. //如果文件不存在会自动新建
  28. OutputStream fileOutputStream = new FileOutputStream("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.hello");
  29. //默认为操作系统默认编码,windows上是gbk
  30. OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, "UTF-8");
  31. //使用line.separator系统变量来换行,如下
  32. String lineSeparator = System.getProperty("line.separator");
  33. fileOutputStream.write(lineSeparator.getBytes());
  34. // 写入到缓冲区
  35. writer.append("中文输入");
  36. writer.append(stringBuffer);
  37. //System.out.println(stringBuffer);
  38. //换行
  39. writer.append("\r\n");
  40. // 刷新缓存冲,写入到文件,直接close也会写入
  41. writer.flush();
  42. // 关闭写入流,同时会把缓冲区内容写入文件
  43. writer.close();
  44. // 关闭输出流,释放系统资源
  45. fileOutputStream.close();
  46. } catch (FileNotFoundException e) {
  47. e.printStackTrace();
  48. } catch (UnsupportedEncodingException e) {
  49. e.printStackTrace();
  50. } catch (IOException e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. }

附:

  1. package com.nl.sx820.io.stream;
  2. import java.io.*;
  3. import java.nio.charset.StandardCharsets;
  4. /**
  5. * Created with IntelliJ IDEA 2018.
  6. * Description: 文件内容读取和写入
  7. *
  8. * @author: 黄昭鸿
  9. * @date: 2018-08-20
  10. * Time: 17:52
  11. */
  12. public class stream {
  13. public static void main(String[] args) {
  14. StringBuffer stringBuffer = new StringBuffer();
  15. try {
  16. //创建一个输入流对象来读取文件:(还可以使用一个文件对象来创建一个输入流对象来读取文件)
  17. InputStream fileInputStream = new FileInputStream("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.txt");
  18. //读取到StringBuffer中
  19. InputStreamReader reader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
  20. //StringBuffer stringBuffer = new StringBuffer();
  21. while (reader.ready()) {
  22. // 转成char加到StringBuffer对象中
  23. stringBuffer.append((char) reader.read());
  24. }
  25. System.out.println("啦啦啦" + stringBuffer.toString());
  26. //关闭读取流
  27. reader.close();
  28. /*
  29. 这里的代码也可以实现文件读取,但会影响上面的读取流代码,只能选其一使用
  30. //从文件输入流一次读取一个字节
  31. System.out.println("从文件输入流一次读取一个字节");
  32. byte byteData;
  33. while ((byteData = (byte) fileInputStream.read()) != -1) {
  34. System.out.print((char) byteData);
  35. }
  36. System.out.println();*/
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. //使用字符串类型的文件名来创建一个输出流对象(也可以使用一个文件对象来创建一个输出流来写文件)
  41. try {
  42. //BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("your output file path"));
  43. //如果文件不存在会自动新建
  44. OutputStream fileOutputStream = new FileOutputStream("E:" + File.separator + "Downloads" + File.separator + "新nextleafwin.txt");
  45. //默认为操作系统默认编码,windows上是gbk
  46. OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
  47. //使用line.separator系统变量来换行,如下
  48. String lineSeparator = System.getProperty("line.separator");
  49. fileOutputStream.write(lineSeparator.getBytes());
  50. // 写入到缓冲区
  51. writer.append("中文输入");
  52. writer.append(stringBuffer);
  53. //System.out.println(stringBuffer);
  54. //换行
  55. writer.append("\r\n");
  56. // 刷新缓存冲,写入到文件,直接close也会写入
  57. writer.flush();
  58. // 关闭写入流,同时会把缓冲区内容写入文件
  59. writer.close();
  60. // 关闭输出流,释放系统资源
  61. fileOutputStream.close();
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. System.out.println("-------------------------------------");
  66. File file = new File("E:" + File.separator + "Downloads" + File.separator + "nextleafwin.txt");
  67. File newFile = new File("E:" + File.separator + "Downloads" + File.separator + "NewNextleafwin.txt");
  68. rf(file);
  69. //如果文件不存在,FileWriter的write()方法会自动新建文件
  70. wf(newFile, "Java.io 包几乎包含了所有操作输入、输出需要的类。所有这些流类代表了输入源和输出目标。" +
  71. "Java.io 包中的流支持很多种格式,比如:基本类型、对象、本地化字符集等等。\n" + "一个流可以理解为一个数据的序列。输入流表示从一个源读取数据,输出流表示向一个目标写数据。");
  72. }
  73. //文件内容读取
  74. public static void rf(File file) {
  75. if (file.isFile()) {
  76. try {
  77. // 创建 FileReader 对象
  78. FileReader fileReader = new FileReader(file);
  79. System.out.println("读取的内容为:");
  80. /* byte byteData;
  81. while ((byteData = (byte) fileReader.read()) != -1) {
  82. System.out.print((char) byteData);
  83. }
  84. System.out.println();*/
  85. char[] a = new char[(int) file.length()];
  86. // 读取数组中的内容
  87. fileReader.read(a);
  88. for (char c : a) {
  89. System.out.print(c);
  90. }
  91. fileReader.close();
  92. } catch (IOException e) {
  93. e.printStackTrace();
  94. }
  95. } else {
  96. System.out.println("不是一个文件,读取??");
  97. }
  98. }
  99. //文件内容写入
  100. //如果文件不存在,FileWriter的write()方法会自动新建
  101. public static void wf(File file, String writeString) {
  102. try {
  103. if (file.exists()) {
  104. if (file.isDirectory()) {
  105. file = new File(file, File.separator + "aNewFile.txt");
  106. file.createNewFile();
  107. // creates a FileWriter Object
  108. FileWriter writer = new FileWriter(file);
  109. // 向文件写入内容
  110. System.out.println("开始写入到" + file.getAbsolutePath());
  111. writer.write(writeString);
  112. writer.flush();
  113. writer.close();
  114. } else if (file.isFile()) {
  115. //覆写
  116. System.out.println("覆写!");
  117. file.createNewFile();
  118. FileWriter writer = new FileWriter(file);
  119. System.out.println("开始写入到" + file.getAbsolutePath());
  120. writer.write(writeString);
  121. writer.flush();
  122. writer.close();
  123. }
  124. } else {
  125. //新建
  126. System.out.println("新建"+file.getAbsolutePath());
  127. file.createNewFile();
  128. FileWriter writer = new FileWriter(file);
  129. writer.write(writeString);
  130. writer.flush();
  131. writer.close();
  132. }
  133. } catch (IOException e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注