zoukankan      html  css  js  c++  java
  • Java——字节(输入/输出)流,字符(输入/输出)流

    一、字节输出流

    OutputStream抽象类

    此抽象类,是表示输出字节流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。

    • 字节: 这样流每次只操作文件中的1个字节
    • 流对象:操作文件的时候,自己不做依赖操作系统

    作用:从Java程序,写入文件(可以写任意文件)

    方法:

    • write(int b) 写入1个字节
    • write(byte[] b) 写入字节数组
    • write(byte[] b,int,int)写入字节数组,int 开始写入的索引, int 写几个
    • close() 方法,关闭流对象,释放与此流相关的资源

    FileOutputStream类

    OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。

    FileOutputStream类,即文件输出流,是用于将数据写入File的输出流。

    流对象使用步骤:

    • 1. 创建流子类的对象,绑定数据目的
    • 2. 调用流对象的方法write写
    • 3. close释放资源

    1、写入文件

    public static void main(String[] args)throws IOException {
    	//流对象的构造方法,可以创建文件,如果文件存在,直接覆盖
    	FileOutputStream fos = new FileOutputStream("c:\a.txt");
    	//流对象的方法write写数据
    	//写1个字节,查询编码表
    	fos.write(97);
    	
    	//写字节数组
    	byte[] bytes = {65,66,67,68};
    	fos.write(bytes);
    	
    	//写字节数组的一部分,开始索引,写几个
    	fos.write(bytes, 1, 2);
    	
    	//写入字节数组的简便方式
    	//写字符串
    	fos.write("hello".getBytes());
    
    	//关闭资源
    	fos.close();
    }
    

    2、IO流的异常处理

    import java.io.FileOutputStream;
    import java.io.IOException;
    
    /*
     *   IO流的异常处理
     *   try catch finally
     *   
     *   细节:
     *     1. 保证流对象变量,作用域足够
     *     2. catch里面,怎么处理异常
     *         输出异常的信息,目的看到哪里出现了问题
     *         停下程序,从新尝试
     *     3. 如果流对象建立失败了,需要关闭资源吗
     *         new 对象的时候,失败了,没有占用系统资源
     *         释放资源的时候,对流对象判断null
     *         变量不是null,对象建立成功,需要关闭资源
     */
    public class FileOutputStreamDemo3 {
    	public static void main(String[] args) {
    		//try 外面声明变量,try 里面建立对象
    		FileOutputStream fos = null;
    		try{
    			fos = new FileOutputStream("s:\a.txt");
    			fos.write(100);
    		}catch(IOException ex){
    			System.out.println(ex);
    			throw new RuntimeException("文件写入失败,重试");
    		}finally{
    			try{
    				if(fos!=null)
    			      fos.close();
    			}catch(IOException ex){
    				throw new RuntimeException("关闭资源失败");
    			}
    		}
    	}
    }
    

    3、字符串转成byte数组

    "hello,world!".getBytes()

    4、文件续写

    FileOutputStream fos = new FileOutputStream(file, true);

    二、字节输入流

    InputStream抽象类

    通过InputStream可以实现。InputStream此抽象类,是表示字节输入流的所有类的超类。,定义了字节输入流的基本共性功能方法。

    • int read():读取一个字节并返回,没有字节返回-1.

    • int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数,没有字节返回-1.

    FileInputStream

    输入流读取文件的步骤

    • 1. 创建字节输入流的子类对象
    • 2. 调用读取方法read读取
    • 3. 关闭资源

    read()方法,

    • read()执行一次,就会自动读取下一个字节
    • 返回值,返回的是读取到的字节, 读取到结尾返回-1

    1、读取单个字节

    public static void main(String[] args) throws IOException{
    	FileInputStream fis = new FileInputStream("c:\a.txt");
    	//读取一个字节,调用方法read 返回int
    	//使用循环方式,读取文件,  循环结束的条件  read()方法返回-1
    	int len = 0;//接受read方法的返回值
        
    	while( (len = fis.read()) != -1){  // 返回值保存到len中
    		System.out.print((char)len);
    	}
    	//关闭资源
    	fis.close();
    }
    

    2、读取一个字节数组

    public static void main(String[] args) throws IOException {
    	FileInputStream fis = new FileInputStream("c:\a.txt");
    	//创建字节数组
    	byte[] b = new byte[1024];
    	
    	int len = 0 ;
    	while( (len = fis.read(b)) !=-1){  // 保存到字节数组b中
    		System.out.print(new String(b,0,len));
    	}
    	fis.close();
    }
    

    Copy文件

    public static void main(String[] args) {
    	long s = System.currentTimeMillis();
    	FileInputStream fis = null;
    	FileOutputStream fos = null;
    	try{
    		fis = new FileInputStream("c:\t.zip");
    		fos = new FileOutputStream("d:\t.zip");
    		//定义字节数组,缓冲
    		byte[] bytes = new byte[1024*10];
    		//读取数组,写入数组
    		int len = 0 ; 
    		while((len = fis.read(bytes))!=-1){
    			fos.write(bytes, 0, len);
    		}
    	}catch(IOException ex){
    		System.out.println(ex);
    		throw new RuntimeException("文件复制失败");
    	}finally{
    		try{
    			if(fos!=null)
    				fos.close();
    		}catch(IOException ex){
    			throw new RuntimeException("释放资源失败");
    		}finally{
    			try{
    				if(fis!=null)
    					fis.close();
    			}catch(IOException ex){
    				throw new RuntimeException("释放资源失败");
    			}
    		}
    	}
    	long e = System.currentTimeMillis();
    	System.out.println(e-s);
    }
    

    三、字符输入/输出流

    1、字符输入——FileReader类

    字符输入流只能读取文本文件,所有字符输入流的超类为java.io.Reader类

    • int read() 读取1个字符
    • int read(char[] c) 读取字符数组
    public static void main(String[] args) throws IOException{
    	FileReader fr = new FileReader("c:\1.txt");
    	// 读取单个字符
    	/*int len = 0 ;
    	while((len = fr.read())!=-1){
    		System.out.print((char)len);
    	}*/
    	//字符数组
    	char[] ch = new char[1024];
    	int len = 0 ;
    	while((len = fr.read(ch))!=-1){
    		System.out.print(new String(ch,0,len));
    	}
    	fr.close();
    }
    

    2、字符输出流——FileWriter

    所有字符输出流的超类为java.io.Writer 

    • write(int c) 写1个字符
    • write(char[] c)写字符数组
    • write(char[] c,int,int)字符数组一部分,开始索引,写几个
    • write(String s) 写入字符串
    public static void main(String[] args) throws IOException{
    	FileWriter fw = new FileWriter("c:\1.txt");
    	
    	//写1个字符
    	fw.write(100);
    	fw.flush();
    	
    	//写1个字符数组
    	char[] c = {'a','b','c','d','e'};
    	fw.write(c);
    	fw.flush();
    	
    	//写字符数组一部分
    	fw.write(c, 2, 2);
    	fw.flush();
    	
    	//写入字符串
    	fw.write("hello");
    	fw.flush();
    	
    	fw.close();
    }
    

    Copy文件

    public static void main(String[] args) {
    	FileReader fr = null;
    	FileWriter fw = null;
    	try{
    		fr = new FileReader("c:\1.txt");
    		fw = new FileWriter("d:\1.txt");
    		char[] cbuf = new char[1024];
    		int len = 0 ;
    		while(( len = fr.read(cbuf))!=-1){
    			fw.write(cbuf, 0, len);
    			fw.flush();
    		}
    		
    	}catch(IOException ex){
    		System.out.println(ex);
    		throw new RuntimeException("复制失败");
    	}finally{
    		try{
    			if(fw!=null)
    				fw.close();
    		}catch(IOException ex){
    			throw new RuntimeException("释放资源失败");
    		}finally{
    			try{
    				if(fr!=null)
    					fr.close();
    			}catch(IOException ex){
    				throw new RuntimeException("释放资源失败");
    			}
    		}
    	}
    }
    
  • 相关阅读:
    “automation服务器不能创建对象”的问题的解决方案大全
    转载区分C#中的Abstract函数和Virtual函数
    DOS批处理
    数据库设计范式
    java 内存查看工具
    Java内存溢出详解
    Struts2 循环编辑指定次数
    Selenium 使用
    spring security 获取当前用户信息
    由MyEclipse内存不足谈谈JVM内存
  • 原文地址:https://www.cnblogs.com/x54256/p/8438094.html
Copyright © 2011-2022 走看看