• 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
  • 相关阅读:
    线程共享全局变量和私有全局变量
    线程退出前操作
    Linux下线程pid和tid
    几种常见的光纤接头(ST,SC,LC,FC)以及PC、APC和UPC的区别
    Javascript对象及数组用法笔记
    Javascript对象及数组用法笔记
    程序员特有的9个坏习惯
    程序员特有的9个坏习惯
    程序人生:真正的效率源自专注
    程序人生:真正的效率源自专注
  • 原文地址:https://www.cnblogs.com/BelieveFish/p/6371847.html
Copyright © 2020-2023  润新知