Java学习第十八天

Filejava

File类在java中表示(带路径的)文件或者目录。linux

  1. File经常使用属性和方法

public static void main(String[] args) {数据库

        

         // 给定路径建立File对象数组

         // File file = new File("D:"+File.separator+"javatest"+File.separator+"a.txt");app

         File file = new File("d:\\javatest\\b.mp3");ui

         System.out.println(file);编码

        

         // 文件基本属性spa

         System.out.println(file.canExecute());.net

         System.out.println(file.canRead());3d

         System.out.println(file.canWrite());

        

         // 文件的建立、删除

         if(!file.exists()) {

             

              boolean r;

              try {

                   r = file.createNewFile();

                   if(r) {

                       System.out.println("文件建立成功");

                   }

              } catch (IOException e) {

                   e.printStackTrace();

              }

         }

        

         // 删除文件

         file.delete();

     }

 

建立文件时会抛出检查时异常IOException

  1. File的路径相关

public static void main(String[] args) {

        

          File file = new File("d:\\javatest\\a");

//       File file = new File("a.txt");

        

         // 获取file的绝对路径

         System.out.println(file.getAbsolutePath());

         // 获取file的建立时的路径字符串

         System.out.println(file.getPath());

         // 获取文件或者目录的名字

         System.out.println(file.getName());

         // 获取文件或者目录的父目录

         System.out.println(file.getParent());

        

     }

注意:若是file是相对路径,相对路径的当前路径是工程目录(java17)

 

  1. 目录的建立

public static void main(String[] args) {

       

         File file = new File("d:\\javatest\\c\\d\\e");

         

         if(!file.exists()) {

             boolean r;

             

            try {

                // 一次只能建立一个目录

                // r = file.mkdir();

                r = file.mkdirs();

                if(r) {

                    System.out.println("目录建立成功");

                }

            } catch (Exception e) {

                e.printStackTrace();

            }

           

         }

    }

 

  1. 目录的遍历

list():返回一个file表示的目录中的子目录或者文件,字符串数组类型

listFiles():返回一个file表示的目录中的子目录或者文件,File数组类型

public static void main(String[] args) {

        

         // 需求:遍历d:\javatest目录

         // list()

         File filenew File("d:\\javatest");

        

        

         /*

         String[] list = file.list();

        

         for (String str : list) {

              System.out.print(str);

              File f = new File(file.getPath()+"\\"+str);

              if(f.isDirectory()) {

                   System.out.println(" 目录");

              }else {

                   System.out.println(" 文件");

              }

         }*/

        

        

         // listFiles();

         File[] listFiles = file.listFiles();

         for (File f : listFiles) {

              System.out.print(f.getName());

              if(f.isDirectory()) {

                   System.out.println(" 目录");

              }else {

                   System.out.println(" 文件");

              }

         }

     }

  1. IO

流(stream):流是一连串流动的数据(字节、字符),先进先出的方式发送的信息的通道中。

 

  1. 输入流和输出流

输入流

数据从源数据源流入程序的过程称为输入流。能够理解为从源数据源读取数据到程序的过程

 

输出流

数据从程序流出到目的地的过程称为输出流。能够理解为把数据从程序写入目的地的过程

 

数据源通常指提供数据的原始媒介,通常常见有文件、数据库、云端、其余硬件等能提供数据的媒介。

 

  1. 流的分类

按照流向分为输入流和输出流

按照处理单元分为字节流和字符流

按照功能分为节点流和转换流。

 

 

InputStream/OutputStream

InputStream 是全部字节输入流的抽象父类,提供了

read 读取一个字节

read(byte[] buf) 读取必定量的字节到缓冲区数组 buf中。

 

OutputStream 是全部字节输出流的抽象父类,提供了

write() 写入一个字节

write(byte[] buf) 写入必定量的字节到输出流

 

FileInputStream 文件字节输入流,专门用于从文件中读取字节到程序内存中。

FileOutputStream 文件字节输出流,专门用于从内存中写入字节到文件中。

 

需求:从文件读取一个字节

public static void main(String[] args) {

        

         // 需求:读取一个文件中的一个字节

         File file = new File("d:\\javatest\\a.txt");

        

         // 1】建立管道

         FileInputStream in = null;

        

         try {

              in = new FileInputStream(file);

             

              // 2】从管道读取一个字节

              /*

              int t;

              t = in.read();

              t = in.read();

              t = in.read();

              t = in.read();

              */

              // System.out.println(t);

             

              // 循环读取一个字节

              int t;

              StringBuilder sb = new StringBuilder();

              while( (t=in.read()) != -1 ) {

                   sb.append((char)t);

              }

             

              System.out.println(sb.toString());

             

             

             

         } catch (FileNotFoundException e) {

              e.printStackTrace();

         } catch(IOException e) {

              e.printStackTrace();

         }

        

         // 3】关闭流管道

         try {

              in.close();

         } catch (IOException e) {

              e.printStackTrace();

         }

     }

 

一次读取多个字节

public static void main(String[] args) {

        

         // 需求:一次读取多个字节

         File file = new File("d:\\javatest\\a.txt");

        

         // 1】建立管道

         FileInputStream in = null;

        

         try {

              in = new FileInputStream(file);

             

              // 2】从管道读取多个字节到缓冲区

              /*

              byte[] buf = new byte[5];

              int len;

              len = in.read(buf);

              len = in.read(buf);

              len = in.read(buf);

              len = in.read(buf);

             

              for(byte b:buf) {

                   System.out.print((char)b+"\t");

              }

              System.out.println(len);

              */

             

              // 经过循环读取文件

              byte[] buf = new byte[5];

              int len;

              StringBuilder sb = new StringBuilder();

              while( (len=in.read(buf)) != -1 ) {

                   // 读取的内容是原始二进制流,须要根据编码的字符集解码成对于字符

                   String str = new String(buf,0,len);

                   sb.append(str);

              }

              System.out.println(sb.toString());

             

             

             

             

         } catch (FileNotFoundException e) {

              e.printStackTrace();

         } catch(IOException e) {

              e.printStackTrace();

         }

        

         // 3】关闭流管道

         try {

              in.close();

         } catch (IOException e) {

              e.printStackTrace();

         }

     }

 

需求:按照指定编码写入文件

public static void main(String[] args) {

        

        

         File file = new File("d:\\javatest\\c.txt");

        

         FileOutputStream out = null;

        

         try {

              // 1】建立输出流管道

              out = new FileOutputStream(file);

             

              // 2】写入数据到管道中

              // 一次写入一个字节

              /*

              out.write(97);

              out.write(98);

              out.write(99);

              */

             

              // 一次写入多个字节

              String str = "hello world";

              // gbk

              /*

              byte[] buf = str.getBytes();

              out.write(buf);

              */

             

              byte[] buf = str.getBytes("UTF-8");

              out.write(buf);

             

              System.out.println("写入完成!");

             

         } catch (FileNotFoundException e) {

              e.printStackTrace();

         } catch (IOException e) {

              e.printStackTrace();

         }

        

         // 3】关闭流

         try {

              out.close();

         } catch (IOException e) {

              e.printStackTrace();

         }

     }

注意:

[1]字符串写入文件时必定会存在编码问题

[2]使用utf8编码写入文件时,若是不含中文时,win系统会对文件的编码形成误判。

[3] 经过字节流写入文件时,向管道写入一个字节,该字节当即写入文件中。

 

总结

InputStream/OutputStream 用于字节的读写。主要用于读取二进制文件(图片、音频、视频),也能够读取文件性文件。

 

需求:请把d:\\javatest\\logo.png 复制到工程目录中,并显示复制进度。

public static void main(String[] args) throws FileNotFoundException,IOException {

       

       

        File oriFile = new File("d:\\javatest\\logo.jpg");

        File toFile = new File("logo.jpg");

       

        long totalLen = oriFile.length();   // 文件大小

        long cpyedLen = 0;  // 已复制完成的大小

        float progress = 0.0f;

       

        FileInputStream in = new FileInputStream(oriFile);

        FileOutputStream out = new FileOutputStream(toFile);

       

        // 一次读取1kb

        byte[] buf = new byte[512];

        int len;

        while( (len=in.read(buf)) != -1) {

            out.write(buf, 0, len);

            cpyedLen += len;

            progress = cpyedLen*1.0f/totalLen;

            System.out.println(progress);

           

           

        }

       

        in.close();

        out.close();

       

        System.out.println("复制完成!");

       

    }

 

    1. Reader/Writer

Reader 是字符输入流的抽象父类,提供了

read 一次读取一个字符

read(char[] cbuf) 一次读取多个字符到字符缓冲区cbuf,返回长度表示读取的字符个数。

 

Writer 是字符输出流的抽象父类,提供了

write

write(char[] cbuf)

write(string)

 

FileReader 文件字符输入流,专门用于读取默认字符编码文本性文件。

 

FileWriter 文件字符输出流,专门用于写入默认字符编码的文本性文件。为了提升效率,FileWriter内部存在一个字节缓冲区,用于对待写入的字符进行统一编码到字节缓冲区,必定要在关闭流以前,调用flush方法刷新缓冲区。

 

需求:一次读取一个字符/多个字符到cbuf

public static void main(String[] args) throws IOException {

       

        File file = new File("d:\\javatest\\d.txt");

       

        FileReader reader = new FileReader(file);

       

        // 1】一次读取一个字符

        /*

        int c;

        c = reader.read();

        c = reader.read();

        c = reader.read();

        c = reader.read();

        c = reader.read();

        System.out.println((char)c);

        */

       

        // 2】一次读取多个字符到cbuf

        /*

        char[] cbuf = new char[2];

        int len;

        len = reader.read(cbuf);

        len = reader.read(cbuf);

        len = reader.read(cbuf);

        len = reader.read(cbuf);

        System.out.println(Arrays.toString(cbuf));

        System.out.println(len);

        */

       

        char[] cbuf = new char[2];

        int len;

        StringBuilder sb = new StringBuilder();

        while( (len=reader.read(cbuf)) != -1 ) {

            sb.append(cbuf,0,len);

        }

       

        System.out.println(sb);

    }

 

需求:写入字符到文件中

public static void main(String[] args) throws IOException {

        

        

         File file = new File("d:\\javatest\\f.txt");

        

         FileWriter writer = new FileWriter(file);

        

         // 1】一次写入一个字符

         /*writer.write('');

         writer.write('');*/

        

         // 2】一次写入多个字符

         /*char[] cbuf = {'h','e','l','l','o','',''};

         writer.write(cbuf);*/

        

         // 3】一次写入一个字符串

         String str = "hello你好";

         writer.write(str);

        

        

         // 刷新字节缓冲区

         writer.flush();

        

         // 关闭流通道

         writer.close();

        

         System.out.println("写入完成");

     }

 

    1. 转换流

 

InputStreamReader 继承于Reader,是字节流通向字符流的桥梁,能够把字节流按照指定编码 解码 成字符流。

 

OutputStreamWriter 继承于Writer,是字符流通向字节流的桥梁,能够把字符流按照指定的编码 编码 成字节流。

  1. 转换流工做原理

 

需求:写入utf8文件

/**

 * 把一个字符串以utf8编码写入文件

 */

public class Test01 {

     public static void main(String[] args) throws IOException {

        

        

         String str = "hello中国";

         File file = new File("d:\\javatest\\g.txt");

        

         // 1】建立管道

         FileOutputStream out = new FileOutputStream(file);

         OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");

        

         // 2】写入管道

         writer.write(str);

        

         // 3】刷新缓冲区

         writer.flush();

        

         // 4】关闭管道

         out.close();

         writer.close();

        

         System.out.println("写入完成");

     }

}

 

需求:读取utf8文件

/**

 * 读取utf8编码的文本文件

 */

public class Test01 {

     public static void main(String[] args) throws IOException {

        

         File file = new File("d:\\javatest\\g.txt");

        

         // 1】创建管道

         FileInputStream in = new FileInputStream(file);

         InputStreamReader reader = new InputStreamReader(in, "UTF-8");

        

         char[] cbuf = new char[2];

         int len;

        

         StringBuilder sb = new StringBuilder();

         while( (len=reader.read(cbuf))!=-1 ) {

              sb.append(cbuf, 0, len);

         }

         System.out.println(sb.toString());

        

     }

}

 

注意:

[1]win平台默认的utf8编码的文本性文件带有BOM,java转换流写入的utf8文件不带BOM。因此用java读取手动建立的utf8文件会出现一点乱码(?hello中国,?是bom致使的)

[2] 一句话:用字符集编码,必定用字符集解码!!

 

FileReader = InputStreamReader + GBK

package cn.sxt07.outputstreamwriter;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

 

/**

 * 读取一个gbk编码的文本性文件

 */

public class Test02 {

     public static void main(String[] args) throws IOException {

        

        

         File file = new File("d:\\javatest\\f.txt");

        

         // 1】创建管道

         /*

          * FileInputStream in = new FileInputStream(file);

          * InputStreamReader reader =  new InputStreamReader(in, "GBK");

          */

 

         FileReader reader = new FileReader(file);

        

         char[] cbuf = new char[2];

         int len;

        

         StringBuilder sb = new StringBuilder();

         while( (len=reader.read(cbuf))!=-1 ) {

              sb.append(cbuf, 0, len);

         }

        

         reader.close();

        

         System.out.println(sb.toString());

     }

}

 

 

    1. BufferedReader/BufferedWriter

BufferedReader 继承于Reader,提供了

read

read(char[] cbuf)

readLine() 用于读取一行文本,实现对文本的高效读取。

BufferedReader 初始化时须要一个reader,本质上BufferedReader在reader的基础上增长readLine()的功能。

 

BufferedWriter继承于Writer,提供了

write

write(char[] cbuf)

write(string)

newline() 写入一个行分隔符。

 

需求:读取一首诗

public static void main(String[] args) throws IOException {

                  

         // 按行读取gbk文本性文件

        

         File file = new File("d:\\javatest\\i.txt");

        

         // 1】建立管道

         FileReader reader = new FileReader(file);

         BufferedReader br = new BufferedReader(reader);

        

         // 2】读取一行

         /*

         String line =  br.readLine();

         line =  br.readLine();

         line =  br.readLine();

         line =  br.readLine();

         */

        

         String line;

         while( (line=br.readLine()) != null) {

              System.out.println(line);

         }

     }

 

需求:以gbk编码写入一首诗到文件

public static void main(String[] args) throws IOException {

        

         File file = new File("d:\\javatest\\j.txt");

        

         // 1】建立gbk管道

         FileWriter writer = new FileWriter(file);

         BufferedWriter bw = new BufferedWriter(writer);

        

         // 2】写入一行

         bw.write("窗前明月光,");

         bw.newLine();

        

         bw.write("疑似地上霜。");

        

         // for win

         // bw.write("\r\n");

        

         // for unix/linux/mac

         // bw.write("\n");

        

         bw.write("举头望明月,");

         bw.newLine();

        

         // 3flush

         bw.flush();

        

         // 4】关闭管道

         bw.close();

         writer.close();

     }

 

需求:以utf8编码高效写入文件

/**

 * utf8写入一首诗

 * @author Administrator

 *

 */

public class Test02 {

     public static void main(String[] args) throws IOException {

        

         File file = new File("d:\\javatest\\j-utf8.txt");

        

         // 1】建立utf8管道

         FileOutputStream out = new FileOutputStream(file);

         OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");

         BufferedWriter bw = new BufferedWriter(writer);

        

         // 2】写入一行

         bw.write("窗前明月光,");

         bw.newLine();

        

         bw.write("疑似地上霜。");

        

         // for win

         bw.write("\r\n");

        

         // for unix/linux/mac

         // bw.write("\n");

        

         bw.write("举头望明月,");

         bw.newLine();

        

         // 3flush

         bw.flush();

        

         // 4】关闭管道

         bw.close();

         writer.close();

     }

}

相关文章
相关标签/搜索