21-java学习-字符流出现的原因、String中的编解码问题、转换流&案例代码

21-java学习-字符流出现的原因、String中的编解码问题、转换流&案例代码

目录:

字符流出现的原因
String中的编解码问题
转换流

1.字符流出现的原因及编码表概述和常见编码表

A:字符流出现的原因:由于字节流操作中文不是特别方便,所以,java就提供了字符流。
B:码表
C:字符流:  字符流 = 字节流 + 编码表

2.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[] bytes, String charsetName):通过指定的字符集解码字节数组
byte[] getBytes(String charsetName):使用指定的字符集合把字符串编码为字节数组
 
编码:把看得懂的变成看不懂的:	String -- byte[]
解码:把看不懂的变成看得懂的:	byte[] -- String

###21.03_IO流(转换流OutputStreamWriter的使用)(掌握)

A:OutputStreamWriter的构造方法
	OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流
	OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
B:案例演示:	OutputStreamWriter写出数据

3.字符流的5种写数据的方式

A: 方法概述
	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) 写一个字符串的一部分

4.转换流InputStreamReader的使用

A:InputStreamReader的构造方法
	InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
	InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据

5.字符流的2种读数据的方式

A:	方法概述
	public int read() 一次读取一个字符
	public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
A:案例演示:	字符流的2种读数据的方式
1.	int ch = 0 ; 		// 记录读取到的字符
	while((ch = isr.read()) != -1){
		System.out.print((char)ch);
	}
	
	// 释放资源
	isr.close() ;


2.	char[] chs = new char[1024] ;
	int len = 0 ;				// 作用: 记录读取到有效的字符个数
	while((len = isr.read(chs)) != -1){
		//System.out.print(String.valueOf(chs, 0, len));
		System.out.print(new String(chs , 0 , len));
	} 
	
	// 释放资源
	isr.close() ;

6.字符流复制文本文件

A:案例演示:	字符流复制文本文件

         // 频繁的读写操作
         // 一次读取一个字符数组复制文件
	char[] chs = new char[1024];
	int len = 0 ;
	
	while((len = isr.read(chs)) != -1){
		osw.write(chs, 0, len) ;
	}

7.FileWriter和FileReader复制文本文件

A:FileReader和FileWriter的出现
	转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的,
	所以,为了简化我们的书写,转换流提供了对应的子类。	
	FileWriter
	FileReader

字符流便捷类:	因为转换流的名字太长了,并且在一般情况下我们不需要制定字符集,
                 于是java就给我们提供转换流对应的便捷类
                 - 
- 转换流				便捷类
- OutputStreamWriter	-------		FileWriter
- InputStreamReader	-------		FileReader
  例子:
  BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt")) ;

// 频繁的读写操作
// 一次读取一个字符复制文件
// 一次读取一个字符数组复制文件
char[] chs = new char[1024] ;
int len = 0 ;
while((len = br.read(chs)) != -1){
	bw.write(chs, 0, len) ;
}

// 释放资源
bw.close() ;
br.close() ;

8.字符缓冲流的基本使用

 高效的字符流
	  高效的字符输出流:	BufferedWriter
	  		     构造方法:	public BufferedWriter(Writer w)
	 高效的字符输入流:	BufferedReader
	 		    构造方法:   public BufferedReader(Reader e)

9.字符缓冲流复制文本文件

A:案例演示:	字符缓冲流复制文本文件
       // 创建对象
	BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
	BufferedWriter bw = new BufferedWriter(new FileWriter("c.txt")) ;
	
	// 频繁的读写操作
	// 一次读取一个字符复制文件
	// 一次读取一个字符数组复制文件
	char[] chs = new char[1024] ;
	int len = 0 ;
	while((len = br.read(chs)) != -1){
		bw.write(chs, 0, len) ;
	}
	
	// 释放资源
	bw.close() ;
	br.close() ;

10.字符缓冲流的特殊功能

A:字符缓冲流的特殊功能
	BufferedWriter:	public void newLine():根据系统来决定换行符 具有系统兼容性的换行符
	BufferedReader:	public String readLine():一次读取一行数据  是以换行符为标记的 读到换行符就换行 没读到数据返回null
		包含该行内容的字符串,不包含任何行终止符,如果已到达流末尾,则返回 null
B:案例演示:	字符缓冲流的特殊功能

11.字符缓冲流的特殊功能复制文本文件

A:案例演示:	字符缓冲流的特殊功能复制文本文件

	// 创建对象
	BufferedReader br = new BufferedReader(new FileReader("OutputStreamWriterDemo.java")) ;
	BufferedWriter bw = new BufferedWriter(new FileWriter("copyFile2.txt")) ;
	
	// 频繁的读写操作
	String line = null ;		// 作用: 记录读取到的行数据
	while((line = br.readLine()) != null){
		bw.write(line) ;
		bw.newLine() ;
		bw.flush() ;
	}
	
	// 释放资源
	bw.close() ;
	br.close() ;

12.把集合中的数据存储到文本文件

A:案例演示: 需求:把ArrayList集合中的字符串数据存储到文本文件
分析:
- a: 创建一个ArrayList集合
- b: 添加元素
- c: 创建一个高效的字符输出流对象
- d: 遍历集合,获取每一个元素,把这个元素通过高效的输出流写到文本文件中
- e: 释放资源
public class MyTest4 {
    public static void main(String[] args) throws IOException {
        /*A:
        案例演示:
        需求:把ArrayList集合中的字符串数据存储到文本文件*/
        ArrayList<String> list = new ArrayList<>();
        list.add("赵四");
        list.add("刘能");
        list.add("王大拿");
        list.add("谢大脚");
        //遍历集合中的数据,通过输出流,写入到文本文件中保存
        FileWriter fileWriter = new FileWriter("names.txt");
        for (String name : list) {
           // System.out.println(name);
            fileWriter.write(name);
            fileWriter.write("\r\n");
            fileWriter.flush();
        }

        fileWriter.close();

    }
}

13.把文本文件中的数据存储到集合中

A:案例演示:	需求:从文本文件中读取数据(每一行为一个字符串数据)到集合中,并遍历集合
分析:
	 * 		a: 创建高效的字符输入流对象
	 * 		b: 创建一个集合对象
	 * 		c: 读取数据(一次读取一行)
	 * 		d: 把读取到的数据添加到集合中
	 * 		e: 遍历集合
	 * 		f: 释放资源
public class MyTest5 {
    public static void main(String[] args) throws IOException {
        //把文本文件中的数据读取到集合中取
        ArrayList<String> list = new ArrayList<>();
        //读取文本文件,把文件中的数据读取到集合
        //采用高效的字符输入流,一次读取一行
        BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream("names.txt")));
        String name=null;
        while ((name=bfr.readLine())!=null){
            list.add(name);
        }
        System.out.println(list);
        bfr.close();
    }
}

14.随机获取文本文件中的姓名

A:案例演示:	需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称
 * 分析:
	 * 		a: 创建一个高效的字符输入流对象
	 * 		b: 创建集合对象
	 * 		c: 读取数据,把数据存储到集合中
	 * 		d: 产生一个随机数,这个随机数的范围是 0 - 集合的长度 . 作为: 集合的随机索引
	 * 		e: 根据索引获取指定的元素
	 * 		f: 输出
	 * 		g: 释放资源
	 public class MyTest6 {
    public static void main(String[] args) throws IOException {
       /* 案例演示:
        需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称*/
        ArrayList<String> list = new ArrayList<>();
        //读取文本文件,把文件中的数据读取到集合
        //采用高效的字符输入流,一次读取一行
        BufferedReader bfr = new BufferedReader(new InputStreamReader(new FileInputStream("names.txt")));
        String name = null;
        while ((name = bfr.readLine()) != null) {
            list.add(name);
        }

        //System.out.println(list);

        bfr.close();
        //随机从集合中抽取一个人
        //生成一个随机索引
        Random random = new Random();
        int index = random.nextInt(list.size());
        String s = list.get(index);
        System.out.println(s+"====来回答问题");
        //把数据和程序解耦
    }
}

15.复制单级文件夹

A:案例演示:	需求: 复制D:\\course这文件夹到E:\\course
- 分析:
- a: 封装D:\\course为一个File对象
- b: 封装E:\\course为一个File对象,然后判断是否存在,如果不存在就是创建一个目录
- c: 获取a中的File对应的路径下所有的文件对应的File数组
- d: 遍历数组,获取每一个元素,进行复制
- e: 释放资源

 //  获取a中的File对应的路径下所有的文件对应的File数组
		File[] files = srcFolder.listFiles() ;	
	// 遍历
	for(File f : files){
		
		// 获取当前文件对应的名称
		String currentFileName = f.getName() ;
		
		// 构建目标文件
		File destFile = new File(destFolder , currentFileName) ;
		
		// 复制
		copyFile(f , destFile) ;
		
	}
}
/**
 * 复制文件
 * @throws IOException 
 */
public static void copyFile(File f, File destFile) throws IOException {
	
	// 创建高效的字节流对象
	BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f)) ;
	BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile)) ;
	
	// 频繁的读写操作
	// 一次读取一个字节数组复制文件
	byte[] bytes = new byte[1024] ;
	int len = 0 ;
	while((len = bis.read(bytes)) != -1){
		bos.write(bytes, 0, len) ;
	}
	
	// 释放资源
	bos.close() ;
	bis.close() ;
	
}

16.复制指定目录下指定后缀名的文件并修改名称

A:案例演示:	* 需求: 复制D:\\demo目录下所有以.java结尾的文件到E:\\demo .并且将其后缀名更改文.jad
- 分析:
- a: 复制D:\\demo文件夹到E:\\
- b: 对E:\\demo文件夹下的文件进行重命名

 // 封装D:\\demo为一个File对象
		File srcFolder = new File("D:\\demo") ;
// 封装E:\\demo为一个File对象
	File destFolder = new File("E:\\demo") ;
	
	// 判断是否存在
	if(!destFolder.exists()){
		
		// 创建
		destFolder.mkdir() ;
	}
	
	// 复制文件夹
	copyFolder(srcFolder , destFolder) ;
	
	// ---------------------上面的代码是复制文件夹,下面的代码是更改名称-----------------------------------------
	
	// 获取E:\\demo下的所有的文件对应File数组
	File[] listFiles = destFolder.listFiles() ;
	
	// 遍历
	for(File file : listFiles){
		
		// 构建目标文件
		String currentFileName = file.getName() ;
		currentFileName = currentFileName.replace(".java", ".jad") ;
		File destFile = new File(destFolder , currentFileName) ;
		
		// 重命名
		file.renameTo(destFile) ;
		
	}
	
}

/**
 * 复制文件夹
 * @throws IOException 
 */
private static void copyFolder(File srcFolder, File destFolder) throws IOException {
	
	// 获取
	File[] files = srcFolder.listFiles(new FilenameFilter() {
		@Override
		public boolean accept(File dir, String name) {
			return new File(dir, name).isFile() && name.endsWith(".java");
		}
	}) ;
	
	// 遍历
	for(File file : files){
		
		// 获取名称
		String currentFileName = file.getName() ;
		
		// 构建目标文件
		File destFile = new File(destFolder , currentFileName) ;
		
		// 复制
		StreamTest4.copyFile(file, destFile) ;
	}
}

17.键盘录入学生信息按照总分排序并写入文本文件

A:案例演示:	需求:键盘录入3个学生信息(姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)),
- 按照总分从高到低存入文本文件
- 
- 分析: 	
- a: 创建一个学生类: 姓名,语文成绩(chineseScore),数学成绩(mathScore),英语成绩(englishScore)
- b: 因为要排序,所以需要选择TreeSet进行存储学生对象
- c: 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
- d: 创建一个高效的字符输出流对象
- e: 遍历集合,把学生的信息写入到指定的文本文件中
- f: 释放资源
//因为要排序,所以需要选择TreeSet进行存储学生对象
		TreeSet<Student> students = new TreeSet<Student>(new Comparator<Student>() {
@Override
		public int compare(Student s1, Student s2) {
			
			// 比较总分
			int num = s2.getTotal() - s1.getTotal() ;
			
			// 比较姓名
			int num2 = (num == 0 ) ? s2.getName().compareTo(s1.getName()) : num ;
			
			// 返回
			return num2;
		}
	}) ;
	
	// 键盘录入学生信息,把学生信息封装成一个学生对象,在把学生对象添加到集合中
	for(int x = 0 ; x < 3 ; x++){
		
		int i =  x + 1  ;
		
		System.out.println("第" + i + "个学生信息录入开始................................");
		
		// 创建一个Scanner对象
		Scanner sc = new Scanner(System.in) ;
		System.out.println("请您输入第" + i + "个学生的姓名: ");
		String name = sc.nextLine() ;
		System.out.println("请您输入第" + i + "个学生的语文成绩: ");
		String chineseScoreStr = sc.nextLine() ;
		System.out.println("请您输入第" + i + "个学生的数学成绩: ");
		String mathScoreStr = sc.nextLine() ;
		System.out.println("请您输入第" + i + "个学生的英语成绩: ");
		String englishScoreStr = sc.nextLine() ;
		
		// 创建一个学生对象
		Student s = new Student() ;
		s.setName(name) ;
		s.setChineseScore(Integer.parseInt(chineseScoreStr)) ;
		s.setMathScore(Integer.parseInt(mathScoreStr)) ;
		s.setEnglishScore(Integer.parseInt(englishScoreStr)) ;
		
		// 把学生对象添加到集合中
		students.add(s) ;
		
	}
	
	// 创建一个高效的字符输出流对象
	BufferedWriter bw = new BufferedWriter(new FileWriter("studentInfo.config")) ;
	bw.write("姓名\t\t总分\t\t语文成绩\t\t数学成绩\t\t英语成绩") ;
	bw.newLine() ;
	bw.flush() ;
	
	// 遍历
	for(Student s : students){
		bw.write(s.getName() + "\t\t" + s.getTotal() + "\t\t" + s.getChineseScore() + "\t\t" + s.getMathScore() + "\t\t" + s.getEnglishScore()) ;
		bw.newLine() ;
		bw.flush(); 
	}
	
	// 释放资源
	bw.close() ;

	//提示
	System.out.println("学生信息存储完毕");
发布了49 篇原创文章 · 获赞 9 · 访问量 1446

猜你喜欢

转载自blog.csdn.net/weixin_42401546/article/details/104815675