IO-读写文件
来源:互联网 发布:veleq电气仿真软件 编辑:程序博客网 时间:2024/05/29 16:31
一、用字节流读写文件
1、写文件
//字节流向文件中写东西public static void main(String[] args) throws IOException{String name="D:"+File.separator+"sun.txt";File f=new File(name);OutputStream ou=new FileOutputStream(f);String str="红鲤鱼与绿鲤鱼与驴,我是字符流";byte[] b=str.getBytes();//字节流try {ou.write(b); //输出流} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{ou.close();}}
2、读文件
//用字节流读取文件内容public static void main(String[] args) throws IOException{String name="D:"+File.separator+"sun.txt";File f=new File(name);InputStream in=new FileInputStream(f);byte[] b=new byte[100];in.read(b);in.close();System.out.println(b);System.out.println(new String(b));}
二、用字符流读写文件
1、写文件
//字符流写入内容到文本public static void main(String[] args) throws IOException {String name="D:"+File.separator+"sun1.txt";File f=new File(name);Writer o =new FileWriter(f);String str="红鲤鱼与绿鲤鱼与驴,我是字符流";o.write(str);o.close();}
2、读文件
//用字符流读取内容public static void main(String[] args) throws IOException {// TODO Auto-generated method stubString name="D:"+File.separator+"sun1.txt";File f=new File(name);char[] ch=new char[100];Reader rd=new FileReader(f);rd.read(ch);rd.close();System.out.println(ch);System.out.println(new String(ch));}
字符流-字节流
1、字节(byte)是计算机存储数据最小单位,以进制存储。一个汉字占两个字节。
2、字符(char)是计算机使用过程中的符号或英文或数字或汉字。
3、用字节流没有缓冲区,用字符流有缓冲区。(不能马上看到效果)
4、用字符流写,不关闭缓冲流,写不进去。
5、字符流效率高一些,字节流效率低一些。因磁盘中的数据文件都是用字节存储的,一般开始时,字节流更广泛。
阅读全文
0 0
- JAVA IO读写文件
- Java IO 读写文件
- 文件读写(IO)
- 【IO】文件读写实用工具
- 文件读写IO
- IO流读写文件
- JAVA-IO读写文件
- IO文件及其读写
- java IO读写文件
- IO 流读写文件
- Java IO 读写文件
- IO流文件读写
- IO读写文件
- io文件读写
- Java IO 读写文件
- Java IO文件读写
- 文件读写IO操作
- Io流读写文件
- 设置MongoDB成Windows服务
- Java基础:编码格式
- K:找寻最长子序列的四个算法
- PL/SQL之一 基础语法
- Leetcode-606. Construct String from Binary Tree
- IO-读写文件
- HTML 显示网页图片 若无法显示图片则显示文字
- 在Myeclipse或者eclipse下创建tld文件步骤详解:
- POI导入Excel表格数据到数据库中
- Angular Material GridList组件
- LeetCode-98. Validate Binary Search Tree
- 以太坊核心概念开发如何用数字货币引领区块链
- opencv编译安装contrib包的注意事项和错误解决
- 数据库探索之旅——初识数据库