java是怎么判断数据流读完了_java 数据流操作

File类

概述

文件和目录路径名的抽象表示,主要用于文件和目录的创建、查找和删除等操作。

构造方法

public File(String pathname); // 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。

public File(String parent, String child); // 从父路径名字符串和子路径名字符串创建新的 File实例。

public File(File parent, String child); // 从父抽象路径名和子路径名字符串创建新的 File实例。

常用方法

public String getAbsolutePath(); // 返回此File的绝对路径名字符串。

public String getPath(); // 将此File转换为路径名字符串。

public String getName(); // 返回由此File表示的文件或目录的名称。

public long length(); // 返回由此File表示的文件的长度

File f = new File("d:/aaa/bbb.java");

f.getAbsolutePath();

判断功能的方法

public boolean exists(); // 此File表示的文件或目录是否实际存在。

public boolean isDirectory(); // 此File表示的是否为目录。

public boolean isFile(); // 此File表示的是否为文件

File f = new File("d:/aaa/bbb.java");

f.exists();

创建删除功能的方法

public boolean createNewFile(); //当且仅当具有该名称的文件尚不存在时,创建一个新的空文件。

public boolean delete(); // 删除由此File表示的文件或目录。

public boolean mkdir(); // 创建由此File表示的目录。

public boolean mkdirs(); // 创建由此File表示的目录,包括任何必需但不存在的父目录

目录的遍历

public String[] list(); // 返回一个String数组,表示该File目录中的所有子文件或目录。 public File[] listFiles(); // 返回一个File数组,表示该File目录中的所有的子文件或目录。

OutputStream

OutputStream: 字节输出流

共性功能方法

public void close(); // 关闭此输出流并释放与此流相关联的任何系统资源。

public void flush(); // 刷新此输出流并强制任何缓冲的输出字节被写出。

public void write(byte[] b); // 将 b.length字节从指定的字节数组写入此输出流。

public void write(byte[] b, int off, int len); // 从指定的字节数组写入 len字节,从偏移量 off开始输 出到此输出流。

public abstract void write(int b); // 将指定的字节输出流。

InputStream

InputStream: 字节输入流

共性功能方法

public void close(); //关闭此输入流并释放与此流相关联的任何系统资源。

public abstract int read(); //从输入流读取数据的下一个字节。

public int read(byte[] b); // 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。

FileOutputStream

FileOutputStream: 文件输出流,用于将数据写出到文件。

构造方法

public FileOutputStream(File file, boolean append); // 创建文件输出流以写入由指定的 File对象表示的文件。参数中都需要传入一个boolean类型的值, true 表示追加数据, false 表示清空原有数据

File file = new File("a.txt", "true"); // 使用File对象创建流对象 true追加数据

FileOutputStream fos = new FileOutputStream(file);

public FileOutputStream(String name, boolean append); // 创建文件输出流以指定的名称写入文件。参数中都需要传入一个boolean类型的值, true 表示追加数据, false 表示清空原有数据

FileOutputStream fos = new FileOutputStream("b.txt" "flase"); // 使用文件名称创建流对象

写出字节

FileOutputStream fos = new FileOutputStream("fos.txt");

write(int b)

fos.write(97); // 写出数据

写出字节数组

FileOutputStream fos = new FileOutputStream("fos.txt");

byte[] b = "abcde".getBytes(); // 字符串转换为字节数组

fos.write(b); // 写出字节数组数据

写出指定长度字节数组

FileOutputStream fos = new FileOutputStream("fos.txt");

byte[] b = "abcde".getBytes();

fos.write(b,2,2); // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。

FileInputStream

FileInputStream:文件输入流,从文件中读取字节。

构造方法

FileInputStream(File file); // 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件系 统中的 File对象 file命名。

File file = new File("a.txt"); // 使用File对象创建流对象

FileInputStream fos = new FileInputStream(file);

FileInputStream(String name); // 通过打开与实际文件的连接来创建一个 FileInputStream ,该文件由文件 系统中的路径名 name命名。

FileInputStream fos = new FileInputStream("b.txt"); // 使用文件名称创建流对象

读取字节

FileInputStream fis = new FileInputStream("read.txt"); // 使用文件名称创建流对象

int read = fis.read(); // 读取数据,返回一个字节

System.out.println((char) read);

read = fis.read(); // 读取到末尾,返回‐1

使用字节数组读取

FileInputStream fis = new FileInputStream("read.txt"); // 使用文件名称创建流对象.

byte[] b = new byte[2]; // 定义字节数组,作为装字节数据的容器

fis.read(b); // 读取文件

Reader

Reader: 字符输入流

共性功能方法

public void close(); // 关闭此流并释放与此流相关联的任何系统资源。

public int read(); // 从输入流读取一个字符。

public int read(char[] cbuf); // 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中

Writer

Writer: 字符输出流

共性功能方法

void write(int c); // 写入单个字符。

void write(char[] cbuf); // 写入字符数组。

abstract void write(char[] cbuf, int off, int len); //写入字符数组的某一部分,off数组的开始索引,len 写的字符个数。

void write(String str); // 写入字符串。

void write(String str, int off, int len); // 写入字符串的某一部分,off字符串的开始索引,len写的字符个 数。

void flush(); // 刷新该流的缓冲。

void close(); // 关闭此流,但要先刷新它。

FileReader

FileReader:读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区

构造方法

FileReader(File file): // 创建一个新的 FileReader ,给定要读取的File对象。

File file = new File("a.txt");

FileReader fr = new FileReader(file); // 使用File对象创建流对象

FileReader(String fileName): // 创建一个新的 FileReader ,给定要读取的文件的名称。

FileReader fr = new FileReader("b.txt"); // 使用文件名称创建流对象

读取字符

FileReader fis = new FileReader("read.txt"); // 使用文件名称创建流对象

int read = fis.read(); // 读取数据,返回一个字节

System.out.println((char) read);

read = fis.read(); // 读取到末尾,返回‐1

使用字符数组读取

FileReader fis = new FileReader("read.txt"); // 使用文件名称创建流对象.

byte[] b = new byte[2]; // 定义字节数组,作为装字节数据的容器

fis.read(b); // 读取文件

FileWriter

FileWriter: 写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

构造方法

FileWriter(File file, boolean append); // 创建一个新的 FileWriter,给定要读取的File对象。true 表示追加数据, false 表示清空原有数据

File file = new File("a.txt");

FileWriter fw = new FileWriter(file); // 使用File对象创建流对象

FileWriter(String fileName, boolean append); // 创建一个新的 FileWriter,给定要读取的文件的名称。true 表示追加数据, false 表示清空原有数据

FileWriter fw = new FileWriter("b.txt"); // 使用文件名称创建流对象

写出字符

FileWriter fw = new FileWriter("fw.txt");

write(int b)

fw.write(97); // 写出数据

写出字符数组

FileWriter fw = new FileWriter("fw.txt");

byte[] b = "abcde".getBytes(); // 字符串转换为字节数组

fw.write(b); // 写出字节数组数据

写出指定长度字符数组

FileWriter fw = new FileWriter("fw.txt");

byte[] b = "abcde".getBytes();

fw.write(b,2,2); // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。

写出字符串

FileWriter fos = new FileWriter("fw.txt");

fw.write("黑马"); // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。

关闭和刷新

flush: 刷新缓冲区,流对象可以继续使用。

close: 先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。

属性集

Properties类

构造方法

public Properties(): 创建一个空的属性列表。

Properties properties = new Properties(); // 创建属性集对象

基本的存储方法

public Object setProperty(String key, String value); // 保存一对属性。

public String getProperty(String key); // 使用此属性列表中指定的键搜索属性值。

public Set stringPropertyNames(); // 所有键的名称的集合。

Properties properties = new Properties(); // 创建属性集对象

properties.setProperty("filename", "a.txt"); // 添加键值对元素

与流相关的方法

public void load(InputStream inStream): 从字节输入流中读取键值对。

Properties pro = new Properties(); // 创建属性集对象

pro.load(new FileInputStream("read.txt")); // 加载文本中信息到属性集

BufferedInputStream

public BufferedInputStream(InputStream in): // 创建一个 新的缓冲输入流。

BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt")); // 创建字节缓冲输入流

BufferedOutputStream

public BufferedOutputStream(OutputStream out); // 创建一个新的缓冲输出流。

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt")); // 创建字节缓冲输出流

BufferedReader

public BufferedReader(Reader in) :创建一个 新的缓冲输入流。

BufferedReader br = new BufferedReader(new FileReader("br.txt")); // 创建字符缓冲输入流

特有方法

BufferedReader: public String readLine() : 读一行文字。读取到最后返回null

BufferedReader br = new BufferedReader(new FileReader("in.txt"));

br.readLine(); // 读取,读取到最后返回null

BufferedWriter

public BufferedWriter(Writer out) : 创建一个新的缓冲输出流。

BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt")); // 创建字符缓冲输出流

特有方法

BufferedWriter: public void newLine() : 写一行行分隔符,由系统属性定义符号

BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt")); // 创建流对象

bw.newLine(); // 写出换行

InputStreamReader

InputStreamReader: 是Reader的子类,是从字节流到字符流的桥梁。它读取字节,并使用指定 的字符集将其解码为字符

构造方法

InputStreamReader(InputStream in); // 创建一个使用默认字符集的字符流。

InputStreamReader isr = new InputStreamReader(new FileInputStream("in.txt"));

InputStreamReader(InputStream in, String charsetName); // 创建一个指定字符集的字符流

InputStreamReader isr2 = new InputStreamReader(new FileInputStream("in.txt") , "utf-8"); // 编码不区分大小写

OutputStreamWriter

OutputStreamWriter: 是Writer的子类,是从字符流到字节流的桥梁。使用指定的字符集将字符 编码为字节

OutputStreamWriter(OutputStream in); //创建一个使用默认字符集的字符流。

OutputStreamWriter isr = new OutputStreamWriter(new FileOutputStream("out.txt"));

OutputStreamWriter(OutputStream in, String charsetName); // 创建一个指定字符集的字符流。

OutputStreamWriter isr2 = new OutputStreamWriter(new FileOutputStream("out.txt") , "utf-8");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值