【Java】IO流原理及流的分类(FileInputStream和FileOutputStream方法详解)
活动地址:CSDN21天学习挑战赛
目录
1.Java IO流原理
1.I/O是Input/Output的缩写,用于数据传输,读写文件网络通讯。
2.Java程序中,对于数据的输入/输出以流(stream)的方式进行。
2.InputStream字节输入流
InputStream抽象类是所有类字节输入流的超类
InputStream常用子类
1.FileInputStream:文件输入流
2.BufferedInputStream:缓冲字节输入流
3.ObjectInputStream:对象字节输入流
1.FileInputStream文件输入流
//创建 FileInputStream 对象,用于读取 文件
fileInputStream = new FileInputStream(filePath);
//从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。
//如果返回-1 , 表示读取完毕
package com.inputstream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;/** * @version 1.0 * @auther Demo龙 * 演示FileInputStream的使用(字节输入流 文件--> 程序) * */public class FileInputStresam { public static void main(String[] args) { Test01 test01 = new Test01(); test01.readFile01(); }}class Test01{ public void readFile01(){ String filePath = "e:\\hello.txt"; int readData = 0; FileInputStream fileInputStream = null; try { //创建 FileInputStream 对象,用于读取 文件 fileInputStream = new FileInputStream(filePath); //从该输入流读取一个字节的数据。 如果没有输入可用,此方法将阻止。 //如果返回-1 , 表示读取完毕 while ((readData = fileInputStream.read()) != -1) { System.out.print((char)readData);//转成char显示 } } catch (IOException e) { e.printStackTrace(); } finally { //关闭文件流,释放资源. try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }}
//最后要关闭文件流,释放资源.
3.FileOutputStream文件输出流
OutputStream方法
//1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容
//2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面
3. //写入一个字节6 //fileOutputStream.write('H');//
4. //写入字符串 String str = "hello,world,Demo!"; //str.getBytes() 可以把 字符串-> 字节数组 //fileOutputStream.write(str.getBytes());
5.write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流
package com.outputstream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/** * @version 1.0 * @auther Demo龙 */public class Fileoutputstream { public static void main(String[] args) { Test test = new Test(); test.writefile(); }}class Test{ public void writefile(){ //创建 FileOutputStream对象 String filePath = "e:\\a.txt"; FileOutputStream fileOutputStream = null; try { //得到 FileOutputStream对象 对象 //老师说明 //1. new FileOutputStream(filePath) 创建方式,当写入内容是,会覆盖原来的内容 //2. new FileOutputStream(filePath, true) 创建方式,当写入内容是,是追加到文件后面 fileOutputStream = new FileOutputStream(filePath, true); //写入一个字节6 //fileOutputStream.write('H');// //写入字符串 String str = "hello,world,Demo!"; //str.getBytes() 可以把 字符串-> 字节数组 //fileOutputStream.write(str.getBytes()); /* write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流 */ fileOutputStream.write(str.getBytes(), 0, str.length()); } catch (IOException e) { e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }}
演示结果
3.文件拷贝
//完成 文件拷贝,将 e:\demo.jpg 拷贝 e:\demo2.jpg
//思路分析
//1. 创建文件的输入流 , 将文件读入到程序
//2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件.
try {
//文件输入流
fileInputStream = new FileInputStream(srcFilePath);
//文件输出流
fileOutputStream = new FileOutputStream(destFilePath);
//定义一个字节数组,提高读取效果
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = fileInputStream.read(buf)) != -1) {
//读取到后,就写入到文件 通过 fileOutputStream
//即,是一边读,一边写
fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法
package com.outputstream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;/** * @version 1.0 * @auther Demo龙 */public class FileCopy { public static void main(String[] args) { //完成 文件拷贝,将 e:\\demo.jpg 拷贝 e:\\demo2.jpg //思路分析 //1. 创建文件的输入流 , 将文件读入到程序 //2. 创建文件的输出流, 将读取到的文件数据,写入到指定的文件. String srcFilePath = "e:\\demo.jpg"; String destFilePath = "e:\\demo2.jpg"; FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { //文件输入流 fileInputStream = new FileInputStream(srcFilePath); //文件输出流 fileOutputStream = new FileOutputStream(destFilePath); //定义一个字节数组,提高读取效果 byte[] buf = new byte[1024]; int readLen = 0; while ((readLen = fileInputStream.read(buf)) != -1) { //读取到后,就写入到文件 通过 fileOutputStream //即,是一边读,一边写 fileOutputStream.write(buf, 0, readLen);//一定要使用这个方法 } System.out.println("拷贝ok~"); } catch (IOException e) { e.printStackTrace(); } finally { try { //关闭输入流和输出流,释放资源 if (fileInputStream != null) { fileInputStream.close(); } if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }}
演示结果