Java中 IO操作

avatar 2017年10月21日17:09:27 5 1852 views
博主分享免费Java教学视频,B站账号:Java刘哥
本文主要介绍 Java IO 中几个常见的操作,读取字符,写入字符,复制文件,复制文件夹等。 直接上代码啦 因为读和写比较常用,尤其是按行读取,所以贴前面啦
  1. /**
  2.     * 从文件里读取内容
  3.     *
  4.     * @param filePath
  5.     * @return
  6.     */
  7.    private static String readFromFile(String filePath) {
  8.        // 使用ArrayList来存储每行读取到的字符串
  9.        StringBuilder sb = new StringBuilder();
  10.        try {
  11.            FileReader fr = new FileReader(filePath);
  12.            BufferedReader bf = new BufferedReader(fr);
  13.            String str;
  14.            // 按行读取字符串
  15.            while ((str = bf.readLine()) != null) {
  16.                sb.append(str + "\r\n");
  17.            }
  18.            bf.close();
  19.            fr.close();
  20.        } catch (IOException e) {
  21.            e.printStackTrace();
  22.        }
  23.        return sb.toString();
  24.    }
  25.    /**
  26.     * 往文件中写入内容
  27.     *
  28.     * @param outputFilePath 输出文件路径
  29.     * @param content
  30.     */
  31.    public static void writeToFile(String outputFilePath, String content) {
  32.        File file = new File(outputFilePath);
  33.        try {
  34.            FileWriter writer = new FileWriter(file);
  35.            writer.write(content);
  36.            writer.close();
  37.        } catch (IOException e) {
  38.            e.printStackTrace();
  39.        }
  40.    }
  其他的如下
  1. package com.liuyanzhao.file;
  2. import java.io.*;
  3. /**
  4.  * @author 言曌
  5.  * @date 2017/10/21 下午3:31
  6.  */
  7. public class Demo {
  8.     /**
  9.      * 创建文件
  10.      * @param file 文件
  11.      */
  12.     public static void createFile(File file) {
  13.         try {
  14.             file.createNewFile();
  15.         } catch (IOException e) {
  16.             e.printStackTrace();
  17.         }
  18.     }
  19.     /**
  20.      * 删除文件
  21.      * @param file 文件
  22.      */
  23.     public static void deleteFile(File file) {
  24.         file.delete();
  25.     }
  26.     /**
  27.      * 根据字节流读取文件内容
  28.      * @param file 文件
  29.      */
  30.     public static void readFileByInputStream(File file) {
  31.         try {
  32.             FileInputStream fis = new FileInputStream(file);
  33.             int length;
  34.             byte[] buf = new byte[1024];
  35.             while ((length = fis.read(buf)) != -1) {
  36.                 System.out.print(new String(buf, 0, length));
  37.             }
  38.             fis.close();
  39.         } catch (FileNotFoundException e) {
  40.             e.printStackTrace();
  41.         } catch (IOException e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.     /**
  46.      * 根据字符流读取文件内容
  47.      *
  48.      * @param file 文件
  49.      */
  50.     public static void readFileByReader(File file) {
  51.         try {
  52.             FileReader reader = new FileReader(file);
  53.             char[] buf = new char[1024];
  54.             int length;
  55.             while ((length = reader.read(buf)) != -1) {
  56.                 System.out.print(new String(buf, 0, length));
  57.             }
  58.         } catch (FileNotFoundException e) {
  59.             e.printStackTrace();
  60.         } catch (IOException e) {
  61.             e.printStackTrace();
  62.         }
  63.     }
  64.     /**
  65.      * 往文件中写入内容(直接覆盖)
  66.      * @param file 文件
  67.      * @param content
  68.      */
  69.     public static void writeByWriter(File file, String content) {
  70.         try {
  71.             FileWriter writer = new FileWriter(file);
  72.             writer.write(content);
  73.             writer.close();
  74.         } catch (IOException e) {
  75.             e.printStackTrace();
  76.         }
  77.     }
  78.     /**
  79.      * 往文件中写入内容(直接覆盖)
  80.      * @param file
  81.      * @param content
  82.      */
  83.     public static void writeByOutputStream(File file, String content) {
  84.         try {
  85.             FileOutputStream fos = new FileOutputStream(file);
  86.             fos.write(content.getBytes());
  87.             fos.close();
  88.         } catch (FileNotFoundException e) {
  89.             e.printStackTrace();
  90.         } catch (IOException e) {
  91.             e.printStackTrace();
  92.         }
  93.     }
  94.     public static void main(String args[]) {
  95. //        File file = new File("/Users/liuyanzhao/Desktop/demo.txt");
  96. ////        readFileByInputStream(file);
  97. ////        readFileByReader(file);
  98. ////        writeByOutputStream(file,"hello saysky");
  99. ////        writeByWriter(file,"hello java");
  100.         File file = new File("/Users/liuyanzhao/Desktop");
  101.         for(File file1: file.listFiles()) {
  102.             System.out.println("名称:"+file1.getName());
  103.             System.out.println("是否存在:"+file1.exists());
  104.             System.out.println("绝对路径:"+file1.getAbsolutePath());
  105.             System.out.println("父级目录:"+file1.getParent());
  106.             System.out.println("大小:"+file1.length()/1000+"kB");
  107.         }
  108.     }
  109. }
上面的代码只介绍了一些基本的简单的操作,以后遇到其他的再补充吧。最后再上一张结构图吧
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0