2021-2-20
1.IO流简述和分类
2.字节流输出入门(字节流写数据)
package com.wc.fileout;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author wc
* @Date: 2021/02/20/17:51
*/
public class FileOutPut {
public static void main(String[] args) throws IOException {
//创建字节输出流的对象---告诉虚拟机往哪个文件写数据
//FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
FileOutputStream fos=new FileOutputStream("D:\\test\\test.txt");
//写入数据
fos.write(97);
//释放资源
fos.close();
}
}
package com.wc.fileout;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author wc
* @Date: 2021/02/20/17:51
*/
public class FileOutPut {
public static void main(String[] args) throws IOException {
//创建字节输出流的对象---告诉虚拟机往哪个文件写数据
//FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
FileOutputStream fos=new FileOutputStream("Stream\\a.txt");
byte [] bys={97,98,99,100,101,102};
//写入数据
fos.write(bys,2,3);
//fos.write(bys);
//释放资源
fos.close();
}
}
3.字节流换行和追加写入
package com.wc.fileout;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author wc
* @Date: 2021/02/20/17:51
*/
public class FileOutPut {
public static void main(String[] args) throws IOException {
//创建字节输出流的对象---告诉虚拟机往哪个文件写数据
//FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
//append,续写开关,true打开,false关闭
FileOutputStream fos=new FileOutputStream("Stream\\a.txt",true);
//写入数据
fos.write(97);
//字符串转字节流.getBytes(),String的用法
fos.write("\r\n".getBytes());
fos.write(98);
//字符串转字节流.getBytes(),String的用法
fos.write("\r\n".getBytes());
fos.write(99);
//字符串转字节流.getBytes(),String的用法
fos.write("\r\n".getBytes());
//释放资源
fos.close();
}
}
4.字节流try catch捕获异常以及小结
package com.wc.fileout;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author wc
* @Date: 2021/02/20/17:51
*/
public class FileOutPut1 {
public static void main(String[] args) {
//创建字节输出流的对象---告诉虚拟机往哪个文件写数据
//FileOutputStream fos=new FileOutputStream(new File("D:\\test\\test.txt"));
//append,续写开关,true打开,false关闭
FileOutputStream fos=null;
try {
fos=new FileOutputStream("Stream\\a.txt");
fos.write(97);
} catch (IOException e) {
e.printStackTrace();
} finally {
//finally里面的代码一定会执行
//加if判断是防止出现空指针异常
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
5.字节输入流(FileInputStream ,FileOutputStream 读取字节流多个字节)
package com.wc.fileout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* @author wc
* @Date: 2021/02/22/11:45
*/
public class FileIntPut {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("Stream\\a.txt");
int read = fis.read();
System.out.println(read);
fis.close();
}
}
6.字节流文件复制案例
package com.wc.fileout;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @author wc
* @Date: 2021/02/22/14:15
*/
public class FileCopy {
public static void main(String[] args) throws IOException {
//创建字节输入-----读
FileInputStream fis=new FileInputStream("D:\\test\\test.txt");
//创建字节输出-----写
FileOutputStream fos=new FileOutputStream("Stream\\test.txt");
//有多少字节就读多少字节通过while来实现
int b;
while((b=fis.read())!=-1){
fos.write(b);
}
fis.close();
fos.close();
}
}
7.字节缓冲流加小结(BufferedInputStream,BufferedOutputStream)
package com.wc.fileout;
import java.io.*;
/**
* @author wc
* @Date: 2021/02/22/14:42
*/
public class FileBuffered {
public static void main(String[] args) throws IOException {
//创建一个字节缓冲输出流,(括号里放字节流)
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("Stream\\a.txt"));
//创建一个字节缓冲输入流,(括号里放字节流)
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("Stream\\copy.txt"));
byte [] bytes=new byte[1024];
int length;
while ((length=bis.read(bytes))!=-1){
bos.write(bytes,0,length);
}
bis.close();
bos.close();
}
}
8.字符流(编码表简介)
9.字符流编码和解码(getBytes)
package com.wc.charstream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo {
public static void main(String[] args) throws UnsupportedEncodingException {
//使用平台默认字符集将String编码为一系列字节
String s="我爱学java";
byte[] bytes = s.getBytes();
//System.out.println(bytes);//[B@1b6d3586 打印的为内存地址
System.out.println(Arrays.toString(bytes));
//使用指定字符集将String编码为一系列字节
byte[] bytes1 = s.getBytes("UTF-8");
byte[] bytes2 = s.getBytes("GBK");
System.out.println(Arrays.toString(bytes1));
System.out.println(Arrays.toString(bytes2));
System.out.println("========================================");
//使用平台默认字符集解码指定字节数组来构造新的string
byte [] bytes3={-101,-120,-100,-11,-12,-14};
byte [] bytes4={-26, -120, -111, -25, -120, -79, -27, -83, -90, 106, 97, 118, 97};
String s1=new String(bytes4);
//使用指定字符集解码指定字节数组来构造新的string
String s2=new String(bytes3,"GBK");
System.out.println(s1);
System.out.println(s2);
}
}
10.字符流写数据(write和flush和close)
package com.wc.charstream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo1 {
public static void main(String[] args) throws IOException {
//创建字符输出流对象
FileWriter fw = new FileWriter("Stream\\a.txt");
//写一个字符
fw.write(97);
fw.write(98);
fw.write(99);
char [] chars={97,98,99,100};
fw.write(chars);
char [] chars1={97,98,99,100};
fw.write(chars1,0,2);
String s="我爱学java";
fw.write(s);
fw.close();
}
}
11.字符流读取数据(filereader以及案例)
package com.wc.charstream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo2 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入用户名:");
String username = sc.next();
System.out.println("请输入密码:");
String passWord = sc.next();
FileWriter fw=new FileWriter("Stream\\a.txt");
FileReader fr=new FileReader("Stream\\a.txt");
fw.write(username);
fw.write("\r\n");
fw.write(passWord);
fw.write("\r\n");
fw.flush();
fw.close();
}
}
12.字符缓冲流(bufferedewriter,bufferedreader)
13.缓冲流特有方法(newline,readline)
package com.wc.charstream;
import java.io.*;
import java.util.Scanner;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo3 {
public static void main(String[] args) throws IOException {
BufferedWriter bfw = new BufferedWriter(new FileWriter("Stream\\a.txt"));
bfw.write("橙子666");
//换行newLine
bfw.newLine();
bfw.write("橙子66");
bfw.newLine();
bfw.write("橙子6");
bfw.newLine();
bfw.flush();
bfw.close();
}
}
package com.wc.charstream;
import java.io.*;
import java.util.Scanner;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo3 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("Stream\\a.txt"));
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
14.缓冲流案例
package com.wc.charstream;
import java.io.*;
import java.util.Arrays;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo4 {
public static void main(String[] args) throws IOException {
//先读取a.txt文件里面的数据,直接用缓冲流读取
BufferedReader br = new BufferedReader(new FileReader("Stream\\a.txt"));
//只有一行数据直接读取
String line = br.readLine();
System.out.println(line);
br.close();
//开始排序,排序要对int类型的数据进行操作,将数组转换成int类型
//将字符按照空格进行切割
String[] split = line.split(" ");
int[] arr = new int[split.length];
//遍历数组,进行转换
for (int i = 0; i < arr.length; i++) {
String smallStr = split[i];
int number = Integer.parseInt(smallStr);
arr[i] = number;
}
//排序
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
//把排序之后的写入
BufferedWriter bw = new BufferedWriter(new FileWriter("Stream\\a.txt"));
for (int i = 0; i < arr.length; i++) {
bw.write(arr[i] + " ");
bw.flush();
}
bw.close();
}
}
15.转换流(指定编码读写InputStreamReader,OutputStreamWriter)
16.对象操作流(ObjectOutputStream,ObjectInputStream)
对象序列化,保证文件安全性
package com.wc.charstream;
import java.io.*;
import java.util.Arrays;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo5 {
public static void main(String[] args) throws IOException {
Student student=new Student("橙子","123456");
//采用对象操作流,增强代码安全性,output往外写
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Stream\\a.txt"));
oos.writeObject(student);
oos.close();
}
}
对象操作反序列化
17.对象操作流程注意点
private static final long serialVersionUID=1L;
package com.wc.charstream;
import java.io.*;
import java.util.Arrays;
/**
* @author wc
* @Date: 2021/02/22/15:26
*/
public class CharStreamDemo5 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//method1();
ObjectInputStream ois =new ObjectInputStream(new FileInputStream("Stream\\a.txt"));
Student student=(Student) ois.readObject();
System.out.println(student);
ois.close();
}
private static void method1() throws IOException {
Student student=new Student("橙子","123456");
//采用对象操作流,增强代码安全性,output往外写
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Stream\\a.txt"));
oos.writeObject(student);
oos.close();
}
}