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
使用字节流读取文件
①第一种方式
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();