七、字符流写数据的5种方式[Java] 纯文本查看 复制代码
package readerwriter;
//使用字符输出流,把内容写入到文件当中
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class OutputStreamWriterTest {
public static void main(String[] args) throws IOException {
/*FileOutputStream fos = new FileOutputStream("day09\\osw.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);*/
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day09\\osw.txt"));
OutputStreamWriter osw1 = new OutputStreamWriter(new FileOutputStream("day09\\osw1.txt"),"UTF-8");
OutputStreamWriter osw2 = new OutputStreamWriter(new FileOutputStream("day09\\osw2.txt"),"GBK");
//字符输出流一次写入一个字符
osw.write(97);
osw.write("\r\n");
//字符输出流一次写入一个字符数组
char[] chs = {'a','b','c','d','e'};
osw.write(chs);
osw.write("\r\n");
//字符输出流一次写入一个字符数组,从哪个索引开始,写入多少长度
osw.write(chs,0,chs.length);
osw.write("\r\n");
//字符输出流一次写入一个字符串
osw.write("hello");
osw.write("\r\n");
//字符输出流一次写入一个字符串,从哪个索引开始,写入多少长度
osw.write("world",0,"world".length());
osw.write("\r\n");
//以UTF-8的编码格式写入数据
osw1.write(97);
osw1.write("\r\n");
char[] chs1 = {'a','b','c','d','e'};
osw1.write(chs1);
osw1.write("\r\n");
osw1.write(chs1,0,chs1.length);
osw1.write("\r\n");
osw1.write("hello");
osw1.write("\r\n");
osw1.write("world",0,"world".length());
osw1.write("\r\n");
//以GBK的编码格式写入数据
osw2.write(97);
osw2.write("\r\n");
char[] chs2 = {'a','b','c','d','e'};
osw2.write(chs2);
osw2.write("\r\n");
osw2.write(chs2,0,chs2.length);
osw2.write("\r\n");
osw2.write("hello");
osw2.write("\r\n");
osw2.write("world",0,"world".length());
osw2.write("\r\n");
osw.close();
osw1.close();
osw2.close();
}
}
八、字符流读数据的2种方式[Java] 纯文本查看 复制代码
package readerwriter;
//使用字符输入流,把文件中的内容读取出来
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputStreamReaderTest {
public static void main(String[] args) throws IOException {
/*FileInputStream fis = new FileInputStream("day09\\osw.txt");
InputStreamReader isr = new InputStreamReader(fis);*/
InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\osw.txt"));
InputStreamReader isr1 = new InputStreamReader(new FileInputStream("day09\\osw1.txt"),"UTF-8");
InputStreamReader isr2 = new InputStreamReader(new FileInputStream("day09\\osw2.txt"),"GBK");
//以默认编码格式读取文件
//字符输入流一次读取一个字符
/*int by = isr.read();
System.out.println((char)by);*/
//字符输入流一次读取一个字符数组
/*char[] chs = new char[1024];
int len = isr.read(chs);
System.out.println(len);
for (char ch : chs){
System.out.print(ch);
}*/
//字符输入流一次读取一个字符数组,从哪个索引开始,读取多少长度
/*char[] chs = new char[1024];
int len1 = isr.read(chs, 0, chs.length);
System.out.println(len1);
for (char ch : chs){
System.out.print(ch);
}*/
//以UTF-8字符集格式读取文件
//字符输入流一次读取一个字符
/*int by = isr1.read();
System.out.println((char)by);*/
//字符输入流一次读取一个字符数组
/*char[] chs = new char[1024];
int len = isr1.read(chs);
System.out.println(len);
for (char ch : chs){
System.out.print(ch);
}*/
//字符输入流一次读取一个字符数组,从哪个索引开始,读取多少长度
/*char[] chs = new char[1024];
int len1 = isr1.read(chs, 0, chs.length);
System.out.println(len1);
for (char ch : chs){
System.out.print(ch);
}*/
//以GBK字符集格式读取文件
//字符输入流一次读取一个字符
/*int by = isr2.read();
System.out.println((char)by);*/
//字符输入流一次读取一个字符数组
/*char[] chs = new char[1024];
int len = isr2.read(chs);//
System.out.println(len);
for (char ch : chs){
System.out.print(ch);
}*/
//字符输入流一次读取一个字符数组,从哪个索引开始,读取多少长度
char[] chs = new char[1024];
int len1 = isr2.read(chs, 0, chs.length);
System.out.println(len1);
for (char ch : chs){
System.out.print(ch);
}
isr.close();
isr1.close();
}
}
九、字符流复制Java文件[Java] 纯文本查看 复制代码
package readerwriter;
//使用字符流,复制视频,一次读写一个字符,一次读写一个字符数组
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\src\\arraybuffered\\ArrayToTXT.java"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day09\\ReaderCopy.java"));
//字符流一次读写一个字符
/*int by;
while((by=isr.read()) != -1){
osw.write(by);
}*/
//字符流一次读写一个字符数组
char[] chs = new char[1024];
int len;
while((len=isr.read(chs)) != -1){
osw.write(chs,0,len);
}
//释放资源
isr.close();
osw.close();
}
}
十、字符流辅助Java文件改进版[Java] 纯文本查看 复制代码
package readerwriter;
//使用字符流简便类,复制文件,一次读写一个字符,一次读写一个字符数组
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy1 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("day09\\src\\readerwriter\\Copy.java");
FileWriter fw = new FileWriter("day09\\Reader1Copy.java");
//字符流简便类,一次读写一个字符
/*int by;
while((by=fr.read()) != -1){
fw.write(by);
}*/
//字符流简便,一次读写一个字符数组
char[] chs = new char[1024];
int len;
while((len = fr.read(chs)) != -1){
fw.write(chs,0,len);
}
//释放资源
fr.close();
fw.close();
}
}
十一、字符缓冲流字符缓冲流:BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可以指定缓冲区大小,或者可以接受默认大小。默认值足够大,可用于大多数用途
BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓冲区大小,或者可以使用默认大小。 默认值足够大,可用于大多数用途
构造方法:BufferedWriter(Writer out)
BufferedReader(Reader in)[Java] 纯文本查看 复制代码
package bufferedreaderwriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
/*
创建字符缓冲输入流对象
把某个文件中的数据读入程序
创建字符缓冲输出流对象
把数据输出到指定文件中
*/
public class BufferedReaderTest1 {
public static void main(String[] args) throws IOException {
//创建字符缓冲输入流对象
BufferedReader br = new BufferedReader(new FileReader("day09\\copy.txt"));
//一次读取一个字符
/*int by;
while ((by=br.read())!=-1){
System.out.println((char) by);
}*/
//一次读取一个字符数组
char[] chs = new char[1024];
int len;
/*while ((len = br.read(chs))!=-1){
System.out.println(new String(chs,0,len));
}*/
//一次读取一个字节数组,从哪个索引开始,读取多少长度
len = br.read(chs,0,chs.length);
System.out.println(new String(chs,0,len));
br.close();
}
}
[Java] 纯文本查看 复制代码
package bufferedreaderwriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterTest1 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("day9\\bwwww.txt"));
//一次写一个字符
bw.write(97);
char[] chs = {'a','b','c'};
//一次写一个字符数组
bw.write(chs);
//一次写一个字符数组,从哪个索引开始,写入多少长度
bw.write(chs,0,chs.length);
//一次写一个字符串
bw.write("abc");
//一次写一个字符串,从哪个索引开始,写入多少长度
bw.write("abc",0,"abc".length());
bw.close();
}
}
十二、字符缓冲流复制Java文件[Java] 纯文本查看 复制代码
package bufferedreaderwriter;
import java.io.*;
public class Copy1 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day09\\copy.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\copy11.txt"));
//一次读写一个字符
int by;
while ((by=br.read()) != -1){
bw.write(by);
}
//一次读写一个字符数组
char[] chs = new char[1024];
int len;
while ((len = br.read(chs))!= -1){
bw.write(chs,0,len);
}
br.close();
bw.close();
}
}
十三、字符缓冲流特有功能缓冲流特有方法:
BufferedWriter:
void newLine():写一行行分隔符,行分隔符字符串由系统属性定义
BufferedReader:
public String readLine() :读一行文字。 结果包含行的内容的字符串,不包括任何行终止字符,如果流的结尾已经到达,则为null [Java] 纯文本查看 复制代码
package bufferedreaderwriter;
//使用字符缓冲输入流,调用其功能,读取文件当中的内容
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderTest {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day09\\bw.txt"));
//字符缓冲输入流一次读取一个字符
/*int by = br.read();
System.out.println((char)by);*/
//字符缓冲输入流一次读取一个字符数组
/*char[] chs = new char[1024];
int len = br.read(chs);
System.out.println(len);
for (char ch : chs){
System.out.print(ch);
}*/
//字符缓冲输入流一次读取一个字符数组,从哪个索引开始,读取多少长度
/*char[] chs = new char[1024];
int len = br.read(chs, 0, chs.length);
System.out.println(len);
for (char ch : chs){
System.out.print(ch);
}*/
//字符缓冲输入流一次读取一行,返回字符串,如果返回值是null,就表示已经到文本结尾
String line;
while((line=br.readLine()) != null){
System.out.println(line);
}
br.close();
}
}
[Java] 纯文本查看 复制代码
package bufferedreaderwriter;
//使用字符缓冲输出流,调用其方法,把内容写入到文件当中
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterTest {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\bw.txt"));
//字符缓冲输出流一次写入一个字符
bw.write(97);
bw.newLine();
//字符缓冲输出流一次写入一个字符数组
char[] chs = {'a','b','c','d','e'};
bw.write(chs);
bw.newLine();
//字符缓冲输出流一次写入一个字符数组,从哪个索引开始,写入多少长度
bw.write(chs,0,chs.length);
bw.newLine();
//字符缓冲输出流一次写入一个字符串
bw.write("hello");
bw.newLine();
//字符缓冲输出流一次写入一个字符串,从哪个索引开始,写入多少长度
bw.write("world",0,"world".length());
//释放资源
bw.close();
}
}
十四、字符缓冲流特有功能复制Java文件[Java] 纯文本查看 复制代码
package bufferedreaderwriter;
//使用字符缓冲流,复制Java文件
import java.io.*;
public class Copy {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day09\\src\\bufferedreaderwriter\\BufferedReaderTest.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\BrCopy.java"));
//字符缓冲流读写一次读写一个字符
/*int by;
while((by=br.read()) != -1){
bw.write(by);
}*/
//字符缓冲流读写一次读写一个字符数组
/*char[] chs = new char[1024];
int len;
while((len = br.read(chs)) != -1){
bw.write(chs,0,len);
}*/
//字符缓冲流读写一次读写一行,返回String
String line;
while((line = br.readLine()) != null){
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
br.close();
bw.close();
}
}
十五、IO流小结字节流 ----------InputStream
---------- FileInputStream
---------- BufferedInputStream
----------OutputStream
---------- FileOutputStream
---------- BufferedOutputStream
字符流---------- Reader
---------- InputStreamReader
---------- FileReader(是 InputStreamReader的简易类)
---------- BufferedReader
---------- Writer
---------- OutputStreamWriter
---------- FileWriter(是 OutputStreamWriter的简易类)
---------- BufferedWriter
十六、集合到文件思路:
创建ArrayList集合
往集合中存储字符串元素
创建字符缓冲输出流对象
遍历集合,得到每一个字符串数据
调用字符缓冲输出流对象的方法写数据
释放资源[Java] 纯文本查看 复制代码
package arraybuffered;
//使用字符缓冲输出流,把集合中的内容写入到文本当中
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class ArrayToTXT {
public static void main(String[] args) throws IOException {
ArrayList<String> array = new ArrayList<>();
array.add("hello");
array.add("world");
array.add("java");
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\array1.txt"));
for (String s : array){
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
}
}
十七、文件到集合步骤
创建字符缓冲输入流对象
创建ArrayList集合对象
调用字符缓冲输入流对象的方法读数据
把读取到的字符串数据存储到集合中
释放资源
遍历集合[Java] 纯文本查看 复制代码
package arraybuffered;
//使用字符缓冲输入流,把文本当中的内容读取,存储到集合当中
import java.io.*;
import java.util.ArrayList;
public class TXTToArray {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day09\\array1.txt"));
ArrayList<String> array = new ArrayList<>();
String line;
while((line=br.readLine()) != null){
array.add(line);
}
br.close();
for (String s : array){
System.out.println(s);
}
}
}
十八、点名器点名器案例步骤
创建字符缓冲输入流对象
创建ArrayList集合对象
调用字符缓冲输入流对象的方法读数据
把读取到的字符串数据存储到集合中
释放资源
使用Random产生一个随机数,随机数的范围在:[0,集合的长度)
把第6步产生的随机数作为索引到ArrayList集合中
获取值把第7步得到的数据
输出在控制台[Java] 纯文本查看 复制代码
package bufferedreaderarray;
//点名器,使用缓冲输入流,从文本当中读取姓名,存储到集合中,并随机点名(生成随机索引,根据索引获取集合对应的姓名)
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class GetName {
public static void main(String[] args) throws IOException {
//集合数据写入到文件
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\names.txt"));
ArrayList<String> array = new ArrayList<>();
array.add("张三");
array.add("李四");
array.add("王五");
array.add("赵六");
array.add("田七");
for (String name : array){
bw.write(name);
bw.newLine();
bw.flush();
}
bw.close();
//文件数据读取到集合
BufferedReader br = new BufferedReader(new FileReader("day09\\names.txt"));
ArrayList<String> array1 = new ArrayList<>();
String name;
while((name=br.readLine())!=null){
array1.add(name);
}
Random r = new Random();
int index = r.nextInt(array1.size());
String names = array1.get(index);
System.out.println(names);
}
}
十九、集合到文件改进版步骤
定义学生类
创建ArrayList集合
创建学生对象
把学生对象添加到集合中
创建字符缓冲输出流对象
遍历集合,得到每一个学生对象
把学生对象的数据拼接成指定格式的字符串
调用字符缓冲输出流对象的方法写数据
释放资源[Java] 纯文本查看 复制代码
package bufferedreaderarray;
public class Student {
private String sid;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String sid, String name, int age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"sid='" + sid + '\'' +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
[Java] 纯文本查看 复制代码
package bufferedreaderarray;
//使用字符缓冲输出流,把集合中的学生信息,写入到文本当中,按照指定的格式
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class ArrayToTXT {
public static void main(String[] args) throws IOException {
ArrayList<Student> array = new ArrayList<>();
Student s1 = new Student("001","张三",21,"西安");
Student s2 = new Student("002","李四",21,"北京");
Student s3 = new Student("003","王五",21,"成都");
array.add(s1);
array.add(s2);
array.add(s3);
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\student.txt"));
for (Student s : array){
StringBuilder sb = new StringBuilder();
sb.append(s.getSid()).append(",").append(s.getName()).append(",").append(s.getAge()).append(",").append(s.getAddress());
String stu = sb.toString();
bw.write(stu);
bw.newLine();
bw.flush();
}
bw.close();
}
}
二十、文件到集合改进版步骤
定义学生类
创建TreeSet集合,通过比较器排序进行排序
键盘录入学生数据
创建学生对象,把键盘录入的数据对应赋值给学生对象的成员变量
把学生对象添加到TreeSet集合
创建字符缓冲输出流对象
遍历集合,得到每一个学生对象
把学生对象的数据拼接成指定格式的字符串
调用字符缓冲输出流对象的方法写数据
释放资源[Java] 纯文本查看 复制代码
package bufferedreaderarray;
public class Student {
private String sid;
private String name;
private int age;
private String address;
public Student() {
}
public Student(String sid, String name, int age, String address) {
this.sid = sid;
this.name = name;
this.age = age;
this.address = address;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"sid='" + sid + '\'' +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
[Java] 纯文本查看 复制代码
package bufferedreaderarray;
//使用缓冲输入流,把文本当中的学生信息读取出来,存储到集合中
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class TXTToArray {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day09\\student.txt"));
ArrayList<Student> array = new ArrayList<>();
String line;
while((line = br.readLine()) != null){
String[] strArr = line.split(",");
Student s = new Student();
s.setSid(strArr[0]);
s.setName(strArr[1]);
s.setAge(Integer.parseInt(strArr[2]));
s.setAddress(strArr[3]);
array.add(s);
}
for (Student s : array){
System.out.println(s);
}
}
}