Java IO 编程

Java IO 编程

Java IO 操作主要指的是通过 Java 进行输入、输出操作。

Java 中所有操作类都存放在 java.io 包中。

1、文件操作类:File

进行文件自身的操作(例如:创建、删除等),只能依靠 java.io.File 类完成。

File类的常用方法类型描述
public File(String pathName)构造传递完整文件操作路径
public File(File parent, String child)构造设置父路径与子文件路径
public boolean createNewFile() throws IOException普通创建新文件
public boolean exists()普通判断给定路径是否存在
public boolean delete()普通删除指定路径的文件
public File getParentFile()普通取得当前路径的父路径
public boolean mkdirs()普通递归创建多级目录
public long length()普通取得文件大小,以字节为单位返回
public boolean isFile()普通判断给定路径是否是文件
public boolean isDirectory()普通判断给定路径是否是目录
public File[] listFiles()普通列出所有的路径以File类对象包装
public String[] list()普通列出指定目录中的全部内容

2、字节流与字符流

字节流:InputStream(输入字节流)、OutputStream(输出字节流)

字符流:Reader(输入字符流)、Writer(输出字符流)

四个操作流的类全部属于抽象类,因此使用这些类时,一定要通过子类对象的向上转型进行抽象类对象的实例化操作。

2.1 字节输出流

OutputStream 类的常用方法类型描述
public void close() throws IOException普通关闭字节输出流
public void flush() throws throws IOExcetion普通强制刷新
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普通输出部分字节数组数据
FileOutputStream 类的常用方法类型描述
public FileOutputStream(File file) throws FileNotFoundException构造将内容输出到指定的路径,如果文件已存在,则使用新的内容覆盖旧的内容
public FileOutputStream(File file,boolean append) throws FileNotFoundException构造如果将布尔参数设置为true,表示追加新的内容到文件中
OutputStream output = new FileOutputStream(file,true) ;		// 文件追加

// 采用单个字节的方式输出
 for (int x= 0 ; x < data.length ; x ++) {
        output.write(data[x]); 			// 内容输出
    }

2.2 字节输入流

InputStream 类的常用方法类型描述
public void close() throws IOException普通关闭字节输入流
public abstract int read() throws IOException普通读取单个字节,如果已经没有内容返回 -1
public int read(byte[] b) throws IOException普通将数据读取到字节数组中,同时返回读取长度
public int read(byte[] b, int off, int len) throws IOException普通将数据读取到部分字节数组中,指定数组接收的长度和数组的偏移量,同时返回读取长度
FileInputStream 类的常用方法类型描述
public FileInputStream(File file) throws FileNotFoundException普通设置要读取文件数据的路径

2.3 字符输出流

Writer 类的常用方法类型描述
public void close() throws IOException普通关闭字符输出流
public void flush() throws throws IOExcetion普通强制刷新
public Writer append(CharSequence csq)普通追加数据
public void write(String str) throws IOException普通输出字符串数据
public void write(char[] cbuf) throws IOException普通输出字符数组数据
FileWriter类的常用方法类型描述
public FileWriter(File file) throws IOException构造设置输出文件
public FileWriter(File file ,boolean append) throws IOException构造设置输出文件以及是否进行追加

2.4 字符输入流

Reader 类的常用方法类型描述
public void close() throws IOException普通关闭字符输入流
public int read() throws IOException普通读取单个数据
public int read() throws IOException普通读取单个字符
public int read(char[] cbuf) throws IOException普通读取数据到字符数组中,返回读取长度
public long skip(long n) throws IOException普通跳过字节长度
FileReader 类的常用方法类型描述
public FileReader(File file) throws FileNotFoundException构造定义要读取的文件路径

3、字节流与字符流的区别

1、字节流直接与终端文件进行数据交互,字符流需要将数据经过缓冲区处理才与终端文件数据交互。
2、在使用 OutputStream 输出数据时即使最后没有关闭输出流,内容也可以正常输出,但是反过来如果使用的是字符输出流 Writer ,在执行到最后如果不关闭输出流,就表示在缓冲区中处理的内容不会被强制清空,所以就不会输出数据。如果有特殊情况不能关闭字符输出流,可以使用 flush() 方法强制清空缓冲区。

在实际开发中流的选用原则
在开发中,对于字节数据处理是比较多的,例如:图片,音乐,电影,文字。而字符流最大的好处是它可以进行中文的有效处理。在开发中,如果要处理中文时应优先考虑字符流,如果没有中文问题,建议使用字节流。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值