I/O流的重要类

本文详细介绍了Java中的IO流,包括文件流如FileInputStream和FileOutputStream,转换流如InputStreamReader和OutputStreamWriter,缓冲流如BufferedReader和BufferedWriter,数据流DataInputStream和DataOutputStream,以及对象流ObjectInputStream和ObjectOutputStream。文章还讨论了不同方式读取文件的效率,文件复制的方法,以及如何使用标准输出流和序列化技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java.io包下需要掌握的流有16个

文件专属:

java.io.FileInputStream (重点!!) java.io.FileOutputStream (重点!!)

java.io.FileReader java.io.FileWriter

转换流:(将字节流转换成字符流,基本用不到)

java.io.InputStreamReader java.io.InputStreamWriter

缓冲流专属:

java.io.BufferedReader java.io.BufferedWriter

java.io.BufferedInputStream java.io.BufferedOutputStream

数据流专属:

java.io.DataInputStream java.io.DataOutputStream

标准输出流:

java.io.PrintWriter java.io.PrintStream(掌握!)

对象专属流:(掌握!)

java.io.ObjectInputStream java.io.ObjectOutputStream

  1. 使用字节流读取文件

①第一种方式

FileInputStream fi = new FileInputStream("文件路径");

int readData=0;

while( (readData=fi.read())!=-1 ){

sout( readData );

}

这样的方式效率太低下,内存和硬盘要一直交互,一个一个字节读

②第二种方式

FileInPutStream fi =null;

fi = new FileInOutStream;

Byte[] b = new Byte[8192];

int readData=0;

while( (readData=fi.read(b)) !=-1 ){

sout( new String(b,0,readData) );

}

这样的方式读取的效率比较高,一次性读取固定Byte数量的字节,减少内存和硬盘的交互次数,效率较高。

③第三种方式 available (读取总字节数量)

Byte[] bb= new Byte[fi.available()];

int readCount=fi.read(bb);

sout( new String(bb) );

这种方式不适合太大文件,因为byte不能太大

文件的复制:

从d盘复制到c盘 中间借助 内存 一边读一边写:

FileInPutStream fis=null;

FileOutPutStream fos = null;

fis=new FileInputStream("d/");

fos=new FileOutPutStream("c/");

Byte[] bb = new Byte[8192];

int readCount =0;

while( (readCount=fis.read(bb)) !=-1 ){

fos.write( bb,0,readCount );

}

缓冲字符流

FileRead fr=new FileRead( "c/" );

BufferedRead bu= new BufferefRead( fr );

String line=null;

while( (line=bu.readLine())!=null ){

sout(line);

}

文件字节流转换为字符流

BufferedRead br = new BufferedRead(new InputStreamRead( new FileinputStream( "d/" )));

String line =null;

while( (line=br.readLine()) !=null ){

sout(line);

}

数据流

DataOutputStream do = new DataOutputStream( new FileOutPutStream( "data" ) );

int i =23;

do.write(i);

DataInputStream di = new DataInputStream( new FileInPutStream( "data" ) );

di.readint();

这两个流只能配套使用,而且读的过程中,顺序必须和写的顺序一样

标准输出流

//指向一个日志文件

PrintStream out = new PrintStream( new FileOutPutStream("log.txt"), true);

//改变输出方向

System.Setout( out );

File:

获取父路径 getParent();

获取绝对路径: getAbsolutePath();

获取文件名: getName()

判断是否是一个目录: isDirectory()

获取文件最后一次修改时间:lastModified()

获取文件大小:length()

获取目录下的所有子目录:File[] listFiles()

文件的拷贝:

public class Test {

public static void main(String[] args) {

//拷贝源

File srcFile = new File("D:\\每天笔记");

//拷贝目标

File destFile = new File("E:\\");

//调用方法拷贝

copyDir(srcFile,destFile);

}

private static void copyDir(File srcFile,File destFile){

if (srcFile.isFile()){

//一边读一边写

FileInputStream in = null;

FileOutputStream out =null;

try {

in=new FileInputStream(srcFile);

String path=(destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() :

destFile.getAbsolutePath()+"\\")+srcFile.getAbsolutePath().substring(3);

out=new FileOutputStream(path);

byte[] bytes=new byte[1024 * 1024];

int count = 0;

while ((count=in.read(bytes))!=-1){

out.write(bytes,0,count);

}

out.flush();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}finally {

if (out !=null){

try {

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (in !=null){

try {

in.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

//获取文件下的所有文件

File[] files = srcFile.listFiles();

for (File ff : files){

if (ff.isDirectory()){

String srcDir = ff.getAbsolutePath();

String destDir=(destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath()+"\\")+srcDir.substring(3);

File newFile = new File(destDir);

if (!newFile.exists()){

newFile.mkdir();

}

}

//递归调用

copyDir(ff,destFile);

}

}

}

序列化和反序列化

序列化 ObjectoutputStream

反序列化 ObjectInputStream

//序列化

//Student 一定要实现序列化接口

Student ss = new Student(111,"sss");

ObjectOutPutStream out = new ObjectOutPutStream( new FileOutPutStream( "students" ) );

out.writeObject( ss );

//反序列化

ObjectInPutStream in = new ObjectInPutStream( new FileInPutStream( "students" ) );

in.readObject();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值