字节流、字符流 谁践踏了优雅 2021-12-20 15:29 307阅读 0赞 **字节流** 字节输出流OutputStream: OutputStream此抽象类,是表示输出字节流的所有类的超类 FileOutputStream类: public static void main(String\[\] args) throws IOException \{ //创建字节输出流对象 //构造方法指定文件地址时,如果存在则覆盖,如果不存在则创建。 FileOutputStream fos=new FileOutputStream("D:\\\\java\\\\d.txt"); /\*//向文件中写入一个字节write(int b) ASCII码 //0-48 //a-97 //A-65 fos.write(49); fos.write(48); fos.write(48);\*/ //向文件中写入一个字节数组 byte\[\] bytes=\{-66,-67,-68,-69\}; //fos.write(bytes); fos.write(bytes, 2, 2); //释放资源 fos.close(); \} 给文件中续写和换行 public static void main(String\[\] args) throws IOException \{ //创建字节输出流 FileOutputStream fos=new FileOutputStream("D:\\\\java\\\\d.txt",true); //字符串-》字符数组 fos.write("abc".getBytes()); fos.write("\\r\\n换行了".getBytes()); //释放资源 fos.close(); \} 异常的处理 public static void main(String\[\] args)\{ FileOutputStream fos=null; try \{ fos=new FileOutputStream("D:\\\\java\\\\d.txt",true); fos.write("abc".getBytes()); \} catch (IOException e) \{ e.printStackTrace(); \}finally \{ try \{ fos.close(); \} catch (IOException e) \{ e.printStackTrace(); \} \} \} ## **字节输入流InputStream** ## public static void main(String\[\] args) throws IOException \{ //创建字节输入流(明确从哪个文件中读取数据) FileInputStream fis=new FileInputStream("D:\\\\java\\\\d.txt"); //一个字节一个字节读取文件所有数据 int len=0; while((len=fis.read())!=-1)\{ System.out.println((char)len); \} /\*System.out.println((char)fis.read()); System.out.println((char)fis.read()); System.out.println((char)fis.read()); System.out.println((char)fis.read()); System.out.println((char)fis.read()); System.out.println(fis.read());\*/ //释放资源 fis.close(); \} public static void main(String\[\] args) throws IOException \{ //明确数据源 FileInputStream fis=new FileInputStream("D:\\\\java\\\\d.txt"); //创建字节数组 byte\[\] bytes=new byte\[2\]; /\*//读取一个字节数组 //实际读取的有效字节 System.out.println(fis.read(bytes)); //字节数组转字符串 System.out.println(new String(bytes));\*/ int len=0; while((len=fis.read(bytes))!=-1)\{ System.out.println(new String(bytes,0,len)); \} //释放资源 fis.close(); \} **字符流** 字符编码表: ascii iso-8859-1 GB2312 GBK GB18030 unicode UTF-8 常见的编码 GBK UTF-8 ISO-8859-1 文字\--->(数字) :编码。 “abc”.getBytes() byte\[\] (数字)--->文字 : 解码。 byte\[\] b=\{97,98,99\} new String(b,0,len) FileReader类: public static void main(String\[\] args) throws IOException \{ //创建字符输入流 FileReader fr=new FileReader("D:\\\\java\\\\d.txt"); int len=0; while((len=fr.read())!=-1)\{ System.out.println((char)len); \} //释放资源 fr.close(); \} public static void main(String\[\] args) throws IOException \{ //创建字符输入流 FileReader fr=new FileReader("D:\\\\java\\\\d.txt"); char\[\] c=new char\[1024\]; int len=0; while((len=fr.read(c))!=-1)\{ System.out.println(new String(c,0,len)); \} fr.close(); \} public static void main(String\[\] args) throws IOException \{ //创建字符输出流 FileWriter fw=new FileWriter("D:\\\\java\\\\d.txt"); //写入一个字符 fw.write(100); fw.flush(); //写入一个字符串 fw.write("你好呀"); //写入一个字符数组 char\[\] c=\{'a','1','r'\}; fw.write(c); //释放资源 //fw.close(); \} public static void main(String\[\] args) throws IOException \{ //明确数据源 FileReader fr=new FileReader("D:\\\\java\\\\d.txt"); //明确目的地 FileWriter fw=new FileWriter("D:\\\\java\\\\z.txt"); int len=0; //开始复制 Date d=new Date(); System.out.println(d.getTime()); while((len=fr.read())!=-1)\{ fw.write(len); fw.flush(); \} //释放资源 fr.close(); fw.close(); System.out.println(d.getTime()); \} 转载于:https://www.cnblogs.com/boss-H/p/11038099.html
还没有评论,来说两句吧...