zoukankan      html  css  js  c++  java
  • Java基础视频笔记(七):IO操作

    基础视频笔记的第七部分:Java中的 I O 操作:


    1、IO简介: IO 也写作 “I/O”可以理解为In 和 Out,输入和输出;

    IO 流,作用:读写设备上的数据,硬盘文件、内存、键盘、网络

    根据数据的走向,可分为:输入流、输出流
    根据处理的数据类型,可分为:字节流、字符流;

    字节流:可以处理所有类型的数据,如MP3、图片、文字、视频等等;
    在读取时,读到一个字节,就返回一个字节; 
    *对应的类都是以Stream结尾的。

    字符流:只能处理纯文本数据,如txt文本等。
    在读取时,读到一个或多个字节,先查找指定的编码表,
    然后将插到的字符返回。
    *对应的类都是以Reader或Writer结尾;

    2、字节、字符、编码的区别

    1、字节是通过网络传输信息、或在硬盘、内存中存储信息的单位,
      是计算机信息技术用于计量存储容量和传输容量的一种计量单位;

      1字节 = 8位二进制,即一个8位的二进制数;,是一个很具体的存储空间;
      0x01,0x45,0xFA

    2、字符(char),是人们使用的记号,抽象意义上的一个符号;
      如:'1' '中' '$' '¥'

    3、编码(Charset),“编码”也称作“字符集”,各个国家和地区所
      制定的不同ANSI编码标准中,都只规定了各自语言所需的“字符”;
      比如:汉字标准 GB2312 中,就没有规定韩国语字符怎样存储

    ANSI 编码标准所规定的内容包含两层含义:

    1、使用哪些字符。也就是说哪些汉字,字母和符号,会被收入标准中,
      所包含的字符的集合,就叫做“字符集”;

    2、规定每个“字符”分别用一个字节还是用多个字节来存储,用哪些
      字节来存储,这个规定就叫做“编码”;

    3、各个国家和地区在指定编码标准的时候,“字符的集合”和编码一般
      都是同时制定的,

      因此,平常我们所说的“字符集”,比如GB2312,GBK,JIS等,除了
    “字符的合集”这层含义之外,也包含了“编码”的含义;

    3、使用字节流读写数据:

    1. // 读取  
    2. FileInputStream fis = new FileInputStream();  
    3. byte[] input = new byte[20];  
    4. fis.read(input);  
    5. // 对读到的字节 解码      
    6. String inputString = new String(input,"UTF-8");  
    7. System.out.println(intpuStream);  


    4、使用带缓冲的字节流读写数据:

    1. *读取效率高效;  
    2.   
    3.         FileInputStream fis = new FileInputStream("movie.mp4")  
    4.         BufferedInputStream bis = new BufferedInputStream(fis,1000000);// 缓冲区的优化,数值调大  
    5.                                         // 读写次数减少,速度加快  
    6.         // 拷贝文件  
    7.         FileOutputStream fos = new FileOutputStream("movie_new.mp4");  
    8.         BufferedOutputStream bos = new BufferedOutputStream(fos,1000000);  
    9.           
    10.         byte[] input = new byte[100000]; // 数组大小要针对文件大小优化,大文件对应的数组就要大一些  
    11.         //计时器  
    12.         long before = System.currentTimeMillis();  
    13.         //判断读取是否完成  
    14.         while(bis.read(input) != -1){  
    15.             bos.write(input);  
    16.             count++;  
    17.         }  
    18.         //关闭流  
    19.         bis.close();  
    20.         fis.close();  
    21.         bos.close();  
    22.         fos.close();  
    23.         //  
    24.         System.out.println(System.curentTimeMillis()-before+"ms");  
    25.         //   
    26.         System.out.println("读写了"+count+"次");  


    5、使用字符流读取文件数据:

    1. * 读取文件内容输出;  
    2. File file = new File("Java.txt");  
    3. FileInputStream fis = new FileInputStream(file);  
    4. // 字节流转字符流,字符集设为 UTF-8  
    5. InputStreamReader isr = new InputStreamReader(fis,"UTF-8");  
    6. char[] input = new char[100];  
    7. int lent = 0;  
    8. // 判断是否到文件末尾  
    9. while((l = isr.read(input)) != -1){  
    10.     // 为-1 到末尾,输出input数组  
    11.     // 默认会强制调用toString()方法  
    12.     System.out.print(input);  
    13.     // 也可以写为:  
    14.     // String() 有重载带偏移量、长度的方法 String(bytes,int offset,int length);  
    15.     System.out.print(new String(input,0,lent));  
    16. }  
    17. isr.close();  
    18. fis.close();  
    19.   
    20.   
    21.   
    22. *文本文件内容拷贝(写入),  
    23. fis  
    24. FileOutputStream fos = new FileOutputStream("test_new.txt");  
    25. isr  
    26. OutputStreamWriter osw = new OutputStreamWriter(fos,"UTF-8");  
    27. char[] input = new char[100];  
    28. int length = 0;  
    29. while((length = isr.read(input)) != -1){  
    30.     osw.write(input);  
    31. }  
    32. isr.close();  
    33. fis.close();  
    34. osw.close();  
    35. fos.close();  


    6、使用带有缓冲的字符流读写数据:

    * 为什么用带缓冲的字符流?
    inputstreanreader 没有读取字符串的方法
    outputstreamwriter 没有写出一行、换行的方法

    * 怎么使用:
    BufferedReader 
    BufferedWriter 

    7、FileReader 和 FileWriter

    1. FileReader 读取文本数据  
    2. FileReader fr = new FileReader("java.txt");  
    3. BufferedReader br = new BufferedReader(fr);  
    4. //写文件  
    5. FileWriter fw = new FileWriter("java_new.txt");  
    6. BufferedWriter bw = new BufferedWriter(fw);  
    7.   
    8.   
    9. String line;  
    10. while((line = br.readline()) != null{  
    11.     bw.write(line+"\n");  
    12. }  
    13. bw.flush();  
    14.   
    15. bw.close();  
    16. fw.close();  


    8、RandomAccessFile 随机文件读写:

      * 按照需求,对文件的特定位置,进行读写操作
     
    MultiwriteFile.java
    File file = new File("D:/test.txt");

    1. package RandomAccessFile;  
    2.   
    3. import java.io.File;  
    4. import java.io.FileNotFoundException;  
    5. import java.io.IOException;  
    6. import java.io.RandomAccessFile;  
    7.   
    8. /* Block 区块 
    9.  * 1            2            3            4            5            6 
    10.  * |------------|------------|------------|------------|------------| 
    11.  * 0xL         1xL          2xL          3xL          4xL          5XL 
    12.  *  
    13.  * 传入的 block值是从 1 开始的 
    14.  * 当前文件指针是从 0 开始的 
    15.  * 0xL 代表这个区块的大小为100个字节 
    16.  *  
    17.  * 公式写为  (block-1)*L 
    18.  *  
    19.  * 可以用于多线程文件下载,多线程传输,也可用于读取特定内容 
    20.  */  
    21.   
    22. public class WriteFile extends Thread {  
    23.     File file;  
    24.     int block;  // 定义一个区块  
    25.     int L  = 100;   // 定义区块的长度  
    26.       
    27.     public WriteFile(File f, int b){  
    28.         this.file = f;  
    29.         this.block = b;  
    30.     }  
    31.       
    32.     public void run() {  
    33.         try {  
    34.             RandomAccessFile raf = new RandomAccessFile(file, "rw");  
    35.             raf.seek((block-1)*L);  // 文件 指针头  
    36.             raf.writeBytes("That's amazing~!");  
    37.             for (int i = 0; i <= 20; i++) {  
    38.                 raf.writeBytes("-");  
    39.             }  
    40.             raf.writeBytes("\n");  
    41.               
    42.         } catch (FileNotFoundException e) {  
    43.             e.printStackTrace();  
    44.         } catch (IOException e) {  
    45.             e.printStackTrace();  
    46.         }  
    47.     }  
    48. }  

    启动线程:

    1. package RandomAccessFile;  
    2.   
    3. import java.io.File;  
    4. import java.io.IOException;  
    5. import java.io.RandomAccessFile;  
    6.   
    7. public class MultiWriteFile {  
    8.       
    9.     static File file = new File("D:/FileDemo/test.txt");  
    10.   
    11.     public static void main(String[] args) {  
    12.         try {  
    13.               
    14.             file.createNewFile();  
    15. /*           
    16.             if(file.exists()){ 
    17.                 file.delete(); 
    18.             } 
    19.             // 启动 WriteFile类的线程 
    20.             new WriteFile(file, 1).start(); 
    21.             new WriteFile(file, 2).start(); 
    22.             new WriteFile(file, 3).start(); 
    23.             new WriteFile(file, 4).start(); 
    24.             new WriteFile(file, 5).start(); 
    25. */            
    26.             // 读取文件中某一段  
    27.             RandomAccessFile raf = new RandomAccessFile(file, "r");  
    28.             // 设定文件指针位置  
    29.             raf.seek(300);  // 399 代表第四行 0 1 2 3  
    30.             byte[] bt = new byte[20];  // 读取第四行前20个字节  
    31.             // 读取文件的相应位置  
    32.             raf.read(bt);   
    33.             // 把字节  转换为 字符  
    34.             String in = new String(bt);  
    35.             System.out.println(in);  
    36.               
    37.         } catch (IOException e) {  
    38.             e.printStackTrace();  
    39.         }  
    40.     }  
    41. }  


    9、用Apache 的IO,操作文件

    登陆 apache.org
    左侧找到: Common——Component——IO——Download

    下载 Commons IO 2.4
    把 commons-io-2.4.jar 复制到当前项目路径下,即可调用其中的类和方法。

  • 相关阅读:
    输入一批整数,输出最大最小值,输入0结束
    获取最低价手机价格
    插入数值
    猜数游戏
    数字金字塔
    输出星期数
    9*9乘法表
    linux 出core设置问题
    linux socket连接中 ERRNO错误
    linux c 获取头文件函数getenv
  • 原文地址:https://www.cnblogs.com/jackchiang/p/4585218.html
Copyright © 2011-2022 走看看