zoukankan      html  css  js  c++  java
  • java的IO详解

    1)操作文件的类File

    在整个IO包中,File是唯一与文件本身有关的类。

    使用File类进行文件的创建和删除等操作。

    在编写路径时使用符合本地操作系统的分隔符,要实现这样的功能,

    需要使用File中的两个常量:pathSeparatorseparator

    在程序开发中,往往会使用Windows开发环境,因为Windows操作系统中

    支持的开发工具较多,而在程序发布时往往直接在Linux或其他操作系统上部署,

    所以若不使用File.separator,就会出现问题。

     1 public class FileDemo01 {
     2     public static void main(String[] args) {
     3         //File f = new File("D:\test.txt");//创建一个新的File实例
     4         File f = new File("D:"+File.separator+"test.txt");
     5         try{
     6             f.createNewFile();
     7         }catch(IOException e){
     8             e.printStackTrace();
     9         }
    10     }
    11 }

    File还可实现:删除文件、判断文件是否存在、创建文件夹、

    列出指定目录的全部文件、判断一个给定的路径是否是目录。

    2RandomAccessFile

    File类只是针对文件本身进行操作,而如果要对文件内容进行操作,则可以使用RandomAccessFile类。

    随机读写流可以实现对文件内容的操作,但是一般情况下操作文件内容会使用字节或字符流。

    3)字节流和字符流

    在程序中所有与数据都是以流的方式进行传输和保存的,程序需要数据时要使用输入流读取数据;

    而当程序需要将一些数据保存起来时,就要使用输出流。

    在字节流中输出数据主要使用OutputStream类完成,输入使用InputStream完成。

    在字符流中输出主要使用Wrier类完成,输入主要使用Reader类完成。

    IO操作步骤:使用File类打开一个文件;通过字节流或字符流的子类指定输出位置;

    进行读/写操作;关闭输入/输出。

    字节流

    1)字节的概念

    位(bit):一个二进制数

    Hello World!!!中有十四个字节。

    字节(byte):8个二进制构成一个字节

     1 public class GetByteDemo {
     2     public static void main(String[] args) {
     3         String s = "3,He";
     4         //byte b1 = '!';
     5         byte b[] = s.getBytes();
     6         //System.out.println(b1);
     7         //System.out.println(b.length);
     8         //System.out.println(new String(b));
     9         for(int i=0;i<b.length;i++){
    10             System.out.println(b[i]);        //输出:51 44 72 101
    11             //System.out.println(((char)b[i]));   //输出:3 , H e    
    12         }
    13     }
    14 }

    2OutputStream是整个IO包中字节输出流的最大父类。

    OutputStream类是一个抽象类,如果要使用此类,首先必须通过子类实例化对象。

    FileOutputStreamOutputstream的子类,通过向上转型后,可以为Outputstream实例化。

    顺序:OutputStream(抽象类)

    FileOutputStream(非抽象类)

     1 public class OutputStreamDemo {
     2     public static void main(String[] args) throws Exception {
     3         
     4         //使用File类找到一个文件;若文件不存在,则会自动创建
     5         File f = new File("D:"+File.separator+"test.txt");
     6         
     7         //通过子类实例化父类对象
     8         OutputStream out = new FileOutputStream(f);
     9         
    10         //进行写操作
    11         String str = "Hello";
    12         byte b[] = str.getBytes();
    13         out.write(b);
    14         
    15         //关闭输出流
    16         out.close();
    17     }
    18 }

    通过FileOutputStream向文件中追加内容。

     1 import java.io.File;
     2 import java.io.FileNotFoundException;
     3 import java.io.FileOutputStream;
     4 import java.io.IOException;
     5 import java.io.OutputStream;
     6 
     7 public class FileOutputStreamDemo {
     8 
     9     public static void main(String[] args) throws IOException {
    10         File f = new File("d:" + File.separator + "test.txt");
    11         OutputStream out = new FileOutputStream(f, true);
    12         String str = "
     Hello World!!!";    //换行符
    13         byte b[] = str.getBytes();
    14         for(int i=0;i<b.length;i++){
    15             out.write(b[i]);
    16         }
    17         out.close();
    18     }
    19 }

    3)字节输入流InputStream

    过程类似与字节输出流,但是注意使用;length方法去掉字符串结尾的空格。

    顺序:InputStream(抽象类)

    FileInputStream(非抽象类)

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 
     7 public class InputStreamDemo01 {
     8 
     9     public static void main(String[] args) throws IOException {
    10         File f  =new File("d:" + File.separator + "test.txt");
    11         InputStream input = null;
    12         input = new FileInputStream(f);
    13         byte b[] = new byte[1024];
    14         int len = input.read(b);    //去掉空格
    15         input.close();
    16         System.out.print(new String(b,0,len));
    17     }
    18 }

    如果根据文件的大小开辟空间,则是:

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 
     7 public class InputStreamDemo01 {
     8 
     9     public static void main(String[] args) throws IOException {
    10         File f  =new File("d:" + File.separator + "test.txt");
    11         InputStream input = null;
    12         input = new FileInputStream(f);
    13         byte b[] = new byte[(int)f.length()];
    14         input.read(b);    //去掉空格
    15         input.close();
    16         System.out.print(new String(b));
    17     }
    18 }

    如果不知道输入的内容有多大,则只能通过判断是否读到文件末尾的方式来读取文件:

     1 import java.io.File;
     2 import java.io.FileInputStream;
     3 import java.io.FileNotFoundException;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 
     7 public class InputStreamDemo01 {
     8 
     9     public static void main(String[] args) throws IOException {
    10         File f  =new File("d:" + File.separator + "test.txt");
    11         InputStream input = null;
    12         input = new FileInputStream(f);
    13         
    14         int len = 0;
    15         byte b[] = new byte[1024];
    16         int temp = 0;
    17         while((temp = input.read()) != -1){
    18             //如果temp值不是-1,则说明没有读完
    19             b[len] = (byte) temp;
    20             len++;
    21         }
    22 
    23         input.close();
    24         System.out.print(new String(b,0,len));
    25     }
    26 }

    字符流

    1)字符输出流Writer

    Write类也是一个抽象类,如果要使用此类,则肯定要使用其子类。

    如果向文件中写入内容,应该使用子类FileWriter

    可以直接输出字符串,不用将字符串变为byte数组之后再输出。

    可以使用FileWriter追加文件内容。

    顺序:Writer(抽象类)

    FileWriter(非抽象类)

    向文件中写入数据

     1 import java.io.File;
     2 import java.io.FileWriter;
     3 import java.io.IOException;
     4 import java.io.Writer;
     5 
     6 public class WriterDemo {
     7 
     8     public static void main(String[] args) throws IOException {
     9         File f = new File("d:" + File.separator + "test.txt");
    10         Writer out = new FileWriter(f);
    11         String str = "Hello World!!!";
    12         out.write(str);
    13         out.close();
    14     }
    15 }

    整个程序与Outputstream的操作流程基本没区别,唯一不同的是,可以直接输出字符串,而不用将字符串为byte数组之后再输出。

     1 import java.io.File;
     2 import java.io.FileWriter;
     3 import java.io.IOException;
     4 import java.io.Writer;
     5 
     6 public class WriterDemo {
     7 
     8     public static void main(String[] args) throws IOException {
     9         File f = new File("d:" + File.separator + "test.txt");
    10         Writer out = new FileWriter(f);
    11         String str = "Hello World!!!";
    12         out.write(str);
    13         out.close();
    14     }
    15 }

    使用FileWriter追加文件的内容

     1 package cn.itcast.writer.demo;
     2 
     3 import java.io.File;
     4 import java.io.FileWriter;
     5 import java.io.IOException;
     6 import java.io.Writer;
     7 
     8 public class WriterDemo {
     9 
    10     public static void main(String[] args) throws IOException {
    11         File f = new File("d:" + File.separator + "test.txt");
    12         Writer out = new FileWriter(f, true);    //true表示追加
    13         String str = "Hello World!!!";
    14         out.write(str);
    15         out.close();
    16     }
    17 }

    2)字符输入流Reader

    Reader类也是抽象了,可以直接使用FileReader子类从文件中读取内容。

     1 import java.io.File;
     2 import java.io.FileNotFoundException;
     3 import java.io.FileReader;
     4 import java.io.IOException;
     5 import java.io.Reader;
     6 
     7 public class ReaderDemo {
     8 
     9     public static void main(String[] args) throws IOException {
    10         File f = new File("d:" + File.separator + "test.txt");
    11         Reader reader = new FileReader(f);
    12         int len = 0;
    13         char c[] =new char[1024];
    14         int temp = 0;
    15         while((temp = reader.read()) !=-1){
    16             c[len] = (char) temp;
    17             len++;
    18         }
    19         reader.close();
    20         System.out.print(new String(c,0,len));
    21     }
    22 
    23 }

    字节流和字符流的区别:

    字节流在操作时本身不会到缓冲区(内存)。是文件本身直接操作的,而字符流在操作时

    使用了内存,通过缓冲区在操作文件。

    操作字节流时,即使不关闭输出流,输出内容也会写入文件,

    但在操作字符流时,只有关闭字符流之后,输出内容才会写入文件。

       

    使用缓冲区,可以提高系统频繁操作某一内容情况下的性能。

    所有的文件在硬盘或在传输时都是以字节的方式进行的,包括图片等都是按字节进行存储的,

    而自负是只有在内存中才会形成,所以在开发中,字节流使用较为广泛。

    System类对IO的支持

    1System.out

    System.outPrintStream的对象,在PrintStream中定义了一系列的print()println()方法.

    PrintStream又是OutputStream的子类,所以可以直接利用此对象向屏幕上输出信息。

    2System.err

    表示错误信息的输出。

    3System.in

    是一个键盘的输入流,是InputStream类型的对象。

    但是,在使用System.in的时候,指定大小会出现空间限制,不指定大小则输入中文时会产生乱码。

    如果可以一次性的从内存中读出数据,这样所有数据只读了一次,就不会产生乱码,而且也不受长度的限制,

    而这些操作可以通过BufferedReader类实现。

    4)输入输出重定向

    通过System类可以改变System.in的输入流来源以及System.outSystem.err两个输出流的输出位置。

    例如,可以把错误信息直接保存到文件中。

    5BufferedReader

    通过此类可以一次性从 缓冲区中将内容全部读取出来。

    6)键盘输入的标准格式

     1 public class BufferedReaderDemo01 {
     2     public static void main(String[] args) {
     3         BufferedReader buf = null;
     4         buf = new BufferedReader(new InputStreamReader(System.in));
     5         String str = null;
     6         System.out.println("请输入内容:");
     7         try{
     8             str = buf.readLine();
     9         }catch(IOException e){
    10             e.printStackTrace();
    11         }
    12         System.out.println("输入内容为:"+str);
    13     }
    14 }

    Scanner

    专门的输入数据类,此类不知可以完成输入数据的操作,也可以对输入的数据进行验证。

    可以实现:基本的数据输入、日期格式的数据输入、从文件中得到数据。

     

  • 相关阅读:
    例6-5
    例6-3
    例6-2
    例6-1
    例5-9
    python3
    python3
    python3
    python3
    python3
  • 原文地址:https://www.cnblogs.com/XuGuobao/p/7225993.html
Copyright © 2011-2022 走看看