本文主要是介绍IO流(一) FileInputStream与FileOutputStream,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一.FileInputStream
1.read()
package com.io;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;/*java.io.FileInputStream:1.文件字节输入流,万能的,任何类型的文件都可以采用这个流来读2.字节的方式,完成输入的操作,完成读的操作(硬盘-->内存)*/
public class FileInputStreamTest01 {public static void main(String[] args) {//创建文件字节输入流对象FileInputStream fis = null;try {fis = new FileInputStream("D:\\code\\untitled\\temp.txt");//int readData = fis.read();//这个方法的返回值是读取到的字节本身 //返回值为-1时表示抵达文件末尾//System.out.println(readData);//97int readData = 0;while ((readData = fis.read()) != -1) {//使用while读取所有内容System.out.println(readData);}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//在finally语句块中确保流的关闭if (fis != null) {//关闭的前提是:流不是空,流是null的时候没必要关闭 避免空指针异常try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
}
2.read(byte[] b)
package com.io;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;/*
int read(byte[]b)一次最多读取b.length个字节减少硬盘和内存的交互,提高程序的执行效率往byete[]数组中读*/
public class FileInputStreamTest03 {public static void main(String[] args) {FileInputStream fis =null;try {fis = new FileInputStream("temp.txt");//IDEA中的默认路径为当前project的根//开始读,采用byte数组,一次读取多个字节,最多读取length个字节byte[] bytes = new byte[4];//一次读取4个字节int readCount = fis.read(bytes);//这个方法的返回值是读取到的字节的数量System.out.println(readCount);//4//System.out.println(new String(bytes));//abcdSystem.out.println(new String(bytes,0,readCount));//abcdreadCount = fis.read(bytes);System.out.println(readCount);//2 第二次只能读取到俩个字节//System.out.println(new String(bytes));//efcdSystem.out.println(new String(bytes,0,readCount));//efreadCount = fis.read(bytes);System.out.println(readCount);//-1 读取不到一个字节就返回-1} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
}
可使用while循环读取
public static void main(String[] args) {FileInputStream fis = null;try {fis = new FileInputStream("temp.txt");int readCount = 0;byte[] bytes = new byte[4];while ((readCount = fis.read(bytes)) != -1) {//把byte数组转换成字符串 输出System.out.print(new String(bytes, 0, readCount));}} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
3.FileInputStream 的其他常用方法
package com.io;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;/*FileInput类的其他常用方法int available() : 返回流中剩余没有读到的字节数量long skip(long n):跳过几个字节不读*/
public class FileInputStreamTest05 {public static void main(String[] args) {FileInputStream fis = null;try {fis= new FileInputStream("temp.txt");//读一个字节int readByte = fis.read();//还剩余可以读的字节数量:5System.out.println("剩下 "+fis.available()+" 个数量没有读");//5//这个方法的用处byte[] bytes = new byte[fis.available()];//这种方法不适合太大的文件 因为byte数组不能太大//不需要循环 ,直接读一次可以读取所有数据int readCount = fis.read(bytes);System.out.println(new String(bytes));//fis.skip(3);//跳过} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
}
二.FileOutputStream
write
package com.io;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;public class FileOutputStreamTest01 {public static void main(String[] args) {FileOutputStream fos = null;try {//myfile文件不存在时会自动新建//这种方式会将与原文件清空再写入//fos = new FileOutputStream("myfile");//以追加的方式在文件末尾写入,不会清空原文件内容fos= new FileOutputStream("myfile",true);byte[] bytes = {97,98,99,100};//将byte数组全部写出fos.write(bytes);//将byte数组的一部分写出fos.write(bytes,0,2);//写出字符串String s ="tjn是真的帅";byte[] bytes1 = s.getBytes();fos.write(bytes1);fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
}
三.文件拷贝
package com.io;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;/*使用FileInputStream与FileOutputStream完成文件的拷贝拷贝的过程是一边写,一边读使用以上字节流可以拷贝任何文件*/
public class copy01 {public static void main(String[] args) {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("C:\\Users\\Administrator\\Pictures\\Feedback\\png\\1.png");fos = new FileOutputStream("D:\\1.png");byte[] bytes = new byte[1024 * 1024];//一次拷贝最多1M(根据数组大小决定)int readCount = 0;while ((readCount = fis.read(bytes)) != -1) {fos.write(bytes, 0, readCount);}fos.flush();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//fis与fos分开try//一起try的时候,其中一个出现异常可能会影响另外一个流的关闭if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}
}
这篇关于IO流(一) FileInputStream与FileOutputStream的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!