19.Java文件操作---I/O流

Java文件操作—I/O流

流(stream)的概念源于UNIX中管道(pipe)的概念。在UNIX中,管道是一条不间断的字节流,用来实现程序或进程间的通信,或读写外围设备、外部文件等。一个流,必有源端和目的端,它们可以是计算机内存的某些区域,也可以是磁盘文件,甚至可以是Internet上的某个URL。实际上,流的源端和目的端可简单地看成是字节的生产者和消费者,对输入流,可不必关心它的源端是什么,只要简单地从流中读数据,而对输出流,也可不知道它的目的端,只是简单地往流中写数据。
流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

Java中的I/O是一个庞大的体系,大致可以分为三个部分:

  • File类:Java中用于操作系统文件或文件夹的类
  • I/O类(方向):主要包含Input(从硬盘读取数据到程序)和Output(将程序内容写入到硬盘)
  • I/O类(数据类型):主要包含字节流(以字节[8 bit]为单位做数据的传输)和字符流以字符为单位[1字符 = 2字节]做数据的传输

在这里插入图片描述

一、File文件类

File类在java.io包下,用于表示文件或目录的信息(名称、大小等),不能用于文件内容的访问。

File类的常用方法

在这里插入图片描述

public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.rtf");
		System.out.println(file.isFile());//判断是否是一个文件
		System.out.println(file.exists());//判断文件是否存在
		System.out.println(file.getName());//获取文件名
		System.out.println(file.getPath());//获取文件路径
		System.out.println(file.getAbsolutePath());//获取文件路径
		System.out.println(file.length());//获取文件长度
	}

在这里插入图片描述
注意:File类实例化对象时必须指定文件路径,对于不同的操作系统,文件路径中的分隔符不同,例如Windows系统分隔符为反斜杠“\”,Linux系统则为正斜杠“/”。

二、常用流

  • 文件字节输入流:FileInputStream
    FileInputStream流被称为文件字节输入流,意思指对文件数据以字节的形式进行读取操作如读取图片视频等
public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
		FileInputStream fis=null;
		try {
			fis = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int l=0;
		StringBuffer str=new StringBuffer();
		while(l!=-1) {
			try {
				l=fis.read();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			char c=(char)l;
			str.append(c);
		}
		System.out.println(str.toString());
		try {
			fis.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

  • 文件字节输出流:FileOutputStream FileOutputStream流是指文件字节输出流,专用于输出原始字节流如图像数据等
public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
        String content="Hello World";
		FileOutputStream fos=null;
		try {
			fos = new FileOutputStream(file, false);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			fos.write(content.getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			fos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

  • 文件字符输入流:FileReader 程序以字符为单位从磁盘文件读取数据
public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
        FileReader fr=null;
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        int c = 0;
        try {
			while((c = fr.read()) != -1){
			    System.out.print((char)c);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			fr.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

  • 文件字符输出流:FileWriter 程序以字符为单位,向磁盘文件写入数据
public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
		FileWriter fw = null;

		    try {
				fw = new FileWriter(file,true);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    String str = "HelloWorld我的Java世界";
		    try {
				fw.write(str);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    try {
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

  • 缓冲字节输入流:BufferedInputStream 缓冲字节输入流对读取数据提供了缓冲的功能,提⾼了读取的效率
public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
		FileInputStream in=null;
		try {
			in = new FileInputStream(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    BufferedInputStream bis = new BufferedInputStream(in, 1024);
	    int b=0;
	    try {
			while((b = bis.read()) != -1){
			    System.out.print((char)b);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			bis.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

  • 缓冲字节输出流:BufferedOutputStream 缓冲字节输出流对写入数据提供了缓冲的功能,提⾼了写入的效率
public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
		FileOutputStream out=null;
		try {
			out = new FileOutputStream(file,false);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    BufferedOutputStream bos = new BufferedOutputStream(out, 1024);
	    try {
			bos.write("helloMyJava".getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			bos.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

缓冲字符输入流:BufferedReader
缓冲字符输入流对读取数据提供了缓冲的功能,提⾼了读取的效率

public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
		FileReader fr=null;
		try {
			fr = new FileReader(file);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    BufferedReader br = new BufferedReader(fr, 1024);
	    String str;
	    try {
			while((str = br.readLine()) != null){
			    System.out.println(str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			fr.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

  • 缓冲字符输出流:BufferedWriter 缓冲字符输出流对读取数据提供了缓冲的功能,提⾼了读取的效率
	public static void main(String[] args) {
		File file=new File("/Users/aries/Desktop/1.txt");
		FileWriter fw=null;
		try {
			fw = new FileWriter(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    BufferedWriter br = new BufferedWriter(fw);
	    try {
			br.write("来呀,你打我撒");
			br.newLine();
		    br.write("来呀,你打我撒");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    try {
			br.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    try {
			fw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

  • 字节流转换字符流:InputStreamReader
  • 字节流转换成字符流的桥梁,可以显式地指定字符集,否则调用默认字符集
  • 字符流转换字节流:OutputSreamWriter
  • 字符流转换成字节流的桥梁,可以显式地指定字符集,否则调用默认字符集
  • 字节数组输入流:ByteArrayInputStream
  • 包含一个内部缓冲区该缓冲区包含从流中读取的字节。内部计数器跟踪read方法要提供的下一个字节。关闭ByteArrayInputStream无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何IOException
  • 字节数组输出流:ByteArrayOutputStream
  • 实现了一个输出流,其中的数据被写入一个byte数组。缓冲区会随着数据的不断写入而自动增长。可使用toByteArray()和toString()获取数据。关闭ByteArrayOutputStream无效。此类中的方法在关闭此流后仍可被调用,而不会产生任何IOException
  • 数据输入流:DataInputStream
  • 类允许应用程序以独立于机器的方式从输入流中读取原始数据
  • 数据输出流:DataOutputStream
  • 类允许应用程序以与机器无关的方式将原始Java数据类型写入输出流
  • 对象输入流:ObjectInputStream完成对象的序列化
  • 对象输出流:ObjectOutputStream完成对象的反序列化
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值