@TOC
文件流
节点流:
FileInputStream: 输入流
FileOutputStream:输出流
读的步骤:
public static void main(String[] args) throws IOException {
// f:/data/a.txt 读取 在控制台上展示 文件内容为ab
// 1. 创建流对象
// 引发: FileNotFoundException
// FileInputStream fin = new FileInputStream("f:/data/a.txt");
File f = new File("f:/data/a.txt");
FileInputStream fin = new FileInputStream(f);
// 2. 读 IOException
// 一次读取一个字节 ,返回的是 字符的整数值,读到文件尾 返回 -1
System.out.println((char)fin.read());//强制转换为char类型
System.out.println((char)fin.read());
System.out.println(fin.read());// -1
// 循环
int temp;
while((temp = fin.read() )!= -1){
System.out.print((char)temp);
}
// 3. 关闭流
fin.close();
}
文件中有中文时
处理方式一:转换流
InputStreamReader:把 字节流 转换成 字符流
public static void main(String[] args) throws IOException {
// f:/data/a.txt 读取 在控制台上展示
// 1. 创建流对象
// 引发: FileNotFoundException
// FileInputStream fin = new FileInputStream("f:/data/a.txt");
File f = new File("f:/data/a.txt");
FileInputStream fin = new FileInputStream(f);// 字节
// 把 fin转换成 字符流 ir (装饰者模式)
InputStreamReader ir = new InputStreamReader(fin);
// 2. 读 IOException
// 循环
int temp;
while((temp = ir.read() )!= -1){
System.out.print((char)temp);
}
// 3. 关闭流 关闭顺序应由内向外,若只关闭最外层的流,则内层流也会关闭
ir.close();
// fin.close();
// ir.close();
}
处理方式二:byte[]
public static void main(String[] args) throws IOException {
// 中文处理(二)
// 1.
File file = new File("f:/data/a.txt");
FileInputStream fin = new FileInputStream(file);
// 2. 读
//
byte [] b = new byte[fin.available()];// 自动获得流中的字节数,必须保证指针在流的开头
byte [] b = new byte[(int)file.length()];// 自动获得文件内容的字节数
// 把 读到的内容 存到 byte 数组中,返回读到的字节数
fin.read(b);
// 把字节数组转换成 字符串
String s = new String(b);
System.out.println(s);
// 3.
fin.close();
}
//从off位置开始存储len个字节到byte[]
int read(byte[] b, int off, int len)
使用try-cath-finally处理方式:
public class TestFileInputStream3 {
public static void main(String[] args) {
// 1.
FileInputStream fin = null;//fin的声明要在try内,否则无法在方法末尾关闭流
try {
fin = new FileInputStream("f:/data/a.txt");
// 2
byte [] b = new byte[fin.available()];// 自动获得文件内容的字节数
// 把 读到的内容 存到 byte 数组中,返回读到的字节数
// (字节数组,存储到字节数组的起始位置,存几个字节)
fin.read(b, 2, 2);
// 把字节数组转换成 字符串
String s = new String(b);
System.out.println(s);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
// 3
if(fin != null){
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
输出
FileOutputStream
public static void main(String[] args) throws IOException {
// String s = "hello你好" 存到 f:/data/b.txt
// 1. FileNotFoundException
// 追加 true,覆盖 false(默认)
FileOutputStream fout = new FileOutputStream("f:/data/b.txt",true);
// 2. 写 IOException
String s = "hello你好";
// (1) 写一个字节
// fout.write(97);
// (2) 写一个字节数组的内容
byte [] b = s.getBytes();//获得字符串的byte数组
// fout.write(b);
// (3) (字节数组,从哪里写,写几个字节)
fout.write(b, 2, 2);
// 3.
fout.close();
}
缓冲流
处理流,处理字节流
BufferedInputStream
BufferedOuputStream
提供 缓冲区,字节数组 ,提高效率。
public static void main(String[] args) throws IOException {
// 复制图片
// 1.
FileInputStream fin = new FileInputStream("f:/data/datou.jpg");
/* 提供一个 读的缓冲区
* private static int DEFAULT_BUFFER_SIZE = 8192; 默认缓冲区大小 8192字节
* buf = new byte[size];
*/
BufferedInputStream bfin = new BufferedInputStream(fin);//包装fin
FileOutputStream fout = new FileOutputStream("f:/data/datou1.jpg");
/*
* 提供一个写的缓冲区
* this(out, 8192);
* buf = new byte[size];
*/
BufferedOutputStream bfout = new BufferedOutputStream(fout);
// 2.
int temp;
while((temp = bfin.read()) != -1){
bfout.write(temp);
}
bfout.flush();// 刷新缓冲区 不然数据可能无法写入
// 3.
bfin.close();
bfout.close();
}
Scanner 扫描器类
public static void main(String[] args) throws FileNotFoundException {
// has方法:
Scanner input = new Scanner(System.in);//in 是final静态InputStream对象
//System.in获取控制台内容到流中
if(input.hasNextInt()){//检测流中数据 输入的是不是数字
int n = input.nextInt();
System.out.println("运算" + (n + 55));
}else{
System.out.println("不是数字");
}
//------------------------------------------
// 参数:数据源
/*
* public final static InputStream in = null;
* 固定:
* System.in : 控制台读
* System.out: 写入控制台
* public final static PrintStream out = null;
*/
Scanner input = new Scanner(System.in);
String s = input.next();
System.out.println(s);
//-------------------------------------------
FileInputStream fin = new FileInputStream("f:/data/a.txt");
Scanner input = new Scanner(fin);
String s = input.next();// ab中文
System.out.println(s);
//---------------------------------------------
Scanner input = new Scanner("aa bb cc dd ee");
String str = input.next();// aa 不会读取空白
String str = input.nextLine();// aa bb cc dd ee 读取空白
System.out.println(str);
}
练习
package day0321;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class TestScanner2Exam {
public static void main(String[] args) throws IOException {
// 1.
Scanner input = new Scanner(System.in);
// BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream("f:/data/name.txt"));
FileOutputStream fout = new FileOutputStream("f:/data/name.txt");
BufferedOutputStream bfout = new BufferedOutputStream(fout);
// 2.
String name;
while(true){
System.out.println("-- 输入名字;");
name = input.next();
if(name.equals("q"))
break;
bfout.write(name.getBytes());
bfout.write("\r\n".getBytes());//写入回车
}
bfout.flush();
// 3.
input.close();
bfout.close();
}
}
对象流
永久性的保存对象。
处理流。
ObjectInputStream: 反序列化。 把字节序列还原成对象。
ObjectOutputStream: 序列化。 把对象以字节序列的方式保存到文件中。
前提:实现 Serializable。
class Student implements Serializable{
/**
* jvm会在运行时为类自动生成版本号,除非指定了版本号
* 类class的版本号:显示指定了版本号,那么JVM就不会自动生成了
*/
private static final long serialVersionUID = 1L;
private int no;
private String name;
private int age;
public Student(int no, String name) {
super();
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "Student [no=" + no + ", name=" + name + "]";
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// 序列化:把对象存到文件总obj.txt
// 1.序列化
Student guojing = new Student(11, "郭靖");
FileOutputStream fout = new FileOutputStream("f:/data/obj.txt");
ObjectOutputStream objOut = new ObjectOutputStream(fout);
objOut.writeObject(guojing);
objOut.close();
// 2. 反序列化
FileInputStream fin = new FileInputStream("f:/data/obj.txt");
ObjectInputStream objFin = new ObjectInputStream(fin);
Student stu = (Student) objFin.readObject();
System.out.println(stu);
objFin.close();
}
Q.E.D.