一文搞定Java文件处理

File
FileOutputStream
BufferedOutputStream
FileInputStream
BufferedInputStream
FileWriter
BufferedWriter
FileReader
BufferedReader
  • 外部操作
    文件名的操作
方法签名描述
public String getName()返回文件对象名字符串
public String toString()返回文件名字符串(通常与getName()相同,但可能会包含更多信息)
public String getParent()返回文件对象父路径名字符串
public File getParentFile()返回表示文件对象父路径的File对象(而不仅仅是字符串)
public String getAbsolutePath()返回文件对象的绝对路径名字符串

文件属性测试

方法签名描述
public boolean canRead()是否能读指定的文件
public boolean canWrite()是否能修改指定的文件
public boolean exists()指定的文件是否存在
public boolean isDirectory()指定的文件是否是目录
public boolean isFile()指定的文件是否是一般文件

一般文件信息和工具
public long length():返回文件的字节长度;
目录操作

方法签名描述
public boolean mkdir()创建指定的目录
public String[] list()返回指定目录下的文件
public String[] list(FilenameFilter filter)返回指定目录下满足文件过滤器的文件
  • 内部操作
    FileOutputStream输出字节流
    FileOutputStream fos=new FileOutputStream(“C:\Users\XuWei\Desktop\test\test.txt”);
    1.参数是字符串表示的路径或路径的字符串
    2.只要有父级路径,没有文件会新建文件;
    3.如果文件有内容,则会清空文件;
import java.io.FileOutputStream;  
import java.io.IOException;  
public class FileFunction {
//注意先写完完整方法再写throws异常;
    public static void main (String[] args) throws IOException{  
FileOutputStream fos=new FileOutputStream("C:\\Users\\XuWei\\Desktop\\test\\test.txt");
//参数是字符串表示的路径或路径的字符串
//文件路径注意是两条反斜杠
        fos.write(97);  
//由于是字节流输出,write里面字符是ASCLL码
        fos.close();  
//不close就会占用文件
    }   
}

public abstract void write(int b) throws IOException;
public void write(byte[] b) throws IOException;
public void write(byte[] b,int off,int len) throws IOException;

import java.io.FileInputStream;  
import java.io.IOException;  
public class FileFunction {  
    public static void main (String[] args) throws IOException{   
        FileInputStream fos1=new FileInputStream("src\\test.txt");  
        System.out.println((char) fos1.read());
        //由于是字节流输入,read直接返回的是Acsll码;
        //如果读不到数据则返回-1;  
        fos.close();  
    }  
}

public abstract int read() throws IOException;
public int read(byte[] b) throws IOException;
public int read(byte[] b,int off,int len) throws IOException;
字符流输入

import java.io.FileReader;  
import java.io.IOException;  
public class JavaFunction2 {  
    public static void main(String[] args) throws IOException {  
        FileReader fr = new FileReader("src\\test.txt");  
  int ch;  
        while ((ch = fr.read()) != -1)  
        {System.out.print((char) ch);}  
        fr.close();  
    }  
}

字节输入流和字符输入流Read比较

 public abstract int read(char[] cbuf,int off,int len){...};//字符输入流第一个参数类型为char
 public int read(byte[] b,int off, int len){...};//字节输入流第一个参数类型为int

字节缓冲区
缓冲流是对基本流的包装是高级流,但基本功能还是要依靠基本流,所以还是要创建基本流对象。

import java.io.*;  
public class Copy {  
    public static void main(String[] args)throws IOException{  
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream("src\\test.txt") );  
        //注意要创建文件的字节输入输出流
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("src\\copy.txt") );  
        int b;  
        while((b=bis.read())!=-1){  
            bos.write(b);  
        }  
        bis.close();  
        bos.close();  
    }  
}

字符缓冲流

import java.io.*;  
public class Copy {  
    public static void main(String[] args)throws IOException{  
        BufferedReader br=new BufferedReader((new FileReader("src\\test.txt")));  
String s;  
while((s=br.readLine())!=null){  
//注意nextLine的返回值是String类型
    System.out.println(s);  
}  
        br.close();  
    }  
}
import java.io.*;  
public class Copy {  
    public static void main(String[] args)throws IOException{  
        BufferedWriter bw=new BufferedWriter((new FileWriter("src\\test.txt")));  
bw.write("你嘴角上扬的样子,百度搜索不到");  
bw.newLine();  
        bw.write("你嘴角上扬的样子,百度搜索不到");  
        bw.close();  
    }  
}

字符流用缓冲流主要因为他们有两个方法,一个是输入的br.readLine()一个是输出的bw.newLine();
接口和对象串行化
对象串行化及序列化,也是高级流,需要依附于基础流完成;

import java.io.*;  
class Student1 implements Serializable{ 
//注意序列化一定要实现Serializable接口,表示他是可序列化的
    String name;  
    int age;  
    Student1(String name, int age){  
        this.name = name;  
        this.age = age;  
    }  
}  
public class ObjectFunction {  
    public static void main(String[] args) throws IOException,ClassNotFoundException{  
        Student1 s1 = new Student1("张三", 18);  
        Student1 s2 = new Student1("张三", 18);  
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("src\\test.txt"));  
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("src\\test.txt"));  //与缓冲流同样为高级流,
        oos.writeObject(s1);  
        oos.writeObject(s2);  
        System.out.println((Student1)ois.readObject());  
        oos.close();  
    }  
}

随机存取文件RandomAccessFile
构造方法
RandomAccessFile(String name,String mode)
RandomAccessFile(File file,String mode)
使用方法
接口DataInput
接口DataOutput
getFilePointer():返回当前文件指针位置;
Seek(long pos):把文件指针定位于pos的位置;
Length():得到文件的长度.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值