一、高效字节输出流写出字节数据
利用高效字节输出流往C盘下的d.txt文件输出一个字节数。
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("day10_缓冲流、转换流\\a.txt"));
bos.write(97);
bos.close();
}
二、高效字节输出流写出字节数组数据
利用高效字节输出流往C盘下的e.txt文件写出一个字节数组数据,如写出:”i love java”。
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("day10_缓冲流、转换流\\c.txt"));
String s = "i love you";
bos.write(s.getBytes());
bos.close();
}
三、高效字符流和集合的综合使用
分析以下需求,并用代码实现
实现一个验证码小程序,要求如下:
1. 在项目根目录下新建一个文件:data.txt,键盘录入3个字符串验证码,
并存入data.txt中,要求一个验证码占一行;
2. 键盘录入一个需要被校验的验证码,如果输入的验证码在data.txt中存在:
在控制台提示验证成功,如果不存在控制台提示验证失败。
public static void main(String[] args) throws IOException {
File file = new File("day10_缓冲流、转换流\\data.txt");
//createVerify(file);
boolean login = login();
if (login) {
System.out.println("登陆成功");
}else{
System.out.println("验证失败");
}
}
public static void createVerify(File file) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入要保存字符串验证码");
String verify = sc.next();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bos.write(verify.getBytes());
bos.close();
}
public static boolean login() throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入验证码:");
String verify = sc.next();
BufferedReader br = new BufferedReader(
new FileReader("day10_缓冲流、转换流\\data.txt"));
String s = br.readLine();
br.close();
return s.equals(verify);
}
四、转换输出流的使用
描述:现有一字符串:”我爱Java”。将该字符串保存到当前项目根目录下的text05.txt文件中。
要求:使用gbk编码保存。
public static void main(String[] args) throws Exception {
String s = "我爱Java";
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
"day10_缓冲流、转换流\\test05.txt"), "GBK");
osw.write(new String(s.getBytes(), "GBK").toCharArray());
osw.close();
}
五、转换输入流的使用
描述:利用转换输入流将当前项目根目录下使用gbk编码的text05.txt文件的内容读取出来,并打印在控制台上。
要求:不能出现乱码的情况。
public static void main(String[] args) throws Exception {
InputStreamReader isr = new InputStreamReader(
new FileInputStream("day10_缓冲流、转换流\\test06.txt"), "GBK");
char[] chs = new char[1024];
int len = 0;
while ((len = isr.read(chs)) != -1) {
System.out.println(chs);
}
isr.close();
}
六、描述:从键盘录入一行字符串,利用字节打印流将该行字符串保存到当前项目根目录下的d.txt文件中。
public static void main(String[] args) throws FileNotFoundException {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一行字符串");
PrintStream ps = new PrintStream("day10_缓冲流、转换流\\d.txt");
ps.println(sc.next());
}
七、高效字符流读写数据
利用IO流的知识读取text.txt文件的内容反转后写入text1.txt文件中,内容如下:
123456
我爱java
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new FileReader("day10_缓冲流、转换流\\text.txt"));
ArrayList<String> list = new ArrayList<>();
char[] bys = new char[1024];
String line = null;
while ((line = br.readLine()) != null) {
list.add(line);
}
Collections.reverse(list);
BufferedWriter bw = new BufferedWriter(new FileWriter("day10_缓冲流、转换流\\text1.txt"));
for (String s : list) {
bw.write(s);
bw.newLine();
}
bw.close();
br.close();
}
八、对象的序列化,对象输出流的使用
描述:
定义一个学生类,成员变量有姓名,年龄,性别,提供setters和getters方法以及构造方法
定义一个测试类,在测试类创建多个学生对象保存到集合中,然后将集合存储到当前项目根目录下的stus.txt文件中。
public class Student implements Serializable {
private static final long serialVersionUID = 6226316605901205593L;
private String name;
private int age;
private String sex;
public Student() {
}
public Student(String name, int age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public static void main(String[] args) throws Exception {
//创建集合用来保存Student对象
List<Student> list = new ArrayList<>();
list.add(new Student("张三", 20, "男"));
list.add(new Student("东方不败", 18, "女"));
list.add(new Student("小龙女", 16, "女"));
//创建序列化流
ObjectOutputStream obs = new ObjectOutputStream(
new FileOutputStream("stus.txt"));
//写入list对象
obs.writeObject(list);
//关闭序列化流
obs.close();
//创建反序列化流读取list对象
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("stus.txt"));
//遍历Student对象
List<Student> list1 = (List) ois.readObject();
for (Student s : list) {
System.out.println(s.getName() + "---" + s.getAge() + "---" + s.getSex());
}
//关闭反序列化流
ois.close();
}