zoukankan      html  css  js  c++  java
  • java IO

    一:阐述

    在之前的联系的例子,数据都在内存级别,当程序运行结束之后,数据也随之丢失。保持数据的持久性,无非写入硬盘。之前学习python的时候,我们将数据写入文件,后期从性能考虑写入数据库(redis也是数据库)。

    今天我们来学习写入文件操作。

    二:File类。

    File类的构造器:

    1 Constructor and Description
    2 File(File parent, String child)
    3 Creates a new File instance from a parent abstract pathname and a child pathname string.
    4 File(String pathname)
    5 Creates a new File instance by converting the given pathname string into an abstract pathname.
    6 File(String parent, String child)
    7 Creates a new File instance from a parent pathname string and a child pathname string.

     不同的构造器,生成不同的File对象。

     1 package test13;
     2 
     3 import java.io.File;
     4 
     5 public class File_Demo {
     6     public static  void main(String...args){
     7         fileDemo();
     8     }
     9     public static void fileDemo(){
    10         File  file=new File("c:\","abv");
    11         System.out.print(file+"
    ");
    12         File file_1=new File("c:\a\b");
    13         System.out.print(file_1+"
    ");
    14         File file_2=new File(file_1,"uu");
    15         System.out.print(file_2+"
    ");
    16     }
    17 }

     

    三:File类的静态常量(目录连接符和切割符号):

    1 Modifier and Type     Field and Description
    2 static String     pathSeparator
    3 The system-dependent path-separator character, represented as a string for convenience.
    4 static char     pathSeparatorChar
    5 The system-dependent path-separator character.
    6 static String     separator
    7 The system-dependent default name-separator character, represented as a string for convenience.
    8 static char     separatorChar
    9 The system-dependent default name-separator character.
     1 package test13;
     2 
     3 import java.io.File;
     4 
     5 public class File_Demo {
     6     public static  void main(String...args){
     7         fileDemo();
     8     }
     9     public static void fileDemo(){
    10         String  sep= File.pathSeparator;
    11         String  sep_pat=File.separator;
    12         System.out.print(sep+"
    ");
    13         System.out.print(sep_pat);
    14     }
    15 }

     

    因为java语言可以跨平台,所以,因为在不同的操作系统,目录的划分符号不一样,在windows中是双引号;在linux中是单引号,不同的目录连接符也不一样,在windows中在linux中/所以我们在程序,如果平台不确定性,则不要写死这些。

    四:File的常用方法:

     1 package test13;
     2 
     3 
     4 import java.io.File;
     5 import java.io.IOException;
     6 
     7 public class File_Demo {
     8     public static  void main(String...args){
     9         try{
    10             fileDemo();
    11         }catch (IOException ex){
    12             System.out.print(ex);
    13         }
    14 
    15     }
    16     public static void fileDemo() throws IOException {
    17         File  file=new File("c:\","abv");
    18         File  file_1=new File("c:\d\f\g","abv");//递归创建。需要注意的是:window是"\",其中一个是转义符.
    19         File  file_2=new File("c:\d\f\g","cc");//递归创建。需要注意的是:window是"\",其中一个是转义符.
    20         file.mkdirs();//创建目录包括递归目录.
    21         file_2.createNewFile();//创建对应的目录下的文件.
    22         file_1.mkdirs();//创建递归目录.
    23         if(file.exists()){// 判断目录和文件是否存在。返回布尔值。
    24             System.out.print(222);
    25         }
    26         file.delete();//删除.
    27 
    28     }
    29 }

     其中需要注意的是在使用方法:createFile()异常的捕获 要不报错,因为该方法是本身就throws异常。

    1    public boolean createNewFile() throws IOException {
    2         SecurityManager security = System.getSecurityManager();
    3         if (security != null) security.checkWrite(path);
    4         if (isInvalid()) {
    5             throw new IOException("Invalid file path");
    6         }
    7         return fs.createFileExclusively(path);
    8     }
  • 相关阅读:
    Haskell Interactive Development in Emacs
    Access Java API in Groovy Script
    手工设置Eclipse文本编辑器的配色
    Color Theme of Emacs
    Gnucash的投资记录
    Special Forms and Syntax Sugars in Clojure
    Use w3m as Web Browser
    SSE指令集加速之 I420转BGR24
    【图像处理】 增加程序速度的方法
    TBB 入门笔记
  • 原文地址:https://www.cnblogs.com/evilliu/p/7890523.html
Copyright © 2011-2022 走看看