概念
1个字符 = 2个字节 1个字节=8个二进制
输入:把硬盘的数据,读取到内存中
输出:把内存的数据,写入到硬盘中
字节流
一切文件数据 都是以字节存储
字节输出流
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)
:将指定的字节输出流。
FileOutputStream:文件字节输出流
- 作用:把内存中的数据写入到硬盘中
- 构造方法 :
public FileOutputStream(File file)
:创建文件输出流以写入由指定的 File对象表示的文件。
public FileOutputStream(String name)
: 创建文件输出流以指定的名称写入文件。
构造方法作用:
- 创建一个FileOutputStream对象
- 会根据传递的文件/文件路径 创建一个空文件
- 会把FileOutputStream对象指向创建好的文件
写入数据的原理(内存->硬盘)
java程序->JVM->OS->调用写数据方法->数据写入文件
字节输出流步骤
- 创建一个FileOutputStream对象,构造方法中传递参数
- 调用FileOutputStream对象中的write方法,把数据写入文件中
- 释放资源
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("F://a.txt");
fileOutputStream.write(97);
fileOutputStream.close();
}
写出字节:write(int b)
方法,每次可以写出一个字节数据
写出字节数组:write(byte[] b)
,每次可以写出数组中的数据
追加写入:在构造方法中添true
public static void main(String[] args) throws IOException {
//在FileOutputStream("路径",true) append:true 等于追加
FileOutputStream fileOutputStream = new FileOutputStream("F://a.txt",true);
fileOutputStream.write("第一次追加写入".getBytes());
fileOutputStream.close();
}
字节输入流
读取硬盘中文件的字节
构造方法:
FileReader(File file)
: 创建一个新的 FileReader ,给定要读取的File对象。FileReader(String fileName)
: 创建一个新的 FileReader ,给定要读取的文件的名称。
方法:read()
public static void main(String[] args) throws IOException {
FileInputStream f = new FileInputStream("f:\\a.txt");
//读取文件中的一个字节并返回,读取到文件末尾返回-1
int len = f.read();
System.out.println(len);
f.close();
}
循环读取文件字节
public static void main(String[] args) throws IOException {
FileInputStream f = new FileInputStream("f:\\a.txt");
int len = 0;
//while循环读取字节,返回-1就结束
while((len = f.read())!=-1){
System.out.print((char)len); //abc
}
f.close();
}
读取多个字节方法 : int read(byte[] b)
字符流
字符输入流
使用字节流会出现小问题:读取中文可能读不全 (GDK占2个字节 UTF-8占3个字节)
Reader字符输入流,是字符输入流最顶层的父类,是一个抽象类
FileReader:文件字符输入流
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("F:\\a.txt");
char[] c = new char[1024];
int len = 0;
while((len = reader.read(c))!=-1){
System.out.println(new String(c,0,len));
}
reader.close();
}
字符输出流
FileWriter:文件字符输出流(把内存中字符数据写入到文件中)
public static void main(String[] args) throws IOException {
// 使用文件名称创建流对象
FileWriter fw = new FileWriter("f1.txt");
fw.write(97);
fw.write("你好");
fw.write("写入数据");
//注意:一定要释放资源,要不然只在缓冲区,不能保存到内容
fw.close();
}
}
续写和换行:
构造方法里的append
换行:\r\n
关闭与刷新
flush
:刷新缓冲区,流对象可以继续使用。
close
:先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了
flush后还能继续写入内容,close后就不能继续写入内容了
IO异常的处理
使用try catch finally
处理流中的异常
public static void main(String[] args) {
//定义变量
FileWriter fw = null;
//可能出现异常的代码块
try {
//创建字符输出流对象,并且追加
fw = new FileWriter("fw.txt",true);
for(int i =0;i<10;i++){
fw.write("hello"+i);
//换行
fw.write("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//释放资源
if(fw!=null){
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
JDK新特性
JDK7的新特性,在try后面加(),可以定义流对象,自动释放资源
public static void main(String[] args) {
try( FileInputStream fi = new FileInputStream("F://a.txt");
FileOutputStream fo = new FileOutputStream("a1.txt",true);) {
//输出到文件
int len = 0;
while((len=fi.read())!=-1){
fo.write(len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
JDK9的新特性,try的前边可以定义流的对象,try后边的()引入流的对象名称
格式:
A a = new A();
B b = new B();
try(a,b){
}catch(){
}
属性值
Properties 类 代表一个持久的属性值 唯一和IO流结合的集合
方法:
- store方法,把集合临时数据,持久化写入硬盘中存储
- load方法,把硬盘中保存的文件(键值对) 读取到集合中
Properties集合是一个双列集合,key vuale 默认都是字符串
- Object setProperty(String key,String value) 调用hashtable的方法put
- String getProperty(String key) 通过key值找到vaule
- Set stringPropertyNames() 返回属性列表的键值
public static void main(String[] args) {
//创建Properties对象
Properties properties = new Properties();
properties.setProperty("范冰冰","170");
properties.setProperty("杨幂","168");
properties.setProperty("余香","175");
//使用stringPropertyNames把键存储进Set集合
Set<String> set = properties.stringPropertyNames();
//循环取出key
for (String s : set) {
System.out.println(s+properties.getProperty(s));
}
}
使用字符输出流对Properties进行存储
public static void main(String[] args) throws IOException {
//创建Properties对象
Properties properties = new Properties();
properties.setProperty("范冰冰","170");
properties.setProperty("杨幂","168");
properties.setProperty("余香","175");
//创建FileWrite对象进行字符输出
FileWriter fw = new FileWriter("prop.txt");
//使用properties的store(Write流对象,”注释“)
properties.store(fw,"save date");
fw.close();
}s
使用字符输入流对Properties进行读取
public static void main(String[] args) throws IOException {
//使用Properties的load方法
Properties prop = new Properties();
prop.load(new FileReader("prop.txt"));
//遍历输出
Set<String> strings = prop.stringPropertyNames();
//取出key
for (String key : strings) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
}