字符集基础知识:
- 编码:把文字转换成字节
- 解码:把字节转换成对应的中文形式(编码前和编码后的字符集必须一致,否则乱码)
package com.gch.d3_charset;import java.io.UnsupportedEncodingException;
import java.util.Arrays;/**目标:学会自己进行文字的编码和解码,为以后可能用到的场景做准备*/
public class Test {public static void main(String[] args) throws UnsupportedEncodingException {// 1.编码:把文字转换成字节(使用指定的编码)String name = "528周杰伦";
// byte[] bytes = name.getBytes(); // 以当前代码默认字符集进行编码(UTF-8)byte[] bytes = name.getBytes("GBK");System.out.println(bytes.length); // 9System.out.println(Arrays.toString(bytes)); // [53, 50, 56, -42, -36, -67, -36, -62, -41]// 2.解码:把字节转换成对应的中文形式(编码前 和 编码后的字符集必须一致,否则乱码)
// String rs = new String(bytes); // 默认的UTF-8String rs = new String(bytes,"GBK"); // 指定GBK解码System.out.println(rs); // 528周杰伦}
}
package com.gch.d4_byte_stream;import java.io.*;public class FileInputStreamDemo1 {public static void main(String[] args) throws IOException {// 1.创建一个文件字节输入流管道与源文件接通。
// InputStream is = new FileInputStream(new File("day09-file-io-app\\src\\data.txt"));// 简化写法InputStream is = new FileInputStream(("day09-file-io-app\\src\\data.txt"));// 2.读取一个字节返回(每次读取1滴水)
// int b1 = is.read();
// System.out.println(b1); // 97
// System.out.println((char)b1); // a
//
// int b2 = is.read();
// System.out.println((char) b2); // b
//
// int b3 = is.read();
// System.out.println((char) b3); // 3
//
// int b4 = is.read(); // 读取完毕返回-1
// System.out.println(b4);// 3.使用循环改进// 定义一个变量记录每次读取的字节int b;while((b = is.read()) != -1){System.out.print((char)b); // ab3}}
}
package com.gch.d4_byte_stream;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;/**目标:使用文件字节输入流每次读取一个字节数组的数据*/
public class FileInputStreamDemo02 {public static void main(String[] args) throws Exception {// 1.创建一个文件字节输入流管道与源文件接通InputStream is = new FileInputStream("day09-file-io-app/src/data02.txt");// 2.定义一个字节数组,用于读取字节数组
// byte[] buffer = new byte[3]; // 3B:3个字节 1KB 1K代表1024个数据 B代表Byte字节// int len = is.read(buffer);
// System.out.println("读取了" + len + "个字节。"); // 读取了3个字节。
// // 解码
// String rs = new String(buffer);
// System.out.println(rs); // gch
//
// int len1 = is.read(buffer);
// System.out.println("读取了" + len1 + "个字节。"); // 读取了3个字节。
// // 解码
// String rs1 = new String(buffer);
// System.out.println(rs1); // abc
// // buffer = [ab c]
//
// // buffer = [ed c]
// int len2 = is.read(buffer);
// System.out.println("读取了" + len2 + "个字节。"); // 读取了2个字节。
// // 解码
// // 读取多少倒出多少(重点)
// String rs2 = new String(buffer,0,len2);
// System.out.println(rs2); // ed
//
// int len3 = is.read(buffer);
// System.out.println(len3); // -1 读取完毕返回-1// 3.改进使用循环,每次读取一个字节数组byte[] buffer = new byte[3];int len; // 记录每次读取的字节数while((len = is.read(buffer)) != -1){// 读取多少倒出多少System.out.print(new String(buffer, 0 ,len)); // gchabced}}
}
package com.gch.d4_byte_stream;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;/**目标:使用文件字节输入流一次读完文件的全部字节,可以解决乱码问题*/
public class FileInputStreamDemo03 {public static void main(String[] args) throws Exception {// 1.创建一个文件字节输入流管道与源文件接通File f = new File("day09-file-io-app/src/data03.txt");InputStream is = new FileInputStream(f);// 2.定义一个字节数组与文件的大小刚刚一样大byte[] buffer = new byte[(int)f.length()];int len = is.read(buffer);System.out.println("读取了" + len + "个字节!"); // 读取了89个字节!System.out.println("文件大小:" + f.length()); // 文件大小:89// 解码System.out.println(new String(buffer));}
}
package com.gch.d4_byte_stream;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;/**目标:字节输出流的使用。IO流的体系:字节流 字符流字节输入流 字节输出流 字符输入流 字符输出流InputStream OutputStream Reader Writer (抽象类)FileInputStream FileOutputStream FileReader FileWriter (实现类)a.FileOutputStream文件字节输出流。-- 作用:以内存为基准,把内存中的数据,按照字节的形式写出到磁盘文件中去。简单来说,把内存数据按照字节写出到磁盘文件中去。-- 构造器:public FileOutputStream(File file):创建一个字节输出流管道通向目标文件对象。public FileOutputStream(String file):创建一个字节输出流管道通向目标文件路径。public FileOutputStream(File file , boolean append):创建一个追加数据的字节输出流管道通向目标文件对象。public FileOutputStream(String file , boolean append):创建一个追加数据的字节输出流管道通向目标文件路径。-- 方法:public void write(int a):写一个字节出去 。public void write(byte[] buffer):写一个字节数组出去。public void write(byte[] buffer , int pos , int len):写一个字节数组的一部分出去。参数一,字节数组;参数二:起始字节索引位置,参数三:写多少个字节数出去。小结:记住。换行: os.write("\r\n".getBytes()); // 换行追加数据管道: OutputStream os = new FileOutputStream("day10_demo/out01.txt" , true); // 追加管道!!*/
public class OutputStreamDemo04 {public static void main(String[] args) throws Exception {// 1、创建一个文件字节输出流管道与目标文件接通// OutputStream os = new FileOutputStream("day09-file-io-app/src/out04.txt");// 覆盖管道,先清空之前的数据,写新数据进去OutputStream os = new FileOutputStream("day09-file-io-app/src/out04.txt",true);// 追加数据管道!// 2、写数据出去// a.public void write(int a):写一个字节出去os.write('a');os.write(98);os.write("\r\n".getBytes()); // 换行
// os.write('晖'); // [ooo]// b.public void write(byte[] buffer):写一个字节数组出去。byte[] buffer = {'a', 97, 98, 99};os.write(buffer);os.write("\r\n".getBytes()); // 换行byte[] buffer1 = "我是中国人".getBytes();
// byte[] buffer1 = "我是中国人".getBytes("GBK"); // 乱码os.write(buffer1);os.write("\r\n".getBytes()); // 换行// c. public void write(byte[] buffer , int pos , int len):写一个字节数组的一部分出去。byte[] buffer2 = {'a',98,99,100};os.write(buffer2, 0 ,3);os.write("\r\n".getBytes()); // 换行// 写数据,必须刷新数据,刷新后可以继续使用流os.flush();// 释放资源,包含了刷新的,关闭后流不可以使用了
// os.close();}
}
package com.gch.d5_resource;import java.io.*;/**目标:学会使用finally释放资源*/
public class CopyDemo01 {public static void main(String[] args) {System.out.println(test(10, 2)); // 100}}public static int test(int a,int b){try{int c = a / b;
// System.exit(0);return c;}catch(Exception e){e.printStackTrace();return -1111; // 计算出现Bug}finally {// 哪怕上面有return语句执行,也必须先执行完这里才可以!// 开发中不建议在这里加return,如果加了,返回的永远是这里的数据了,这样会出问题!return 100;}}
}
package com.gch.d5_resource;import java.io.*;/**目标:学会使用finally释放资源*/
public class CopyDemo01 {public static void main(String[] args) {InputStream is = null;OutputStream os = null;try {// 1.创建一个字节输入流管道与原视频接通is = new FileInputStream("C:\\Users\\A.G.H\\Desktop\\蓝杰\\并发编程\\002弹球游戏视频.mp4");// 2.创建一个字节输出流管道与目标文件接通os = new FileOutputStream("C:\\Users\\A.G.H\\new.mp4");// 3.定义一个字节数组转移数据byte[] buffer = new byte[1024];int len; // 记录每次读取的字节数while((len = is.read(buffer)) != -1){// 读取多少倒出多少os.write(buffer, 0 ,len);}System.out.println("复制完成~");
// System.out.println(10 / 0);return;} catch (IOException e) {e.printStackTrace();} finally {// 无论代码是正常结束还是出现异常,finally代码块都会执行System.out.println("======finally======");// 4.关闭流try {if(os != null)os.close();} catch (IOException e) {throw new RuntimeException(e);}try {if(is != null)is.close();} catch (IOException e) {throw new RuntimeException(e);}}}
}