------- android培训、java培训、期待与您交流! ----------
(一)IO就是在不同设备间传递数据。
•Java对数据的操作是通过流的方式。
•Java用于操作流的对象都在IO包中。
•相对路径与绝对路径:
•绝对路径:从盘符开始指定的物理路径。是真正文件在硬盘上的位置。
•相对路径:相对于某个指定位置的路径。
-如c:/a/b/c.txt文件。
-相对于c:的路径为a/b/c.txt
-c.txt相对于c:/a/b的路径为直接的c .txt
•代码中Windows中路径分隔可使用/或者\\。
(二)IO流分类:
A:流向
输入流 读取数据
输出流 写出数据
B:数据类型
字节流
字节输入流 InputStream
字节输出流 OutputStream
字符流
字符输入流 Reader
字符输出流 Writer
注意:
A:四个顶层类都是抽象类。
B:一般每个体系的流都是以基类作为后缀名。
(三)字节流
字节流是“万能流”,可以对所有文件进行操作
InputStream 字节输入流根类
|--FileInputStream 普通字节输入流
|--BufferedInputStream 高效字节输入流
OutputStream 字节输出流根类
|--FileOutputStream 普通字节输出流
|--BufferedOutputSteam 高效字节输出流
/*
*
* OutputStream:此抽象类是表示输出字节流的所有类的超类。
* FileOutputStream: OutputStream的子类 文件输出流是用于将数据写入 File的输出流。
*
* 构造:
* public FileOutputStream(String name) throws FileNotFoundException 通过文件字符串的名称,写出到一个文件中
* public FileOutputStream(File file)throws FileNotFoundException 通过文件对象,写出到一个文件中
*
* 普通方法:
* public void write(int b) throws IOException 一次输出一个字节
* public void write(byte[] b) throws IOException 一次输出一个字节数组
* public void write(byte[] b, int off,int len) throws IOException 一次输出一个字节数组的一部分
*
* public void close() throws IOException 关闭流
*/
public class Demo01_OutputStream {
public static void main(String[] args) throws IOException {
//创建流对象
FileOutputStream fos = null;
fos = new FileOutputStream("a.txt");
// File file = new File("a.txt");
// FileOutputStream fos2 = new FileOutputStream(file);
//写出或读取数据
fos.write(97);
String s = "i love java";
byte[] bytes = s.getBytes();
System.out.println(Arrays.toString(bytes));
fos.write(bytes);
fos.write(bytes,2,2);
fos.close();
}
}
/*
*
* InputStream:此抽象类是表示字节输入流的所有类的超类。
* FileInputStream: InputStream的子类 从文件系统中的某个文件中获得输入字节。
*
* 构造:
* public FileInputStream(String name) throws FileNotFoundException 通过文件字符串的名称,从一个文件中读
* public FileInputStream(File file) throws FileNotFoundException 通过文件对象,从一个文件中读
*
* 普通方法:
* public int read() throws IOException 一次读取一个字节 返回为读取到的那个字节 如果读取到文件末位,返回-1
* public int read(byte[] b)throws IOException 一次读取一个字节组
* byte[] b:最终读取到的内容存放的
* 返回值:本次读到的字节个数
*/
public class Demo02_InputStream {
public static void main(String[] args) throws IOException {
//创建流对象
FileInputStream fis = new FileInputStream("a.txt");
//读入或者写出
/*
*
一次读取一个字节
//System.out.println(fis.read());
//定义变量,用于临时存储读取到的字节
// int b;
//
// while((b=fis.read())!=-1) {
// System.out.println((char)b);
// }
*
*/
//定义变量,用于临时存储读取到的字节
byte[] bytes = new byte[3];
//定义变量,记录本次读取到的字节个数,用于判断循环是否结束
int len;
while((len=fis.read(bytes))!=-1) {
System.out.println("本次读取到"+len+"个字节");
System.out.println("本次读取到的内容是:"+Arrays.toString(bytes));
String s = new String(bytes,0,len);
System.out.println(s);
}
//关闭流
fis.close();
}
}
(四)字符流
Reader 字符输入流根类
|--InputStreamReader 字符转换流
|--FileReader 普通字符输入流
|--BufferedReader 高效字符输入流
Writer 字符输出流根类
|--OutputStreamWriter 字符转换流
|--FileWriter 普通字符输出流
|--BufferedWriter 高效字符输出流
l转换流OutputStreamWriter:字符流通向字节流的桥梁。
l转换流InputStreamReader:字节流通向字符流的桥梁。
lInputStreamReader是Reader的子类,是FileReader的父类,是字符流的一种。
lOutputStreamWriter是Writer的子类,是FileWriter的父类,是字符流的一种。
/*
* Writer:字符输出流根类
*
* FileWriter:用来写入字符文件的便捷类。
*
* 普通方法:
* public void write(int c) throws IOException 一次写出一个字符
* public void write(char[] cbuf) throws IOException 一次写出一个字符数组
* public void write(char[] cbuf,int index,int len) throws IOException 一次写出一个字符数组的一部分
*
* public void write(String str) throws IOException 一次写出一个字符串
*/
public class Demo03_Writer {
public static void main(String[] args) throws IOException {
//创建流对象
FileWriter fw = new FileWriter("c.txt");
//写出数据
fw.write('中');
fw.write('a');
fw.write(97);
char[] chars = new char[]{'a','b','c'};
fw.write(chars);
fw.write(chars, 0, 2);
fw.write("我爱Java");
// fw.flush();
//关闭流
fw.close();
}
}
/*
* Reader:字符输入流根类
*
* FileReader:用来读入字符文件的便捷类。
*
* 普通方法:
* public int read() throws IOException 一次读取一个字符
* public int read(char[] cbuf) throws IOException 一次读取一个字符数组
*/
public class Demo04_Reader {
public static void main(String[] args) throws IOException {
//创建IO流对象
FileReader fr = new FileReader("c.txt");
//读取
// //一次读取一个字符
// int c;
// while((c=fr.read())!=-1) {
// System.out.println((char)c);
// }
//一次读取一个字符数组
char[] chars = new char[1024];
int len;
while((len=fr.read(chars))!=-1) {
System.out.println(chars);
System.out.println(new String(chars, 0, len));
}
//关闭流
fr.close();
}
}
(五)练习
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
* 九种复制方法
*/
public class Demo_nine_Copy {
public static void main(String[] args) throws IOException {
long currentTimeMillis = System.currentTimeMillis();
method1();
long currentTimeMillis2 = System.currentTimeMillis();
method2();
long currentTimeMillis3 = System.currentTimeMillis();
method3();
long currentTimeMillis4 = System.currentTimeMillis();
method4();
long currentTimeMillis5 = System.currentTimeMillis();
method5();
long currentTimeMillis6 = System.currentTimeMillis();
method6();
long currentTimeMillis7 = System.currentTimeMillis();
method7();
long currentTimeMillis8 = System.currentTimeMillis();
method8();
long currentTimeMillis9 = System.currentTimeMillis();
method9();
long currentTimeMillis10 = System.currentTimeMillis();
System.out.println("普通字节流 一次一个字节: "+(currentTimeMillis2-currentTimeMillis));
System.out.println("普通字节流 一次一个字节数组: "+(currentTimeMillis3-currentTimeMillis2));
System.out.println("高效字节流 一次一个字节: "+(currentTimeMillis4-currentTimeMillis3));
System.out.println("高效字节流 一次一个字节数组: "+(currentTimeMillis5-currentTimeMillis4));
System.out.println("普通字符流 一次一个字符: "+(currentTimeMillis6-currentTimeMillis5));
System.out.println("普通字符流 一次一个字符数组: "+(currentTimeMillis7-currentTimeMillis6));
System.out.println("高效字节流 一次一个字符数组: "+(currentTimeMillis8-currentTimeMillis7));
System.out.println("高效字节流 一次一个字符: "+(currentTimeMillis9-currentTimeMillis8));
System.out.println("高效字节流 一次一行: "+(currentTimeMillis10-currentTimeMillis9));
}
//普通字节流 一次一个字节
public static void method1() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
int b=0;
while((b=fis.read())!=-1){
fos.write(b);
}
fos.close();
fis.close();
}
//普通字节流 一次一个字节数组
public static void method2() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
int len=0;
byte[] bytes = new byte[1024];
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
//高效字节流 一次一个字节
public static void method3() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int b=0;
while((b=bis.read())!=-1){
bos.write(b);
}
bos.close();
bis.close();
}
//高效字节流 一次一个字节数组
public static void method4() throws IOException{
FileInputStream fis = new FileInputStream("c.txt");
FileOutputStream fos = new FileOutputStream("d.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len=0;
byte[] bytes = new byte[1024];
while((len=bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
//普通字符流 一次一个字符
public static void method5() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
int b=0;
while((b=fis.read())!=-1){
fos.write(b);
}
fos.close();
fis.close();
}
//普通字符流 一次一个字符数组
public static void method6() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
int len = 0;
char[] bytes = new char[1024];
while((len=fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
//高效字符流 一次一个字符数组
public static void method7() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
BufferedReader br = new BufferedReader(fis);
BufferedWriter bw = new BufferedWriter(fos);
int len = 0;
char[] bytes = new char[1024];
while((len=br.read(bytes))!=-1){
bw.write(bytes,0,len);
}
bw.close();
br.close();
}
//高效字符流 一次一个字符
public static void method8() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
BufferedReader br = new BufferedReader(fis);
BufferedWriter bw = new BufferedWriter(fos);
int b = 0;
while((b=br.read())!=-1){
bw.write(b);
}
bw.close();
br.close();
}
//高效字符流 一次一行
public static void method9() throws IOException{
FileReader fis = new FileReader("c.txt");
FileWriter fos = new FileWriter("d.txt");
BufferedReader br = new BufferedReader(fis);
BufferedWriter bw = new BufferedWriter(fos);
String str;
while((str=br.readLine())!=null){
bw.write(str);
bw.newLine();
}
bw.close();
br.close();
}
}
运行结果:
效率分析:
1.高效流复制效率明显普通流
2.一次一个数组效率高于一次一个字节或字符
(六)转换流
/*
* System.in:“标准”输入流 是字节输入流InputStream的一种。通常,此流对应于键盘输入
*/
public class Demo07_System_in {
public static void main(String[] args) throws IOException {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while((line=br.readLine())!=null) {
if("byebye".equals(line)) {
System.out.println("欢迎下次光临");
break;
}
System.out.println(line);
}
isr.close();
}
}
/*
* System.out:“标准”输出流。 是字节输出流OutputStream的一种。通常,此流对应于控制台输出
*/
public class Demo08_System_out {
public static void main(String[] args) throws IOException {
OutputStream os = System.out;
OutputStreamWriter osw = new OutputStreamWriter(os);
osw.write("我认为,IO学完更有意思了!");
osw.close();
}
}
(七)对象序列化流
/*
* 对象序列化流:
* 序列化流:ObjectOutputStream
* 反序列化流:ObjectInputStream
*
* Serializable:类通过实现 java.io.Serializable 接口以启用其序列化功能。
*
* 序列化与反序列化的序列化ID必须相同
*/
public class Demo05_Serializable {
public static void main(String[] args) throws IOException, Exception {
Person p = new Person("唐嫣",26);
OutputStream os = new FileOutputStream("d.txt");
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(p);
oos.close();
InputStream is = new FileInputStream("d.txt");
ObjectInputStream ois = new ObjectInputStream(is);
Object readObject = ois.readObject();
System.out.println(readObject);
ois.close();
}
}
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
// private static final long serialVersionUID = -8074037887726454614L;
// private static final long serialVersionUID = 250l;
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
(八) IO 流相关类 ——Properties
/*
* Properties:表示了一个持久的属性集
*
* 属性列表中每个键及其对应值都是一个字符串。
*
* 构造方法:
* public Properties()
*
* 特殊方法:
* public Object setProperty(String key, String value) 属性集赋值
* public String getProperty(String key) 属性集取值
* public String getProperty(String key, String defaultValue) 属性集取值
*/
public class Demo06_Properties {
public static void main(String[] args) {
Properties prop = new Properties();
prop.setProperty("heroName", "wukong_sun");
prop.setProperty("HP", "1500");
prop.setProperty("MP", "1000");
System.out.println(prop);
System.out.println(prop.getProperty("heroName"));
System.out.println(prop.getProperty("HP"));
System.out.println(prop.getProperty("MP"));
System.out.println(prop.getProperty("SP","sorry"));
}
}
/*
* 使用Properties完成数据存储,并且完成遍历,最好使用Properties特有的方法进行遍历。
*
* public Set<String> stringPropertyNames()返回此属性列表中的键集
*
* Properties与IO相关的方法
* public void list(PrintStream out) 将属性列表输出到指定的输出流。此方法对调试很有用。
* public void store(Writer writer, String comments) throws IOException 将属性列表输出到指定的输出流
* public void load(Reader reader) throws IOException 读取属性集
*/
public class Test {
public static void main(String[] args) throws IOException {
method2();
}
//属性集从Properties到文件
public static void method() throws IOException {
Properties prop = new Properties();
prop.setProperty("heroName", "wukong_sun");
prop.setProperty("HP", "1500");
prop.setProperty("MP", "1000");
Set<String> keys = prop.stringPropertyNames();
for (String key : keys) {
String value = prop.getProperty(key);
System.out.println(key+"="+value);
}
PrintWriter pw = new PrintWriter("e.properties");
prop.list(pw);
pw.close();
}
//属性集从文件到Properties集合
public static void method2() throws IOException {
Properties prop = new Properties();
Reader reader = new FileReader("e.properties");
prop.load(reader);
reader.close();
System.out.println(prop);
}
}