IO流
字符流出现的原因及编码表概述和常见编码表
字符流出现的原因:字节流操作中文不是特别方便,java就提供了字符流
字符流:只能对文本文件进行读写
字符流:字符流=字节流+编码表
String类中的编码和解码问题
编码:就是把字符串转换成字节数组
- 把一个字符串转换成一个字节数组
- public byte[ ] getBytes():使用平台的默认字符集将此String编码为byte 序列,并将结果存储到一个新的 byte 数组中
public class MyTest {
public static void main(String[] args) {
//getBytes()采用平台默认的码表进行编码
byte[] bytes = "我要被存储到byte数组中啦".getBytes();
for (byte aByte : bytes) {
System.out.println(aByte);
}
}
}
- public byte[] getBytes(()String charsetName):使用指定的字符集将此String编码为byte序列,并将结果存储到一个新的byte数组中
import java.io.*;
public class MyTest {
public static void main(String[] args) throws UnsupportedEncodingException {
//getBytes("GBK")采用GBK码表进行编码
byte[] bytes = "我要被存储到byte数组中啦".getBytes("GBK");
for (byte aByte : bytes) {
System.out.println(aByte);
}
}
}
解码:把字节数组转换成字符串
- public String(byte[ ] bytes):通过使用平台的默认字符集解码指定的byte数组,构造一个新的String
public class MyTest {
public static void main(String[] args){
byte[] bytes = "我要被存储到byte数组中啦".getBytes();
for (byte aByte : bytes) {
System.out.println(aByte);
}
//采用平台默认的码表进行解码
String s = new String(bytes);
System.out.println(s);
}
}
- public String(byte[ ] bytes,String charsetName):通过使用指定的charset解码指定的byte数组,构造一个新的String
import java.io.*;
public class MyTest {
public static void main(String[] args) throws UnsupportedEncodingException {
byte[] bytes = "我要被存储到byte数组中啦".getBytes("GBK");
for (byte aByte : bytes) {
System.out.println(aByte);
}
//采用GBK码表进行解码
String s = new String(bytes,"GBK");
System.out.println(s);
}
}
使用什么字符集进行编码,那么就是使用什么字符集进行解码
- byte[ ] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
- String(byte[ ] bytes,String charsetName):通过指定的字符集解码字节数组
编码:把看得懂的变成看不懂的:String – byte[ ]
解码:把看不懂的变成看得懂的:byte[ ] – String
转换流OutputStreamWriter的使用
OutputStreamWriter的构造方法
-
OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
-
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换
字符流的5种写数据的方式
- public void write(int c):写一个字符
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"));
out.write('a');
out.flush();
out.close();
}
}
- public void write(char[ ] cbuf):写一个字符数组
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"));
char[] chars = new char[]{'a','b','c','d'};
out.write(chars);
out.flush();
out.close();
}
}
- public void write(char[ ] cbuf,int off,int len):写一个字符数组的 一部分
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"));
char[] chars = new char[]{'a','b','c','d'};
out.write(chars,0,1);
out.flush();
out.close();
}
}
- public void write(String str):写一个字符串
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"));
out.write("abcde");
out.flush();
out.close();
}
}
- public void write(String str,int off,int len):写一个字符串的一部分
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"));
out.write("abcde",1,2);
out.flush();
out.close();
}
}
转换流InputStreamReader的使用
InputStreamReader的构造方法
-
InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
-
InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
字符流的2种读数据的方式
- public int read():一次读取一个字符,如果没有读到返回-1
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
int read = in.read();
System.out.println(read);
in.close();
}
}
- public int read(char[ ] cbuf):一次读取一个字符数组,如果没有读到返回-1
package org.westos.demo3;
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt"));
char[] chars = new char[2];
int len=in.read(chars);
for (char aChar : chars) {
System.out.println(aChar);
}
in.close();
}
}
字符流复制文本文件
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("原文件.txt"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("复制件.txt"));
int ch = 0;
while ((ch=in.read())!=-1){
out.write(ch);
out.flush();
}
in.close();
out.close();
}
}
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(new FileInputStream("原文件.txt"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("复制件.txt"));
char[] chars = new char[1000];
int len=0;
while ((len=in.read(chars))!=-1){
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
}
}
FileWriter和FileReader复制文本文件
FileReader和FileWriter的出现
转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,为了简化书写,转换流提供了对应的子类
- FileWriter
- FileReader
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MyTest {
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader("原文件.txt");
FileWriter writer = new FileWriter("复制件.txt");
int len=0;
char[] chars = new char[10];
while ((len=reader.read(chars))!=-1){
writer.write(String.valueOf(chars),0,len);
writer.flush();
}
reader.close;
writer.close;
}
}
字符流便捷类:因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集,于是java就给我们提供转换流对应的便捷类
转换流-------------------------------------------便捷类
OutputStreamWriter-------------------------FileWriter
InputStreamReader-------------------------FileReader
字符缓冲流的基本使用
高效的字符流
-
高效的字符输出流:BufferedWriter
构造方法:public BufferedWriter(Writer w) -
高效的字符输入流:BufferedReader
构造方法:public BufferedReader(Reader e)
字符缓冲流复制文本文件
import java.io.*;
public class MyTest {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("原文件.txt"));
BufferedWriter writer = new BufferedWriter(new FileWriter("复制件.txt"));
int len=0;
char[] chars = new char[10];
while ((len=reader.read(chars))!=-1){
writer.write(chars,0,len);
writer.flush();
}
reader.close();
writer.close();
}
}
字符缓冲流的特殊功能
字符缓冲流的特殊功能
- BufferedWriter:public void newLine():根据系统来决定换行符,具有系统兼容性的换行符
- BufferedReader:public String readLine():一次读取一行数据,是以换行符为标记的,读到换行符就换行,没读到数据返回null
- 包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
把集合中的数据存储到文本文件
需求:把ArrayList集合中的字符串数据存储到文本文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class MyTest {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
list.add("我要把这句话存到文本文件里");
list.add("换个行");
BufferedWriter writer = new BufferedWriter(new FileWriter("文本文件.txt"));
for (String s : list) {
writer.write(s);
writer.newLine();
writer.flush();
}
writer.close();
}
}
把文本文件中的数据存储到集合中
需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class MyTest {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("文本文件.txt"));
ArrayList<String> list = new ArrayList<>();
String s = reader.readLine();
String s1 = reader.readLine();
list.add(s);
list.add(s1);
for (String a : list) {
System.out.println(a);
}
reader.close();
}
}
随机获取文本文件中的姓名
需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
public class MyTest {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("学生.txt"));
ArrayList<String> list = new ArrayList<>();
while (true){
String name = reader.readLine();
if (name!=null){
list.add(name);
}else {
break;
}
}
Random random = new Random();
int i = random.nextInt(list.size());
String s = list.get(i);
System.out.println(s);
}
}
复制单级文件夹
public class Mytest {
public static void main(String[] args) throws IOException {
File srcFile = new File("C:\\Users\\Administrator\\Desktop\\作业");
File desFile = new File("D:\\作业");
if(!desFile.exists()){
desFile.mkdirs();
}
copyFile( srcFile,desFile);
System.out.println("复制完成");
}
private static void copyFile(File srcFile, File desFile) throws IOException {
//list()和listFiles()的区别:
/*1.返回值类型不同:前者为String数组,后者为File对象数组
2.数组中的元素内容不同:前者为String类型的[文件名](包含后缀名),
后者为File对象类型的[完整路径]。
所以,遍历文件夹中所有的文件,包括子文件夹的文件时,必须用listFiles()方法。*/
File[] files = srcFile.listFiles();
for (File f : files) {
if(f.isFile() ){
copyFiles(f,desFile);
}else {
}
}
}
private static void copyFiles(File f, File desFile) throws IOException {
FileInputStream fileIn= new FileInputStream(f);
FileOutputStream fileOut = new FileOutputStream(new File(desFile,f.getName()));
byte[] bytes = new byte[1024 * 4];
int len=0;
while ((len=fileIn.read(bytes))!=-1){
fileOut.write(bytes,0,len);
fileOut.flush();
}
fileIn.close();
fileOut.close();
}
}
复制指定目录下指定后缀名的文件并修改名称
public class Mytest {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\Administrator\\Desktop\\作业");
File file2 = new File("C:\\Users\\Administrator\\Desktop\\作业1");
if(!file2.exists()){
file2.mkdirs();
}
复制(file, file2);
}
private static void 复制(File file, File file2) throws IOException {
File[] files = file.listFiles();
for (File file1 : files) {
if (file1.isFile()) {
if (file1.getAbsolutePath().endsWith(".jpg")) {
String s = file1.getAbsolutePath().substring(0, file1.getAbsolutePath().lastIndexOf("."));
File newfile = new File(s + ".png");
//file1.renameTo(newfile);
FileInputStream in = new FileInputStream(file1);
FileOutputStream out = new FileOutputStream(new File(file2.getAbsolutePath()+"\\"+newfile.getName()));
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
in.close();
out.close();
} else {
FileInputStream in = new FileInputStream(file1);
FileOutputStream out = new FileOutputStream(new File(file2.getAbsolutePath()+"\\"+file1.getName()));
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
in.close();
out.close();
}
} else {
if (file1.isDirectory()) {
File file3 = new File(file2.getAbsolutePath()+"\\"+file1.getName());
file3.mkdirs();
复制(file1, file3);
}
}
}
}
}
键盘录入学生信息按照总分排序并写入文本文件
需求:键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),按照总分从高到低存入文本文件
public class MyTest {
public static void main(String[] args) throws IOException {
TreeSet<Student> treeSet = new TreeSet<>(new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getTotalScore() - s2.getTotalScore() == 0 ? s1.getName().compareTo(s2.getName()) : s1.getTotalScore() - s2.getTotalScore();
return -num;
}
});
for (int i = 1; i <= 3; i++) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入第" + i + "个学生的名字");
String name = scanner.nextLine();
Student student = new Student();
student.setName(name);
System.out.println("请输入第" + i + "个学生的语文成绩");
int yw = scanner.nextInt();
student.setChineseScore(yw);
System.out.println("请输入第" + i + "个学生的数学成绩");
int sx = scanner.nextInt();
student.setMathScore(sx);
System.out.println("请输入第" + i + "个学生的英语成绩");
int yy = scanner.nextInt();
student.setEnglishScore(yy);
//把学生添加集合中
treeSet.add(student);
}
//把学生成绩,存到文本文件中
// BufferedWriter bfw = new BufferedWriter(new FileWriter("score.txt",true));
long time = System.currentTimeMillis();
String timeStr = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒").format(new Date(time));
String fileName= timeStr+"-score.txt";
BufferedWriter bfw = new BufferedWriter(new FileWriter(fileName));
bfw.write("序号\t姓名\t语文\t数学\t英语\t总分");
bfw.newLine();
bfw.flush();
int i=1;
int j=1;
for (Student student : treeSet) {
System.out.println((j++) + "\t" + student.getName() + "\t" + student.getChineseScore() + "\t" + student.getMathScore() + "\t" + student.getEnglishScore() + "\t" + student.getTotalScore());
bfw.write((i++)+"\t"+student.getName()+"\t"+student.getChineseScore()+"\t"+student.getMathScore()+"\t"+student.getEnglishScore()+"\t"+student.getTotalScore());
bfw.newLine();
bfw.flush();
}
bfw.close();
}
}
完