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

    xiaoxiao2022-07-07  150

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

    字符转换流 OutputStreamWriter 是字符流通向字节流的桥梁: 可使用指定的 编码表 将要写入流中的字符编码成字节。 它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。

    编码和解码 编码:把字符串转换成字节数组,把看的懂的转换成看不懂的 解码:把字节数组转换成字符串,把看不懂的,转换成看的懂 乱码:编解码采用的码表不一致,就会导致乱码

    byte[] getBytes () 使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。 byte[] getBytes (Charset charset) 使用给定的 charset 将此 String 编码到 byte 序列,并将结果存储到新的 byte 数组。

    码表:

    ASCII

    GB2312

    GBK

    UNICODE(usc-2 , usc-4)

    UTF-8

    tring类中的编码和解码问题

    编码: 就是把字符串转换成字节数组

    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 str = "你好中国"; byte[] bytes = str.getBytes("UTF-8"); //编码 //String( byte[] bytes) //通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。 //String( byte[] bytes, Charset charset) //通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。 String s = new String(bytes, "UTF-8");//解码 System.out.println(s);

    转换流OutputStreamWriter的使用

    OutputStreamWriter的构造方法 OutputStreamWriter(OutputStream out):根据默认编码(GBK)把字节流的数据转换为字符流

    OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流

    public class MyTest3 { public static void main(String[] args) throws IOException { //OutputStreamWriter(OutputStream out, Charset cs) //创建使用给定字符集的 OutputStreamWriter。 //OutputStreamWriter(OutputStream out, String charsetName) //创建使用指定字符集的 OutputStreamWriter。 //参数2:可以指定码表 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt",true), "UTF-8"); out.write("你好中国,跟我一起好吗"); out.write("\r\n"); //写入换行符 out.write("你好中国,跟我一起好吗",0,5);//写字符串的一部分,从 0开始,写5个字符 out.flush(); 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 MyTest { public static void main(String[] args) throws IOException { //InputStreamReader 是字节流通向字符流的桥梁: //它使用指定的 charset 读取字节并将其解码为字符。 //它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。 // InputStreamReader // InputStreamReader(InputStream in) // 创建一个使用默认字符集的 InputStreamReader。 // InputStreamReader(InputStream in, Charset cs) // 创建使用给定字符集的 InputStreamReader。 //输入流,所关联的文件如果不存在就会报错 InputStreamReader in = new InputStreamReader(new FileInputStream("c.txt")); int len = in.read(); //一次读取一个字符 System.out.println(len); len = in.read(); //一次读取一个字符 System.out.println(len); len = in.read(); //一次读取一个字符 System.out.println(len); len = in.read(); //一次读取一个字符 System.out.println(len); len = in.read(); //一次读取一个字符 System.out.println(len); len = in.read(); //一次读取一个字符 System.out.println(len); len = in.read(); //一次读取一个字符 System.out.println(len); //如果读取不到有效字符,返回 -1 我们就会拿-1 判断这个文件是否读完 in.close(); } }

    字符流复制文本文件

    public class MyTest2 { public static void main(String[] args) throws IOException { InputStreamReader in = new InputStreamReader(new FileInputStream("d.txt")); //定义一个容器 char[] chars = new char[100]; // int len = in.read(chars); //一次读取一些字符,放到字符数组中,返回的是实际读取到的有效字符个数 int len = in.read(chars, 0, 17);//从0开始读,读取17个字符放到容器中 System.out.println(len); System.out.println("------------------"); //for (char ch : chars) { // System.out.println(ch); //} System.out.println(new String(chars,0,len)); System.out.println(String.valueOf(chars,0,len)); in.close(); } }

    FileWriter和FileReader复制文本文件

    FileReader和FileWriter的出现 转换流的名字比较长,而我们常见的操作都是按照本地默认编码实现的, 所以,为了简化我们的书写,转换流提供了对应的子类。 FileWriter FileReader public class MyTest { public static void main(String[] args) throws IOException { // 父类 子类 便捷类 不能指定码表,用的是默认码表 // InputStreamReader---- FileReader // OutputStreamWriter---- FileWriter FileReader reader = new FileReader("MyTest.java"); FileWriter writer = new FileWriter("MyTest134.java"); char[] chars = new char[1000]; int len=0; while ((len=reader.read(chars))!=-1){ writer.write(chars,0,len); writer.flush(); } reader.close(); writer.close(); } }

    字符缓冲流的基本使用

    public class MyTest2 { public static void main(String[] args) throws IOException { //高效的字符流 里面有特有的方法比较好用 // BufferedReader // BufferedWriter BufferedReader bfr = new BufferedReader(new FileReader("MyTest134.java")); BufferedWriter bfw = new BufferedWriter(new FileWriter("MyTest1344444.java")); char[] chars = new char[1000]; int len = 0; while ((len = bfr.read(chars)) != -1) { bfw.write(chars, 0, len); bfw.flush(); } bfr.close(); bfw.close(); } }

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

    public class MyTest { public static void main(String[] args) throws IOException { //需求:把ArrayList集合中的字符串数据存储到文本文件 //分析: ArrayList<String> list = new ArrayList<>(); list.add("冯小刚"); list.add("张艺谋"); list.add("宁浩"); list.add("徐峥"); list.add("小四"); //把集合中的数据保存到文件文件中 //思路,遍历集合,取出数据写入文本文件 BufferedWriter writer = new BufferedWriter(new FileWriter("username.txt")); for (String s : list) { writer.write(s); writer.newLine(); writer.flush(); } writer.close(); } }

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

    public class MyTest2 { public static void main(String[] args) throws IOException { //需求是,把文本文件中的数据读取到集合中 ArrayList<String> list = new ArrayList<>(); BufferedReader reader = new BufferedReader(new FileReader("username.txt")); while (true){ String s = reader.readLine(); if(s!=null){ list.add(s); }else{ break; } } System.out.println(list); } }

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

    public class MyTest3 { public static void main(String[] args) throws IOException { // 需求:我有一个文本文件,每一行是一个学生的名字,请写一个程序,每次允许随机获取一个学生名称 BufferedReader bfr = new BufferedReader(new FileReader("username.txt")); ArrayList<String> list = new ArrayList<>(); while (true) { String s = bfr.readLine(); if (s != null) { list.add(s); } else { break; } } // System.out.println(list); Random random = new Random(); //生成一个随机索引 int index = random.nextInt(list.size()); String s = list.get(index); System.out.println(s); //程序跟数据进行解耦 } }

    复制单级文件夹

    public class MyTest { public static void main(String[] args) throws IOException { //复制单级文件夹 //1.封装源文件夹 File srcFolder = new File("E:\\test"); //2.封装目标文件夹 File targetFolder = new File("D:\\test"); if (!targetFolder.exists()) { targetFolder.mkdirs(); } //进行复制 copyFolder(srcFolder, targetFolder); System.out.println("复制完成"); } private static void copyFolder(File srcFolder, File targetFolder) throws IOException { //遍历源文件夹下,所有的文件,复制到目标文件夹下去 File[] files = srcFolder.listFiles(); for (File f : files) { if(f.isFile()){ copyFiles(f, targetFolder); }else{ //递归 } } } //复制文件 private static void copyFiles(File f, File targetFolder) throws IOException { //使用字节流来复制 FileInputStream in = new FileInputStream(f);//封装源文件 //封装目标文件 //File file = new File(targetFolder, f.getName()); //System.out.println(file); FileOutputStream out = new FileOutputStream(new File(targetFolder, f.getName())); 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(); } }
    最新回复(0)