• 13-本地文件操作


    一.文件的增删改查

    View Code

    二.文件夹的增删改查

    View Code

    三.文件属性的读取

     

    四.文件属性设置

    五.遍历文件夹

    六.文件的简单读写

     1 package com.example;
     2 
     3 
     4 import java.io.BufferedReader;
     5 import java.io.BufferedWriter;
     6 import java.io.File;
     7 import java.io.FileInputStream;
     8 import java.io.FileNotFoundException;
     9 import java.io.FileOutputStream;
    10 import java.io.IOException;
    11 import java.io.InputStreamReader;
    12 import java.io.OutputStreamWriter;
    13 import java.io.UnsupportedEncodingException;
    14 
    15 public class MyClass {
    16     public static void main(String []args){
    17         File testFile = new File("testFile");
    18         if (testFile.exists()){
    19             try {
    20                 FileInputStream fis = new FileInputStream(testFile);
    21                 InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
    22                 BufferedReader br = new BufferedReader(isr);
    23 
    24                 String line;
    25                 while ((line = br.readLine())!= null){
    26                     System.out.println(line);
    27                 }
    28 
    29                 br.close();
    30                 isr.close();
    31                 fis.close();
    32 
    33             } catch (FileNotFoundException e) {
    34                 e.printStackTrace();
    35             } catch (UnsupportedEncodingException e) {
    36                 e.printStackTrace();
    37             } catch (IOException e) {
    38                 e.printStackTrace();
    39             }
    40 
    41         }
    42         else {
    43             try {
    44                 testFile.createNewFile();
    45             } catch (IOException e) {
    46                 e.printStackTrace();
    47             }
    48             FileOutputStream fos = null;
    49             try {
    50                 fos = new FileOutputStream(testFile);
    51                 OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");
    52                 BufferedWriter bw = new BufferedWriter(osw);
    53 
    54                 bw.write("h
    ");
    55                 bw.write("e
    ");
    56                 bw.write("l
    ");
    57                 bw.write("l
    ");
    58                 bw.write("o
    ");
    59 
    60                 bw.close();
    61                 osw.close();
    62                 fos.close();
    63             } catch (FileNotFoundException e) {
    64                 e.printStackTrace();
    65             } catch (UnsupportedEncodingException e) {
    66                 e.printStackTrace();
    67             } catch (IOException e) {
    68                 e.printStackTrace();
    69             }
    70 
    71 
    72         }
    73     }
    74 }
    View Code
  • 相关阅读:
    接口自动化(三)--读取json文件中的数据
    接口自动化(二)--操作Excel获取需要数据
    接口自动化(一)--概述
    pycharm(2016.3.2版本)导入工程文件执行程序时弹出Edit configuration
    使用Fiddler实现网络限速
    fiddler工具条、状态栏、请求信息栏各按钮的作用
    修改elementUI源码新增组件/修改组件
    DRF框架的基本组件
    django之原生SQL操作封装
    jqtree使用说明
  • 原文地址:https://www.cnblogs.com/BelieveFish/p/6371847.html
Copyright © 2020-2023  润新知