zoukankan      html  css  js  c++  java
  • JAVA学习第四十七课 — IO流(一):文件的读写

    输入流和输出流相对于内存

    将外部设备的数据读取到内存中:输入

    将内存中的数据写入外部设备中:输出

    IO流经常使用基类

    字节流的抽象基类:InputStream,OutputStream

    字符的抽象基类:Reader。Writer

    PS:由这四个流派生出的子类都是以父类名作为后缀名

    如:InputStream的派生类FileInputStream,Reader的派生类FileReader

    PS:假设是文字形式的数据应该优先考虑字符流,且将数据从内存写到硬盘上应该是Writer

    FileWriter类

    演示:将文字储存到硬盘上(FileWriter)

    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Main 
    {
    	public static void main(String[] args) throws IOException{
    //假设文件不存在。会自己主动创建,存在。会覆盖
    		FileWriter fw = new FileWriter("Demo.txt");//抛异常
    		
    //调用Writer中的write方法,写入数据,但实际上数据写入的是暂时储存缓冲区中
    		fw.write("你好");
    	
    //用flush方法刷新该流的缓冲。

    假设该流已保存缓冲区中各种 write() 方法的全部字符,则马上将它们写入预期目标。 fw.flush();//能够用多次 //关闭资源 //关闭流,关闭资源,在关闭前会先调用flush刷新缓冲中的数据到目的地 //API文档:关闭此流,但要先刷新它。

    //在关闭该流之后。再调用 write() 或 flush() 将导致抛出 IOException。关闭曾经关闭的流无效。 fw.close();//仅仅能用一次 } }

    PS:FileWriter的构造函数 FileWriter("damo.txt",true);续写数据,不写,默认是false,会覆盖原有数据


    细节

    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Main 
    {
    	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    	public static void main(String[] args) throws IOException{
    
    		FileWriter fw = new FileWriter("Demo.txt",true);
    		//构造函数,中加个true能够实现对文件的续写
    
    		fw.write("你好"+LINE_SEPARATOR+"世界");//Windows下换行是
    ,Linus下是
    
    		fw.write(LINE_SEPARATOR+"ads");
    		fw.flush();
    		fw.close();
    	}
    }

    关于流异常的基本处理

    在try的外面创建引用。在里面创建对象,关闭时要推断是是否是空指针

    import java.io.FileWriter;
    import java.io.IOException;
    
    public class Main 
    {
    	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
    	public static void main(String[] args){
    
    		FileWriter fw = null;
    		try {
    			fw = new FileWriter("z:\Demo.txt",true);//假设传入一个找不到的地址会抛出两个异常
    						//找不到文件异常、空指针异常。所以finally部分。加一句推断是否是空指针
    		fw.write("你好"+LINE_SEPARATOR+"世界");
    		fw.flush();
    		
    		} catch (Exception e) {
    			System.out.println(e.toString());
    		}finally{
    			//最后再try一次,单独处理
    			if(fw!=null){
    				try {
    				fw.close();
    				} catch (IOException e) {
    				throw new RuntimeException("关闭失败");
    				}
    			}
    		}	
    	}
    }
    

    FileReader类

    读取一个文本文件,并将读取出来的数据打印到控制台

    import java.io.FileReader;
    import java.io.IOException;
    
    public class Main 
    {
    	public static void main(String[] args) throws IOException {
    		
    		//一定要确保文件是存在的
    		FileReader fr = new FileReader("g:\java\Main\Demo.txt");//文件内容是ab
    		
    		//read方法,作为整数读取的字符,范围在 0 到 65535 之间 (0x00-0xffff),假设已到达流的末尾,则返回 -1 
    
    //		int c = fr.read();
    //		System.out.println((char)c);
    //		int c1 = fr.read();
    //		System.out.println((char)c1);
    //		int c2 = fr.read();
    //		System.out.println(c2);//-1
    		
    		int temp;
    		while((temp = fr.read())!=-1){
    			System.out.println(temp);
    		}
    		fr.close();
    	}
    }


    read();读取一个一个字符。相比之下,read()方法还有读取数组的重载形式:效率高

    import java.io.FileReader;
    import java.io.IOException;
    
    public class Main 
    {
    	public static void main(String[] args) throws IOException {
    		
    		FileReader fr = new FileReader("g:\java\Main\Demo.txt");//文件内容是abcdef
    		//使用read(char[] a)读取文本文件数据
    		/*
    		 * 先创建字符数组。 
    		 */
    //		char ch[] = new char[5];
    //		int num = fr.read(ch);//将读取到的字符存储到数组中,num是字符的个数
    //		System.out.println(num+":"+new String(ch));//5:abcde
    //		
    //		int num1 = fr.read(ch);//将读取到的字符存储到数组中
    //		System.out.println(num1+":"+new String(ch));//1:fbcde
    //		//这里相当于。f把原来数组里的a替换了。而txt里无数据了,所以剩余的数组元素还在
    //
    //		int num2 = fr.read(ch);//再读也没有数据了。所以返回-1,char数组无覆盖
    //		System.out.println(num2+":"+new String(ch));//-1:fbcde
    		
    		//正规写法
    		int num = 0; 
    		
    		char[] ch = new char[5];//数组的长度最好是1024*n
    		
    		while((num = fr.read(ch))!=-1){
    			System.out.println(new String(ch,0,num));
    		}
    		
    		fr.close();
    	}
    }


  • 相关阅读:
    [网页基础]DIV+CSS学习笔记(一)CSS的引入及选择器基础
    [网页基础]CSS+DIV布局,简单布局例子
    [C#]结构体和类的区别
    [网页基础]DIV+CSS学习笔记(二)深入理解盒子模型
    [数据库]mysql存储过程的建立及使用
    [网页基础]DIV+CSS学习笔记(三)盒子的定位与浮动
    [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例
    Java实现断点续传和原理
    Minio的Docker部署dockercompose启动流程
    ShardingJDBC
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5323631.html
Copyright © 2011-2022 走看看