zoukankan      html  css  js  c++  java
  • Java IO流篇

    什么是IO流

     思考问题

      如何读写文件?

    解决--通过流读写文件

      是指一连串流动的字符,以先进先出传输信息的通道。

      Java操控硬盘上的文件,通过IO流来实现

      

         

     Java流的分类

      按流向区分 ---输出流 OutputStream 和 Writer 作为基类

            ---输入流 InputStream 和 Reader 作为基类

        输入输出流是相对于计算机内存来说的

      按处理数据单元划分 ---字节流

                  ----字节输入流InputStream和字节输出流OutputStream

                ---字符流 

                 ----字符输入流Reander和字符输出流Writer

         字节流是8位通用字节流,字符流是16位Unicode字符流

     File构造器

      1、什么是文件?

            文件是认为是相关记录或放在一起的数据的集合

      2、文件一般存储在哪里?

        计算机硬盘

      3、JAVA程序如何访问文件属性?

         java api:java.io.File类

         

    public class test01 {
    	public static void main(String[] args) throws IOException {
    //		File  f1=new File("D://111");
    //			f1.mkdir();  //创建文件夹
    		File  f2=new  File("D://111//a.txt");			
    			f2.createNewFile();  //创建文件,前提是111文件夹存在	
    	}
    }
    

    File类常用方法

      boolean  exists()  判斷文件或目錄是否存在

      boolean   isFile()  判斷是否是文件

      Boolean    isDirectory()   判斷是否是目錄

      String   getPanth()  返回此對象表示的文件的相對路徑

      String    getAbsolutePath()  返回此對象表示的文件的絕對路徑

      String     getName()  返回此對象表示的文件或目錄的名稱

      boolean    delete()  刪除此對象指定的文件或目錄

      boolean     createNewFile()  創建名稱的文件,不創建文件夾

      long    length()  返回文件的長度,單位為字節,如果文件不存在,則返回0L

    字節流

      用FileInputStream和FileOutputStream來讀取文本

      

    import java.io.*;
    //读取文件数据
    /*
     * 第一步:先找到要读取的文件  File对象
     * 第二步:读取文件 读出来的数据应该是二进制,要使数据到你的程序里来,首先要建立一个传输数据的通道  搭建数据的输入通道
     */
    public class test01 {
    
    	public static void main(String[] args) throws IOException {
    		readTest();
    		readTest1();
    		readTest2();
    		readTest3();
    	}
    	
    	public static void readTest() throws IOException{
    		//第一步
    		File file=new File("D:\test\test.txt");
    		//第二步,建立数据输入通道
    		FileInputStream  input=new FileInputStream(file);
    		
    		//读取文件中一个字符数据,并返回输出
    		int connet=input.read();
    		System.out.println(connet);
    		//关闭流
    		input.close();
    		
    	}
    	
    	//使用循环读取数据
    	public static void readTest1() throws IOException{
    		int connet=0;
    		File file=new File("D:\test\test.txt");
    		FileInputStream input=new FileInputStream(file);
    		while((connet=input.read())!=-1){
    			System.out.println((char)connet);
    		}
    		input.close();
    	}
    	
    	//使用缓冲读取数据  缺点:无法读取完整的一个文件
    	public static void readTest2() throws IOException{
    		File file=new File("D:\test\test.txt");
    		FileInputStream input=new FileInputStream(file);
    		//建立缓冲字节数组,读取文件数据
    		byte[]  buf=new byte[3];//相当于超市里的购物车
    		
    		int length=input.read(buf);//数据已经存储到了字节数组
    		//因为数据是存储到字节数组中,而这里的read方法的返回值是表示本次读取的几个字节数据到字节数组中
    		//上一句的作用就是读文件中数据存到字节数组中去
    		//使用字节数组构建字符串
    		System.out.println(length);
    		String contnet=new String(buf);
    		System.out.println("内容"+contnet);
    		input.close();
    		
    	}
    	
    	//使用缓冲数组配合循环一起读取
    	public static void readTest3() throws IOException{
    		File file=new File("D:\test\test.txt");
    		FileInputStream input=new FileInputStream(file);
    		
    		int length=0;//保存每次读取到的字节个数
    		byte[]  buf=new byte[1024];
    		//byte[]  buf=new byte[1024*3];
    		//缓冲数组一般是1024的倍数,因为是计算机的处理单位,原则上缓冲数组越大,效率越高
    		while((length=input.read(buf))!=-1){
    			System.out.println(new String(buf,0,length));
    		}
    		input.close();
    	}
    }
    
  • 相关阅读:
    Android-PullToRefresh 下拉刷新增加setOnItemLongClickListener
    【453】周志华-机器学习-读书笔记
    【452】pandas筛选出表中满足另一个表所有条件的数据
    【451】python 同一行打印进度条
    【449】Win10 蓝牙耳机链接没有声音
    HBase(一)HBase入门简介
    kafka可视化客户端工具(Kafka Tool)的基本使用
    Kafka(五)Kafka的API操作和拦截器
    Kafka(四)Kafka在zookeeper中的存储
    Kafka(三)Kafka的高可用与生产消费过程解析
  • 原文地址:https://www.cnblogs.com/wysk/p/7666842.html
Copyright © 2011-2022 走看看