• 【Day27】输入输出流一


    目录

    一、缓冲流的使用

    二、File类的使用

    三、其它流的使用


    一、缓冲流的使用

    /**
     * 处理流之一:缓冲流的使用
     * BufferedInputStream
     * BufferedOutputStream
     * BufferedReader
     * BufferedWriter
     * 2、作用:提供流的读取,写入速度
     *      提高读写速度的原因,内部提供了一个缓冲区
     *
     * 3、处理流,就是套在已有的流的基础上
     *
     */
    1. public class Bufferedtest {
    2. @Test
    3. public void BufferedInputStreamTest(){
    4. long stast=System.currentTimeMillis();
    5. //1、造文件
    6. File file = new File("花花.png");
    7. File file1 = new File("花花2.png");
    8. //2、造流
    9. //2.1造节点流
    10. FileInputStream fi=null;
    11. FileOutputStream fo=null;
    12. BufferedInputStream bd=null;
    13. BufferedOutputStream bo=null;
    14. try {
    15. fi=new FileInputStream(file);
    16. fo=new FileOutputStream(file1);
    17. //2.2造缓冲流
    18. bd=new BufferedInputStream(fi);
    19. bo=new BufferedOutputStream(fo);
    20. //3、赋值的细节:读取与写入
    21. byte[] bytes=new byte[5];
    22. int len;
    23. while ((len=bd.read(bytes))!=-1){
    24. bo.write(bytes,0,len);
    25. }
    26. System.out.println("完成");
    27. } catch (FileNotFoundException e) {
    28. e.printStackTrace();
    29. } catch (IOException e) {
    30. e.printStackTrace();
    31. }finally {
    32. /*
    33. 内层流可以省略,因为关闭外层流的同时,内层流也会关闭
    34. */
    35. if(bo!=null){
    36. try {
    37. bo.close();
    38. } catch (IOException e) {
    39. e.printStackTrace();
    40. }
    41. }if(bd!=null){
    42. try {
    43. bd.close();
    44. } catch (IOException e) {
    45. e.printStackTrace();
    46. }
    47. }if(fo!=null){
    48. try {
    49. fo.close();
    50. } catch (IOException e) {
    51. e.printStackTrace();
    52. }
    53. }if(fi!=null){
    54. try {
    55. fi.close();
    56. } catch (IOException e) {
    57. e.printStackTrace();
    58. }
    59. }
    60. }
    61. long end=System.currentTimeMillis();
    62. System.out.println("方式一所用时间为:"+(end-stast));//42
    63. }
    64. public void copyFile(String str1,String str2){
    65. //1、造文件
    66. File file = new File(str1);
    67. File file1 = new File(str2);
    68. //2、造流
    69. //2.1造节点流
    70. FileInputStream fi=null;
    71. FileOutputStream fo=null;
    72. BufferedInputStream bd=null;
    73. BufferedOutputStream bo=null;
    74. try {
    75. fi=new FileInputStream(file);
    76. fo=new FileOutputStream(file1);
    77. //2.2造缓冲流
    78. bd=new BufferedInputStream(fi);
    79. bo=new BufferedOutputStream(fo);
    80. //3、赋值的细节:读取与写入
    81. byte[] bytes=new byte[5];
    82. int len;
    83. while ((len=bd.read(bytes))!=-1){
    84. bo.write(bytes,0,len);
    85. }
    86. System.out.println("完成");
    87. } catch (FileNotFoundException e) {
    88. e.printStackTrace();
    89. } catch (IOException e) {
    90. e.printStackTrace();
    91. }finally {
    92. /*
    93. 内层流可以省略,因为关闭外层流的同时,内层流也会关闭
    94. */
    95. if(bo!=null){
    96. try {
    97. bo.close();
    98. } catch (IOException e) {
    99. e.printStackTrace();
    100. }
    101. }if(bd!=null){
    102. try {
    103. bd.close();
    104. } catch (IOException e) {
    105. e.printStackTrace();
    106. }
    107. }if(fo!=null){
    108. try {
    109. fo.close();
    110. } catch (IOException e) {
    111. e.printStackTrace();
    112. }
    113. }if(fi!=null){
    114. try {
    115. fi.close();
    116. } catch (IOException e) {
    117. e.printStackTrace();
    118. }
    119. }
    120. }
    121. }
    122. @Test
    123. public void runFile(){
    124. long staet=System.currentTimeMillis();
    125. String str1="花花.png";
    126. String str2="花花3.png";
    127. copyFile(str1,str2);
    128. long end =System.currentTimeMillis();
    129. System.out.println("方式二所有时间为:"+(end-staet));//37
    130. }
    131. @Test
    132. public void BufferedReaderWriter(){
    133. BufferedReader bufferedReader =null;
    134. BufferedWriter bufferedWriter =null;
    135. try {
    136. bufferedReader = new BufferedReader(new FileReader(new File("hello.txt")));
    137. bufferedWriter = new BufferedWriter(new FileWriter(new File("hello3.txt")));
    138. //方式一
    139. char[] chars=new char[5];
    140. int len;
    141. while ((len=bufferedReader.read(chars))!=-1){
    142. bufferedWriter.write(chars,0,len);
    143. bufferedWriter.flush();
    144. }
    145. //方式二:
    146. // String str;
    147. // while ((str=bufferedReader.readLine())!=null){
    148. // bufferedWriter.write(str);
    149. // bufferedWriter.newLine();
    150. // }
    151. } catch (FileNotFoundException e) {
    152. e.printStackTrace();
    153. } catch (IOException e) {
    154. e.printStackTrace();
    155. }finally {
    156. if (bufferedReader!=null){
    157. try {
    158. bufferedReader.close();
    159. } catch (IOException e) {
    160. e.printStackTrace();
    161. }
    162. }
    163. if (bufferedWriter!=null){
    164. try {
    165. bufferedWriter.close();
    166. } catch (IOException e) {
    167. e.printStackTrace();
    168. }
    169. }
    170. }
    171. }
    172. }

    二、File类的使用

    /**
     * File类的使用:
     *
     * 1、File类的一个对象,代表一个文件或一个文件目录(俗称:文件夹)
     * 2、File类声明在Java.io包下
     * 3、File类中涉及到关于文件或文件目录的创建、删除、重命名、修改时间、文件大小等方法
     *      并未涉及到写入或读取文件内容的操作,如果需要读取或写入文件内容,必须使用IO流来完成
     * 4、后续File类的对象常会作为参数传递到流的构造器中,指明读取或写入的“终点”
     *
     */
    1. public class FileTest1 {
    2. //1、创建File类的实例
    3. //File(String filepath)
    4. //File(String parentPath,String childPath)
    5. //File(File parentFile,String childPath)
    6. //2、相对路径:
    7. // 绝对路径:包含目录
    8. @Test
    9. public void test(){
    10. //构造器1
    11. File file=new File("hello.txt");//相对路径
    12. File file1=new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");//绝对路径
    13. System.out.println(file);
    14. System.out.println(file1);
    15. //构造方法
    16. //构造器2
    17. File file2=new File("D:\\WenJian\\IDEA\\Java高级基础部分","Day08输入输出流");
    18. System.out.println(file2);
    19. //构造器3
    20. File file3 = new File(file2, "hello.txt");
    21. System.out.println(file3);
    22. }
    23. @Test
    24. public void test1(){
    25. /*
    26.  public String getAbsolutePath():获取绝对路径
    27.  public String getPath() :获取路径
    28.  public String getName() :获取名称
    29.  public String getParent():获取上层文件目录路径。若无,返回null
    30.  public long length() :获取文件长度(即:字节数)。不能获取目录的长度。
    31.  public long lastModified() :获取最后一次的修改时间,毫秒值
    32.  public String[] list() :获取指定目录下的所有文件或者文件目录的名称数组
    33.  public File[] listFiles() :获取指定目录下的所有文件或者文件目录的File数组
    34. */
    35. File file=new File("hello.txt");
    36. File file1 = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\hello.txt");
    37. System.out.println(file.getAbsolutePath());
    38. System.out.println(file.getPath());
    39. System.out.println(file.getName());
    40. System.out.println(file.getParent());
    41. System.out.println(file.length());
    42. System.out.println(file.lastModified());
    43. System.out.println(file.list());
    44. System.out.println(file.listFiles());
    45. System.out.println();
    46. //file1
    47. System.out.println(file1.getAbsolutePath());
    48. System.out.println(file1.getPath());
    49. System.out.println(file1.getName());
    50. System.out.println(file1.getParent());
    51. System.out.println(file1.length());
    52. System.out.println(file1.lastModified());
    53. System.out.println(file1.list());
    54. System.out.println(file1.listFiles());
    55. }
    56. @Test
    57. public void test2(){
    58. File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分");
    59. String[] lists=file.list();
    60. for (String s: lists) {
    61. System.out.println(s);
    62. }
    63. //含有绝对路径的输出
    64. File[] fileps=file.listFiles();
    65. for (File file1:fileps
    66. ) {
    67. System.out.println(file1);
    68. }
    69. }
    70. /*
    71. public boolean renameTo(File dest):把文件重命名为指定的文件路径
    72. 比如:file1.renameTo(file2):
    73. 要想保证返回true,需要file1在硬盘中是存在的,且file2不能再硬盘中存在
    74. */
    75. @Test
    76. public void test3(){
    77. File file1 = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");
    78. File file2=new File("D:\\WenJian\\IDEA\\fghj\\aa.txt");
    79. boolean b = file2.renameTo(file1);
    80. System.out.println(b);
    81. }
    82. /*
    83.  public boolean isDirectory():判断是否是文件目录
    84.  public boolean isFile() :判断是否是文件
    85.  public boolean exists() :判断是否存在
    86.  public boolean canRead() :判断是否可读
    87.  public boolean canWrite() :判断是否可写
    88.  public boolean isHidden() :判断是否隐藏
    89. */
    90. @Test
    91. public void test4(){
    92. File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");
    93. System.out.println(file.isDirectory());
    94. System.out.println(file.isFile());
    95. System.out.println(file.exists());
    96. System.out.println(file.canRead());
    97. System.out.println(file.canWrite());
    98. System.out.println(file.isHidden());
    99. System.out.println();
    100. File file1 = new File("D:\\WenJian\\IDEA");
    101. System.out.println(file1.isDirectory());
    102. System.out.println(file1.isFile());
    103. System.out.println(file1.exists());
    104. System.out.println(file1.canRead());
    105. }
    106. /*
    107.  public boolean createNewFile() :创建文件。若文件存在,则不创建,返回false
    108.  public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。
    109. 如果此文件目录的上层目录不存在,也不创建。
    110.  public boolean mkdirs() :创建文件目录。如果上层文件目录不存在,一并创建
    111. 注意事项:如果你创建文件或者文件目录没有写盘符路径,那么,默认在项目
    112. 路径下。
    113.  public boolean delete():删除文件或者文件夹
    114. 删除注意事项:
    115. Java中的删除不走回收站。
    116. 要删除一个文件目录,请注意该文件目录内不能包含文件或者文件目录
    117. */
    118. @Test
    119. public void test5() throws IOException {
    120. File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello1.txt");
    121. if(!file.exists()){
    122. file.createNewFile();
    123. System.out.println("创建成功");
    124. }else {
    125. file.delete();
    126. System.out.println("删除成功");
    127. }
    128. }
    129. // D:\WenJian\IDEA\fghj
    130. // public boolean mkdir() :创建文件目录。如果此文件目录存在,就不创建了。
    131. //如果此文件目录的上层目录不存在,也不创建。
    132. @Test
    133. public void test6(){
    134. File file1 = new File("D:\\WenJian\\IDEA\\fghj");
    135. File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分\\Day08输入输出流\\src\\File\\hello.txt");
    136. if (!file1.exists()) {
    137. file1.mkdir();
    138. System.out.println("创建成功");
    139. }else {
    140. file1.delete();
    141. System.out.println("删除成功");
    142. }
    143. file.delete();
    144. }
    145. /**
    146. *
    147. * @description:
    148. * @author ----千里之行,始于足下----
    149. * @date 2022/8/22 18:59
    150. */
    151. @Test
    152. public void test7() throws IOException {
    153. File file = new File("D:\\hello.txt");
    154. if (!file.exists()) {
    155. file.createNewFile();
    156. }
    157. File file1 = new File(file.getParent(), "hello.txt");
    158. System.out.println(file);
    159. file.delete();
    160. }
    161. @Test
    162. public void test8(){
    163. File file = new File("D:\\WenJian\\IDEA\\Java高级基础部分");
    164. String[] files=file.list();
    165. for (String fle :
    166. files) {
    167. int length = fle.length();
    168. // if(fle.substring(length-3).equals("jpg")){
    169. if(fle.endsWith("jpg")){
    170. System.out.println(fle);
    171. }
    172. }
    173. }
    174. }

    三、其它流的使用

    /**
     * 1.1、标准的输入输出流
     *     System.in:标准的输入流,默认从键盘输入
     *     System.out:标准的输出流,默认从控制台输出
     *     1.2
     *     System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定输入输出的流
     *
     *     练习:
     *         从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续
     *         进行输入操作,直至当输入“e”或者“exit”时,退出程序。
     *         
     * @description:  
     * @author ----千里之行,始于足下----
     * @date 2022/9/16 20:25
     */
    1. public class FileTest2 {
    2. public static void main(String[] args) {
    3. BufferedReader br = null;
    4. try {
    5. InputStreamReader isr = new InputStreamReader(System.in);
    6. br = new BufferedReader(isr);
    7. while (true) {
    8. System.out.println("请输入字字符串");
    9. String data = null;
    10. data = br.readLine();
    11. if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
    12. System.out.println("程序结束");
    13. break;
    14. }
    15. String upperCase = data.toUpperCase();
    16. System.out.println(upperCase);
    17. }
    18. }catch(IOException e){
    19. e.printStackTrace();
    20. }finally{
    21. if (br != null) {
    22. try {
    23. br.close();
    24. } catch (IOException e) {
    25. e.printStackTrace();
    26. }
    27. }
    28. }
    29. }
    30. }

  • 相关阅读:
    最大路径和
    C语言-入门-const关键字(十九)
    尚医通 (十七) --------- 数据字典开发
    人工智能——大白话熟悉目标检测基本流程
    程序调试技巧
    保证接口数据安全的10种方案
    内存卡剪切后怎么恢复
    Perl 脚本运行时提示:Can‘t locate Win32/OLE.pm in @INC
    Linux基本权限管理
    【LTTng】核心概念精读
  • 原文地址:https://blog.csdn.net/fool_Java/article/details/126897134