首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >JavaIO流输入输出流-字符流

JavaIO流输入输出流-字符流

作者头像
待你如初见
发布2018-08-01 17:46:38
发布2018-08-01 17:46:38
9030
举报
文章被收录于专栏:待你如初见待你如初见

字节字符转换流

认识文本和文本文件

123

java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)文件是byte byte byte ...的数据序列文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化为byte的存储结果

字符流(Reader Writer)—->操作的是文本文本文件

123456

字符的处理,一次处理一个字符字符的底层任然是基本的字节序列字符流的基本实现 InputStreamReader 完成byte流解析为char流,按照编码解析 OutputStreamWriter 提供char流到byte流,按照编码处理 FileReader/FileWriter

字符流的过滤器

123456789101112131415161718192021

//BufferedReader ---->readLine 一次读一行 //BufferedWriter/PrintWriter ---->写一行 public static void brAndBw() throws IOException { BufferedReader br=null; BufferedWriter bw=null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream("E:\\VSC.dat"))); bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:\\javaio\\VSC.dat"))); String line; while ((line=br.readLine())!= null){ System.out.println(line); bw.write(line);//无换行 //bw.newLine();//添加换行 } } catch (IOException e) { e.printStackTrace(); }finally { bw.close(); br.close(); } }

读文件示例

12345678910111213141516

public static void main(String[] args) throws IOException { FileInputStream fis=null; InputStreamReader isr=null; try { fis = new FileInputStream("G://path.txt"); isr = new InputStreamReader(fis,"utf-8");//默认为项目编码可指定 int c; while ((c=isr.read())!=-1){ System.out.print((char) c); } } catch (IOException e) { e.printStackTrace(); }finally { isr.close(); fis.close(); }

文件复制示例

123456789101112131415161718192021222324252627

public static void main(String[] args) throws IOException { FileInputStream fis=null; InputStreamReader isr=null; FileOutputStream fos=null; OutputStreamWriter osw=null; try { fis = new FileInputStream("G://path.txt"); isr = new InputStreamReader(fis,"utf-8");//默认为项目编码可指定 fos = new FileOutputStream("G://path2.txt"); osw = new OutputStreamWriter(fos); int c; char[] a = new char[1024]; while ((c = isr.read(a,0,a.length)) != -1){ String s = new String(a,0,c); System.out.println(s); osw.write(a,0,c); } } catch (IOException e) { e.printStackTrace(); }finally { fis.close(); osw.close(); } }

字节字符读写流

123456789101112131415161718192021

public static void fileCopyByFrAndFw() throws IOException { FileWriter fw = null; FileReader fr = null; int c; char[] chars = new char[1024]; try { fr = new FileReader("e://javaio//text.dat"); fw = new FileWriter("e://text.dat");//FileWriter("e://text.dat",true)追加操作 while ((c = fr.read(chars,0,chars.length))!=-1){ System.out.println(chars); fw.write(chars,0,c); } } catch (IOException e) { e.printStackTrace(); }finally { fw.close(); fr.close(); } }

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-01-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字节字符转换流
    • 认识文本和文本文件
    • 字符流(Reader Writer)—->操作的是文本文本文件
    • 字符流的过滤器
    • 读文件示例
    • 文件复制示例
  • 字节字符读写流
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档