一、String类中的编码和解码问题
编码: 就是把字符串转换成字节数组
-
把一个字符串转换成一个字节数组
-
public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
-
public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
-
解码: 把字节数组转换成字符串
-
public String(byte[] bytes): 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
-
public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
-
使用什么字符集进行编码,那么就是使用什么字符集进行解码
理解:
*** 编码:把看得懂的变成看不懂的: String – byte[]
解码:把看不懂的变成看得懂的: byte[] – String***
二、转换流OutputStreamWriter的使用
OutputStreamWriter的构造方法
OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
案例演示:
public class MyTest1 {
public static void main(String[] args) throws IOException {
/* 1、请编写程序,采用字符流多种方式,完成文本文件的复制,并测试
a)基本的流一次一个字符
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest2.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("b.java"));
int len=0;
char[] chars = new char[1024];
while ((len =in.read(chars))!=-1){
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
}
}
字符流的5种写数据的方式
public void write(int c) 写一个字符
public void write(char[] cbuf) 写一个字符数组
public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
public void write(String str) 写一个字符串
public void write(String str,int off,int len) 写一个字符串的一部分
三 、转换流InputStreamReader的使用
InputStreamReader的构造方法
InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
public class MyTest3 {
public static void main(String[] args) throws IOException {
//一次读取一个字符数组,写出一个字符数组 推荐 使用
InputStreamReader in = new InputStreamReader(new FileInputStream("MyTest.java"));
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("MyTest2.java"));
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();
}
}
四、 字符流复制文本文件
案例演示:
public class MyTest {
public static void main(String[] args) throws IOException {
FileReader in = new FileReader("src/MyTest.txt");
FileWriter out = new FileWriter("MyTest3.txt");
char[] chars=new char[1000];
int len=0;
while ((len=in.read(chars))!=-1){
//System.out.println("读取的次数"+len);
out.write(chars,0,len);
out.flush();
}
in.close();
out.close();
}
}
五 、字符缓冲流的基本使用
高效的字符流
高效的字符输出流: BufferedWriter
构造方法: public BufferedWriter(Writer w)
高效的字符输入流: BufferedReader
构造方法: public BufferedReader(Reader e)
特殊功能:
符缓冲流的特殊功能
BufferedWriter: public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
BufferedReader: public String readLine():一次读取一行数据 是以换行符为标记的 读到换行符就换行 没读到数 据返回null
包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
字符缓冲流的特殊功能复制文本文件
public class GaoXiao2 {
public static void main(String[] args) throws IOException {
BufferedReader bfr = new BufferedReader(new FileReader("MyTest2.java"));
BufferedWriter bfw = new BufferedWriter(new FileWriter("e.txt"));
String Line=null;
while ((Line=bfr.readLine())!=null){
bfw.write(Line);
bfw.newLine();
bfw.flush();
}
bfr.close();
bfw.close();
}
}
六、 把集合中的数据存储到文本文件
案例演示:
public class MyTest {
public static void main(String[] args) throws IOException {
//A:
//案例演示:
//需求:把ArrayList集合中的字符串数据存储到文本文件
ArrayList<String> list = new ArrayList<>();
list.add("张飞");
list.add("赵云");
list.add("马超");
list.add("黄忠");
list.add("关羽");
FileWriter writer = new FileWriter("username.txt");
for (String s : list) {
writer.write(s);
writer.write("\r\n");
writer.flush();
}
writer.close();
}
}
随机获取文本文件中的姓名
案例演示;
//案例演示:
//需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
//点名器
public class MyTest3 {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("username.txt")));
//读取一行数据,往集合里面添加一个
String line=null;
while ((line=reader.readLine())!=null){
list.add(line);
}
//随机从集合里面抽取一个人
Random random = new Random();
int index = random.nextInt(list.size());
String s = list.get(index);
System.out.println(s);
//数据跟程序解耦
}
}
七、复制单级文件夹
案例演示:
public class 复制单级文件夹 {
public static void main(String[] args) throws IOException {
File file = new File("C:\\Users\\MRCHENIKE\\Desktop\\许显军");
File file1 = new File("E:\\"+file.getName());
//判断是否有该文件夹
if (!file1.exists()){
//如果没有,创建一个文件夹
file1.mkdirs();
System.out.println();
}
copyFolder(file,file1);
}
private static void copyFolder(File file, File file1) throws IOException {
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isFile()){
copyFlies(file2,file1);
}else{
//如果不是文件,那么就是文件夹,打开文件夹,再一次回到上面方法
//递归
}
}
}
private static void readFile(String absolutePath) {
}
private static void copyFlies(File file2, File file1) throws IOException {
FileInputStream in = new FileInputStream(file2);
File f = new File(file1, file2.getName());
FileOutputStream out = new FileOutputStream(f);
int len=0;
byte[] bytes=new byte[1024*8];
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
out.flush();
}
in.close();
out.close();
}
}