字符流

mac2024-10-25  46

编解码

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

package org.org.westos.demo; import java.io.UnsupportedEncodingException; public class MyClass { public static void main(String[] args) throws UnsupportedEncodingException { //使用平台默认的编码表进行编解码 //编码 byte[] bytes = "好好学习,天天向上".getBytes(); //解码 String s = new String(bytes); System.out.println(s); //指定编码表进行编解码 //编码 byte[] bytes1 = "爱生活,爱Java".getBytes("GBK"); //解码 String str = new String(bytes1, "GBK"); System.out.println(str); //如果编解码用的编码表不一致,就会出现乱码 String s1 = new String(bytes1, "UTF-8"); System.out.println(s1); } }

字符流

当然,子类还有其他的,主要学习InputStreamReader和OutputStreamWriter

OutputStreamWriter

概述:它是字符流通向字节流的桥梁,可使用指定的charset将要写入流中的字符编码成字节构造方法 OutputStreamWriter(OutputStream out):根据默认编码把字节流的数据转换为字符流 OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流的数据转换为字符流其他方法 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) 写一个字符串的一部分 package org.org.westos.demo; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; public class MyClass { public static void main(String[] args) throws IOException { //创建一个输出流,关联的文件若不存在,自动创建 //可以指定编码表,不指定则使用默认码表 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("a.txt"),"UTF-8"); //写入数据 //一次写一个字符 out.write('好'); out.write("\r\n"); //一次写一个字符数组 char[] chars = {'a', 'b', 'c'}; out.write(chars); //一次写一个字符数组的一部分 out.write(chars,1,2); out.write("\r\n"); //一次写一个字符串 out.write("大家好"); out.write("\r\n"); //一次写一个字符串的一部分 String str = "好好学习,天天向上"; out.write(str,str.indexOf("天"),4); //字符流在写入数据后必须要刷新一下 out.flush(); //关闭流(关闭流之前也会刷新) out.close(); } }

InputStreamReader

概述:它是字节流通向字符流的桥梁,它也可使用指定的charset读取字节并解码成字符构造方法 InputStreamReader(InputStream in):创建一个使用默认字符集的InputStreamReader InputStreamReader(InputStream in,charset cs):创建使用给定字符集的InputStreamReader其他方法 public int read() 一次读取一个字符 public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1 package org.org.westos.demo; import java.io.*; public class MyClass { public static void main(String[] args) throws IOException { //创建一个输入流并关联文件 InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt")); //读取字符 //一次读取一个字符 int read = in.read(); System.out.println(read); //一次读取一个字符数组,返回值是读取到的有效字节个数 char[] chars = new char[1024]; int len = in.read(chars); String s = String.valueOf(chars); System.out.println(s); //若要读取一个字符数组的一部分,只需在读取的时候给定范围 //例如int len = in.read(chars,0,len); in.close(); } }

InputStreamReader和OutputStreamWriter的综合应用:赋值文本文件 (1)读一个字符,写一个字符

package org.org.westos.demo; import java.io.*; public class MyClass { public static void main(String[] args) throws IOException { //复制文本文件,并读一个字符,写一个字符 InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt")); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("b.txt")); //定义一个字符,记录读取的每个字符 int ch = 0; while((ch=in.read())!=-1){ out.write(ch); out.flush(); } in.close(); out.close(); } }

(2)读一个字符数组,写一个字符数组

package org.org.westos.demo; import java.io.*; public class MyClass { public static void main(String[] args) throws IOException { //复制文本文件,并读一个字符数组,写一个字符数组 InputStreamReader in = new InputStreamReader(new FileInputStream("a.txt")); OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("c.txt")); //定义一个字符,记录读取的每个字符 int ch = 0; //创建一个字符数组,充当缓冲区 char[] chars = new char[1024]; while((ch=in.read(chars))!=-1){ out.write(chars,0,ch); out.flush(); } in.close(); out.close(); } }

上面几个代码流的处理都是直接抛给虚拟机,我们也可以自己处理,即通过try,catch

package org.org.westos.demo; import java.io.*; public class MyClass { public static void main(String[] args) { InputStreamReader in=null; OutputStreamWriter out=null; try{ //复制文本文件,并读一个字符数组,写一个字符数组 in = new InputStreamReader(new FileInputStream("a.txt")); out = new OutputStreamWriter(new FileOutputStream("c.txt")); //定义一个字符,记录读取的每个字符 int ch = 0; //创建一个字符数组,充当缓冲区 char[] chars = new char[1024]; while((ch=in.read(chars))!=-1){ out.write(chars,0,ch); out.flush(); } }catch (IOException e){ e.printStackTrace(); }finally { try{ if(in!=null){ in.close(); } if(out!=null){ out.close(); } }catch (IOException e){ e.printStackTrace(); } } } }

便捷字符流

与字符流的关系: 注:便捷字符流与它的父类用法一摸一样,唯一的弊端就是不能指定码表,只能使用平台默认码表

高效字符流

高效的字符输出流:BufferedWriter 高效的字符输入流:BufferedReader构造方法 public BufferedWriter(Writer w) public BufferedReader(Reader e)其他方法 String readLine ():读取一个文本行 void newLine ():写入一个行分隔符应用(复制文本文件) package org.org.westos.demo; import java.io.*; public class MyClass { public static void main(String[] args) throws IOException { //读取一行,写出一行来复制文本文件 BufferedReader bir = new BufferedReader(new FileReader("a.txt")); BufferedWriter bfw = new BufferedWriter(new FileWriter("e.txt")); //复制 String line = null; while((line=bir.readLine())!=null){ bfw.write(line); bfw.newLine();//换行 bfw.flush(); } bir.close(); bfw.close(); } }

高效字符流跟高效字节流一样,效果是一样的,就是达到了高效的效果

至此,了解了字节流和字符流,那么,什么情况下使用那种流呢? 如果数据所在的文件通过windows自带的记事本打开并能读懂内容,就用字符流,其他用字节流 如果什么都不知道,就用字节流,因为字节流可以读写任何类型文件

最新回复(0)