Java 输入输出抽象类,Java IO(输入/输出)

Java中输入/输出的类存放在java.io包中

输入流类都是抽象类InputStream(字节输入流)或抽象类Reader(字符输入流)的子类;

输出流类都是抽象类OutputStream(字节输出流)或抽象类 Writer(字符输出流)的子类。

1.输入流

1.1 InputStream(字节输入流)

常用方法:read()、close()等;该类中错误引发IOException异常。

close():关闭此输入流并释放与该流关联的所有系统资源

1.2 Writer(字符输出流)

处理字符文件

2. 输出流

2.1 OutputStream(字节输出流)

常用方法:write(),flush(),close()等;返回值为void,错误引发IOException异常。

flush():彻底完成输出并清空缓存区

close():关闭输出流

2.2 Reader(字符输入流)

处理字符文件

3. File类

java.io包中唯一代表磁盘文件本身的对象

调用File类中的方法,实现创建、删除、重命名文件等操作;

File类对象用来获取文件本身的信息,如:目录、长度、读写权限等

3.1 文件创建与删除

//方法1:new File(String pathname)

File file=new File("C:/Users/simou/Desktop/1.txt");

//方法2:new File(String parent,String child)

File file=new File("C:/Users/simou/Desktop","1.txt");

//方法3:new File(File f,String child)

File file=new File("C:/Users/simou/Desktop/","1.txt");

File f=new File("C:/Users/simou/Desktop/");

File file=new File(f,"1.txt");

3.2 获取文件信息

常用方法:getName(),canRead(),canWrite(),exits()/isFile(),length(),

getAbsolutePath()-获取绝对路径,

getParent()-获取父路径,

isDirectory()-文件时一个目录,

isHidden()-隐藏文件,

lastModified()-最后修改时间

4. 文件输入/输出流

4.1 FileInputStream类、FileOutputStream类 字节流

InputStream类->FileInputStream类;

OutputStream类->FileOutputStream.

常用构造方法:

File file=new File(name);

FileOutputStream out=new FileOutputStream(file);

String name="C:/Users/simou/Desktop/1.txt";

FileOutputStream out=new FileOutputStream(name);

package io;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

public class filewrite {

public static void main(String[] args) {

String name="C:/Users/simou/Desktop/1.txt";

File file=new File(name);

try{

FileOutputStream out=new FileOutputStream(file);

byte[] bt="helloworlder".getBytes();

out.write(bt);

out.close();

}catch (Exception e) {

e.printStackTrace();

}

try{

FileInputStream in=new FileInputStream(file);

byte[] te=new byte[1024];

int len=in.read(te);

System.out.println(new String(te,0,len));

System.out.println(file.length());

in.close();

}catch (Exception e) {

e.printStackTrace();

}

}

}

4.2 FileReader类、FileWriter类 字符流

Reader->InputStreamReader->FileReader

Writer->OutputStreamWriter->FileWriter

优势:只要不关闭流,每次调用read()方法就顺序读取源中其余的内容,知道源末尾或流被关闭.

用法:同FileInputStream类

5. 带缓存的输入/输出流

5.1 BufferedInputStream类、BufferedOutputStream类

InputStream类-FilterInputStream类>BufferedInputStream类

OutputStream类-FilterOutputStream类>BufferedOutputStream类

常用构造方法:

BufferedInputStream(InputStream in); //带有32字节的缓存流

BufferedInputStream(InputStream in,int size); //自定义缓存区的缓存流

优势:带缓存的输入/输出流使用方法flush()来将缓存区的数据强制输出完

读取文件过程

file->InputStream ->BufferedInputStream->目的地

5.2 BufferedReader类、BufferedWriter类

Reader->BufferedReader

Writer->BufferedWriter

常用方法:read(),readLine(),write(),flush();newLine()-写入一个行分隔符;

在使用BufferedWriter类的write()方法时,数据并没有立刻被写入至输出流,而是首先进入缓存区中;若要想立刻将缓存区的数据写入输出流,一定要调用flush()方法。

读取文件过程

字符数据->BufferedWriter->OutputStreamWriter->OutputStream ->file

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值