字节流和字符流的转换类
OutputStreamWriter:是Writer的子类,将输出的字符流变成字节流
InputStreamReader:是Reader的子类,将输入的字节流变成字符流
将字节输出流变成字符输出流
//================================================= // File Name : OutputStreamWriter_demo //------------------------------------------------------------------------------ // Author : Common import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; //主类 //Function : 从文件中读取内容 public class OutputStreamWriter_demo { public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径 Writer out = null; out = new OutputStreamWriter(new FileOutputStream(f)); //字节流变成字符流 out.write("HelloWord"); out.close(); } }
将字节输出流变成字符输出流
//================================================= // File Name : InputStreamReader_demo //------------------------------------------------------------------------------ // Author : Common import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.Reader; import java.io.Writer; //主类 //Function : 从文件中读取内容 public class InputStreamReader_demo { public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.txt");//路径 Reader reader = null; reader = new InputStreamReader(new FileInputStream(f)); //字节流变成字符流 char c[] = new char[1024]; int len = reader.read(c); reader.close(); System.out.println(new String(c,0,len)); } }