IO
字节流:能够处理所有类型的数据,视频,文本,图片,音乐
字符流:处理纯文本,doc不是纯文本
代码1:
import org.junit.Test; import java.io.*; import java.util.Arrays; public class Demo10 { @Test public void test1() throws FileNotFoundException { //创建字节输出流对象,方式1 File file=new File("E:\\12.txt"); OutputStream ops = new FileOutputStream(file); System.out.println("-------------"); //创建字节输出流对象,方式2 OutputStream ops2 = new FileOutputStream("E:\\12.txt"); } @Test public void test2() throws IOException { OutputStream ops = new FileOutputStream("E:\\ab.txt");//不存在会创建文件 ops.write(66); ops.close(); OutputStream ops2 = new FileOutputStream("E:\\abc.txt"); String str1="同九年汝何秀"; byte[] strByte=str1.getBytes(); ops2.write(strByte); ops2.close(); OutputStream ops3 = new FileOutputStream("E:\\ab.txt"); byte[] bytes2 = {97,98,99,100}; System.out.println(Arrays.toString(bytes2));//[97, 98, 99, 100] ops3.write(bytes2,0,2); ops3.close();//关闭之后,文件内是ab OutputStream ops4 = new FileOutputStream("E:\\ab.txt",true); String str2="\n"; ops4.write(str2.getBytes()); String str3="同九年,汝更秀"; ops4.write(str3.getBytes()); ops4.close(); /* ab 同九年,汝更秀 * */ } }
代码2
import org.junit.Test; import java.io.*; public class Demo11 { @Test public void test1() throws FileNotFoundException { //第一种方式 File file1 = new File("E:\\ab.txt"); InputStream ips1 = new FileInputStream(file1); //第二种方式 InputStream ips2 = new FileInputStream("E:\\ab.txt"); } @Test public void test2() throws IOException { InputStream ips2 = new FileInputStream("E:\\a.txt"); int read1=ips2.read(); System.out.println(read1);//97 int read2=ips2.read(); System.out.println(read2);//98 int read3=ips2.read(); System.out.println(read3);//99 int read4=ips2.read(); System.out.println(read4);//-1,没有内容后输出-1 ips2.close(); } @Test //for循环遍历 public void test3() throws IOException { InputStream ips3=new FileInputStream("E:\\a.txt"); while (true){ int reads=ips3.read(); if (reads==-1){ break; } System.out.println((char)reads); } ips3.close(); } @Test public void test4() throws IOException { //循环遍历的另一种方式 InputStream ips3=new FileInputStream("E:\\a.txt"); int len=0; while ((len=ips3.read()) != -1){ System.out.println((char)len); } ips3.close(); } @Test public void test5() throws IOException { InputStream ips4=new FileInputStream("E:\\bcd.txt"); byte b[] = new byte[4]; /* int len = ips4.read(b); while (len!=-1){ String s = new String(b,0,len); System.out.println(s); len=ips4.read(b); } ips4.close(); */ int len=0; while ((len=ips4.read(b))!=-1){ String s = new String(b,0,len); System.out.println(s); } ips4.close(); } }
代码3
//文件复制 import java.io.*; public class Demo12 { public static void main(String[] args) { InputStream ips = null; OutputStream ops = null; try { //新建输入和输出流 ips = new FileInputStream("E:\\qr.jpg"); ops = new FileOutputStream("E:/qrcopy.jpg"); //读取数据,新建字节数组 byte[] b = new byte[20]; int len=0; while ((len=ips.read(b))!=-1){ ops.write(b,0,len); } System.out.println("复制完毕"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if (ops!=null){ try { ops.close(); } catch (IOException e) { e.printStackTrace(); } } if (ips!=null){ try { ips.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
代码4
import org.junit.Test; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; /* 字符输出流: Writer抽象类FileWriter 一次输出一个字符 * */ public class Demo13 { @Test public void test1() throws IOException { //第一种方式创建输出流对象 Writer writer = new FileWriter("E:\\qr.jpg"); //第二种方式创建输出流对象 Writer writer1 = new FileWriter(new File("E:\\qr.jpg")); } @Test public void test2() throws IOException { Writer writer = new FileWriter("E:\\qr.txt"); writer.write(97); writer.write("真相只有一个"); //writer.flush(); writer.close(); //a真相只有一个 } @Test public void test3() throws IOException { Writer writer = new FileWriter("E:\\qr.txt"); char[] ch = {'今','天','吃','什','么'}; //writer.write(ch); writer.write(ch,1,2); writer.close(); } }
代码5
import org.junit.Test; import java.io.*; import java.util.Arrays; /* 字符输入流 Reader:抽象类通过子类来实现 * */ public class Demo14 { @Test public void test1() throws FileNotFoundException { //第一种方式 Reader reader = new FileReader("E:\\qr.txt"); //第二种方式 Reader reader2 = new FileReader(new File("E:\\qr.txt")); } @Test public void test2() throws IOException { Reader reader = new FileReader("E:/qr.txt"); int read = reader.read(); System.out.println((char)read); int read2 = reader.read(); System.out.println((char)read2); int read3 = reader.read(); System.out.println((char)read3);//若超出返回-1 reader.close(); } @Test public void test3() throws IOException { Reader reader = new FileReader("E:/qr.txt"); //循环遍历1 while (true){ int read=reader.read(); if (read==-1){ break; } System.out.println((char)read); } reader.close(); //循环遍历2 Reader reader2 = new FileReader("E:/qr.txt"); int len=0; while ((len=reader2.read())!=-1){ System.out.println((char)len); } reader2.close(); //循环遍历3 Reader reader3 = new FileReader("E:/qr.txt"); char c[]=new char[4]; int length=reader3.read(c); while (length!=-1){ String s = new String(c,0,length); System.out.println(s); length=reader3.read(c); } reader3.close(); } }
代码6
import java.io.*;
/*
复制文件
* */
public class Demo15 {
public static void main(String[] args) {
//创建输入输出流对象
Reader reader=null;
Writer writer=null;
try {
reader = new FileReader("E:\\requirements.txt");
writer = new FileWriter("E:\\requirements2.txt");
//读取数据到数组内并进行写出
char c[]=new char[20];
int len=0;
while ((len=reader.read(c))!=-1){
writer.write(c,0,len);
}
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
代码7
import org.junit.Test; import java.io.*; /* 字节缓冲流 1.字节输入缓冲流,底层采用8192字节数组,存储数据 2.字节输出缓冲流 * */ public class Demo16 { @Test public void test1() throws FileNotFoundException { //字节流 InputStream ips = new FileInputStream("E:\\a.txt"); //处理流 BufferedInputStream bis = new BufferedInputStream(ips); } @Test public void test2() throws FileNotFoundException { OutputStream ops = new FileOutputStream("E:\\b.txt"); BufferedOutputStream bops=new BufferedOutputStream(ops); } //使用字节缓冲输入输出流复制文件 @Test public void test3() throws IOException { //创建输入输出流对象 long startTime=System.currentTimeMillis(); BufferedInputStream bips=new BufferedInputStream(new FileInputStream("E:\\ZZY21011011\\tan.mp4")); BufferedOutputStream bops=new BufferedOutputStream(new FileOutputStream("E:\\ZZY21011011\\copy.mp4")); //读取数据,创建字节数组,提供读取速度 byte[] bytes= new byte[1024*20]; int len=0; while ((len=bips.read(bytes))!=-1){ bops.write(bytes,0,len); } //关闭资源 bips.close(); bops.close(); long endTime=System.currentTimeMillis(); System.out.println("耗时:"+(endTime-startTime)); } }
代码8
import org.junit.Test; import java.io.*; /* 缓冲字符流: --缓冲字符输入流 BufferedReader 底层有一个char类型的数组,存储缓冲的数据8192 对象.readLine() 一次读取一行数据,当没有数据的时候返回null --缓冲字符输出流 BufferedWriter 底层有一个char类型的数组,存储缓冲的数据 8192 * */ public class Demo17 { @Test public void test1() throws IOException { BufferedReader brd=new BufferedReader(new FileReader("E:\\w.txt")); String s =brd.readLine(); System.out.println(s); brd.close(); } @Test public void test2() throws IOException { BufferedWriter brw = new BufferedWriter(new FileWriter("E:\\w.txt")); brw.write("昔日龌龊不足夸"); brw.newLine();//隔开一行,换行 //brw.write("\n"); brw.write("今朝放荡思无涯"); brw.newLine(); brw.write("春风得意马蹄疾"); brw.newLine(); brw.write("一日看尽长安花"); brw.close(); } }
代码9
import org.junit.Test; import sun.nio.cs.ext.GBK; import java.io.*; /* 转换流:将字节流转为字符流 * */ public class Demo18 { @Test public void test1() throws IOException { FileReader frd = new FileReader("E:\\qr.txt"); int read=frd.read(); System.out.println((char)read);//输出为乱码,因为编码不一致 frd.close(); InputStreamReader reader = new InputStreamReader(new FileInputStream("E:\\qr.txt"),"GBK"); int read2=reader.read(); System.out.println((char)read2);//此时不是乱码了 reader.close(); } @Test public void test3() throws IOException { FileOutputStream fos = new FileOutputStream("E:\\qr.txt"); OutputStreamWriter writer=new OutputStreamWriter(fos, "GBK"); writer.write("你好"); writer.flush(); writer.close();//qr已经写入你好 } }
代码10
/* 数据流: 可以保留数据的原始特性 int boolean 以二进制的形式存储数据 只有字节流,没有字符流 * */ import org.junit.Test; import java.io.*; public class Demo19 { @Test public void test1() throws IOException { //新建数据流对象 DataOutputStream dops = new DataOutputStream(new FileOutputStream("E://qr.txt")); //写出数据 dops.writeInt(10); dops.writeBoolean(false); dops.writeUTF("你好"); dops.close(); } @Test public void test2() throws IOException { //新建数据流对象 DataInputStream dips=new DataInputStream(new FileInputStream("E://qr.txt")); //读取 int i = dips.readInt(); System.out.println(i);//10 boolean b = dips.readBoolean(); System.out.println(b);//false String s = dips.readUTF(); System.out.println(s);//你好 //关闭流 dips.close(); } }
代码11
import org.junit.Test; import java.io.*; /* 对象流,进行对象的存取 --对象输出流 ObjectOutputStream --对象输入流 ObjectInputStream 序列化:如果将一个对象进行存储或者网络传输,那么要将此对象进行序列化 实现序列化:implements Serializable 会将对象数据采用二进制的形式存到磁盘 反序列化: * */ public class Demo20 { @Test public void test1() throws IOException { //创建对象 Person p = new Person("李白",18,1.81); //创建对象输出流 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://wr.txt")); //写出对象 oos.writeObject(p); //关闭流 oos.close(); } @Test public void test2() throws IOException, ClassNotFoundException { //创建对象输入流 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E://wr.txt")); //读取对象 Object object=ois.readObject(); System.out.println(object); //关闭资源 ois.close(); } } class Person implements Serializable{ String name; int age; double score; public Person() { } public Person(String name, int age, double score) { this.name = name; this.age = age; this.score = score; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}'; } }
代码12
import org.junit.Test; import java.io.*; /* 对象流,进行对象的存取 --对象输出流 ObjectOutputStream --对象输入流 ObjectInputStream 序列化:如果将一个对象进行存储或者网络传输,那么要将此对象进行序列化 实现序列化:implements Serializable 会将对象数据采用二进制的形式存到磁盘 反序列化:将对象从磁盘或者网络中读取出来反序列化 注意: 1.如果有属性不想进行序列化,可以选择在属性前加上transient 2.static修饰的属性不会参与序列化 3.在进行序列化或者反序列化,序列化的版本必须一致 4.在进行反序列化时,对象类的class文件不能消失,必须存在 * */ public class Demo20 { @Test public void test1() throws IOException { //创建对象 Person p = new Person("李白",18,1.81); p.setCountry("China"); //创建对象输出流 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://wr.txt")); //写出对象 oos.writeObject(p); //关闭流 oos.close(); } @Test public void test2() throws IOException, ClassNotFoundException { //创建对象输入流 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E://wr.txt")); //读取对象 Object object=ois.readObject(); System.out.println(object);//Person{name='李白', age=0, score=1.81, country=null},如果age没有transient关键字则输出age是18 //关闭资源 ois.close(); } } class Person implements Serializable{ String name; transient int age; double score; static String country; public Person() { } public Person(String name, int age, double score) { this.name = name; this.age = age; this.score = score; } public static void setCountry(String country) { Person.country = country; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + ", country=" + country + '}'; } }
代码13
/* Externalizable 此接口也可以进行序列化和反序列化 序列化方法 public void writeExternal(ObjectOutput out) throws IOException { 反序列化方法 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 使用此接口任意属性都可以被序列化 * */ import org.junit.Test; import java.io.*; public class Demo21 { @Test public void test1() throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://qw.txt")); Student s1 = new Student("李白",18,99.9); s1.setCountry("中国"); oos.writeObject(s1); oos.close(); } @Test public void test2() throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E://qw.txt")); Object object=ois.readObject(); System.out.println(object);//Student{name='李白', age=18, score=99.9, country=中国} } } class Student implements Externalizable { String name; transient int age; static String country; double score; public void setCountry(String country) { Student.country = country; }//非静态方法 public Student() { } public Student(String name, int age, double score) { this.name = name; this.age = age; this.score = score; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + ", country=" + country + '}'; } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(name); out.writeUTF(country); out.writeInt(age); out.writeDouble(score); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { name=in.readUTF(); country=in.readUTF(); age=in.readInt(); score=in.readDouble(); } }
代码14
package com.atguigu.day17; import org.junit.Test; import java.io.File; import java.io.FileNotFoundException; import java.io.PrintStream; import java.util.Scanner; /* 打印流 System.out 默认输出到控制台 System.err 默认输出到控制台 可以通过System.setOut(打印流);改变流向 可以通过System.setErr(打印流);改变流向 * */ public class Demo1 { @Test public void test1() throws FileNotFoundException { //创建打印流 PrintStream ps = new PrintStream("E://a.txt"); ps.println("昔日龌龊不足夸"); ps.println("如今放荡思无涯"); System.setOut(ps); System.out.println("春风得意马蹄疾"); System.out.println("一日看尽长安花"); System.err.println("结束"); System.setErr(ps); System.err.println("句号"); ps.close(); } @Test public void test2() throws FileNotFoundException { Scanner sc = new Scanner(new File("E://a.txt")); while (sc.hasNextLine()){ String str = sc.nextLine(); System.out.println(str); } sc.close(); } }