java中的IO操作总结(一)
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- //创建要操作的文件路径和名称
- //其中,File.separator表示系统相关的分隔符,Linux下为:/ Windows下为:\\
- String path = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- //由于IO操作会抛出异常,因此在try语句块的外部定义FileWriter的引用
- FileWriter w = null;
- try {
- //以path为路径创建一个新的FileWriter对象
- //如果需要追加数据,而不是覆盖,则使用FileWriter(path,true)构造方法
- w = new FileWriter(path);
- //将字符串写入到流中,\r\n表示换行想有好的
- w.write("Nerxious is a good boy\r\n");
- //如果想马上看到写入效果,则需要调用w.flush()方法
- w.flush();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //如果前面发生异常,那么是无法产生w对象的
- //因此要做出判断,以免发生空指针异常
- if(w != null) {
- try {
- //关闭流资源,需要再次捕捉异常
- w.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- import java.io.File;
- import java.io.FileReader;
- import java.io.IOException;
- public class Demo2 {
- public static void main(String[] args ) {
- String path = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- FileReader r = null;
- try {
- r = new FileReader(path);
- //方式一:读取单个字符的方式
- //每读取一次,向下移动一个字符单位
- int temp1 = r.read();
- System.out.println((char)temp1);
- int temp2 = r.read();
- System.out.println((char)temp2);
- //方式二:循环读取
- //read()方法读到文件末尾会返回-1
- /*
- while (true) {
- int temp = r.read();
- if (temp == -1) {
- break;
- }
- System.out.print((char)temp);
- }
- */
- //方式三:循环读取的简化操作
- //单个字符读取,当temp不等于-1的时候打印字符
- /*int temp = 0;
- while ((temp = r.read()) != -1) {
- System.out.print((char)temp);
- }
- */
- //方式四:读入到字符数组
- /*
- char[] buf = new char[1024];
- int temp = r.read(buf);
- //将数组转化为字符串打印,后面参数的意思是
- //如果字符数组未满,转化成字符串打印后尾部也许会出现其他字符
- //因此,读取的字符有多少个,就转化多少为字符串
- System.out.println(new String(buf,0,temp));
- */
- //方式五:读入到字符数组的优化
- //由于有时候文件太大,无法确定需要定义的数组大小
- //因此一般定义数组长度为1024,采用循环的方式读入
- /*
- char[] buf = new char[1024];
- int temp = 0;
- while((temp = r.read(buf)) != -1) {
- System.out.print(new String(buf,0,temp));
- }
- */
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if(r != null) {
- try {
- r.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String doc = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- String copy = File.separator + "home" + File.separator + "siu" +
- File.separator + "life" + File.separator + "lrc.txt";
- FileReader r = null;
- FileWriter w = null;
- try {
- r = new FileReader(doc);
- w = new FileWriter(copy);
- //方式一:单个字符写入
- int temp = 0;
- while((temp = r.read()) != -1) {
- w.write(temp);
- }
- //方式二:字符数组方式写入
- /*
- char[] buf = new char[1024];
- int temp = 0;
- while ((temp = r.read(buf)) != -1) {
- w.write(new String(buf,0,temp));
- }
- */
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //分别判断是否空指针引用,然后关闭流
- if(r != null) {
- try {
- r.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(w != null) {
- try {
- w.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- public class Demo {
- public static void main(String[] args ) {
- String doc = File.separator + "home" + File.separator + "siu" +
- File.separator + "work" + File.separator + "demo.txt";
- String copy = File.separator + "home" + File.separator + "siu" +
- File.separator + "life" + File.separator + "lrc.txt";
- FileReader r = null;
- FileWriter w = null;
- //创建缓冲区的引用
- BufferedReader br = null;
- BufferedWriter bw = null;
- try {
- r = new FileReader(doc);
- w = new FileWriter(copy);
- //创建缓冲区对象
- //将需要提高效率的FileReader和FileWriter对象放入其构造函数内
- //当然,也可以使用匿名对象的方式 br = new BufferedReader(new FileReader(doc));
- br = new BufferedReader(r);
- bw = new BufferedWriter(w);
- String line = null;
- //读取行,直到返回null
- //readLine()方法只返回换行符之前的数据
- while((line = br.readLine()) != null) {
- //使用BufferWriter对象的写入方法
- bw.write(line);
- //写完文件内容之后换行
- //newLine()方法依据平台而定
- //windows下的换行是\r\n
- //Linux下则是\n
- bw.newLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //此处不再需要捕捉FileReader和FileWriter对象的异常
- //关闭缓冲区就是关闭缓冲区中的流对象
- if(br != null) {
- try {
- r.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if(bw != null) {
- try {
- bw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
转载于:https://blog.51cto.com/wxweven520/1097203
java中的IO操作总结(一)相关推荐
- java中的IO操作总结
java中的IO操作 在java中IO涉及的范围比较大,本文主要针对文件内容的读写 对于文件内容的操作主要分为两大类: 字符流:有两个抽象类 writer Reader 其对应子类FileWriter ...
- java中的IO操作之File类
Java的集合框架: 类和接口存在于java.util包中. Java的IO: 类和接口存在于java.io包中. 学习方法: 文档在手,天下我有! --------- ...
- Java中的IO技术使用总结
请尊重他人劳动成果,请勿随意剽窃,转载请注明,谢谢!转载请注明出处:http://blog.csdn.net/evan_man/article/details/51983126 Ja ...
- java中的IO详解(上)
Java中的IO整理完整版(一) [案例1]创建一个新文件 1 import java.io.*; 2 class hello{ 3 public static void main(Strin ...
- Java NIO学习系列六:Java中的IO模型
前文中我们总结了linux系统中的5中IO模型,并且着重介绍了其中的4种IO模型: 阻塞I/O(blocking IO) 非阻塞I/O(nonblocking IO) I/O多路复用(IO multi ...
- Java中的IO流(六)
上一篇<Java中的IO流(五)>把流中的打印流PrintStream,PrintWriter,序列流SequenceInputStream以及结合之前所记录的知识点完成了文件的切割与文件 ...
- 深入理解Java中的IO
本文转载自:深入理解Java中的IO 为了方便理解与阐述,先引入两张图 在整个Java.io包中最重要的就是5个类和一个接口. 5个类指的是File.OutputStream.InputStream. ...
- java io类型_Java NIO之Java中的IO分类
前言 前面两篇文章(Java NIO之理解I/O模型(一).Java NIO之理解I/O模型(二))介绍了,IO的机制,以及几种IO模型的内容,还有涉及到的设计模式.这次要写一些更贴近实际一些的内容了 ...
- 猿创征文|Java中的IO流大家族 (两万字详解)
目录 IO流 概述 分类 四大家族 需要掌握的16个流 文件专属 FileInputstream(读 字节) FileInputStream类的其他常用方法 FileOutputStream (写 字 ...
最新文章
- php参数一个字符串数组,PHP字符串控制台参数到数组
- 《Hadoop权威指南》第二章 关于MapReduce
- esp8266 擦拭_【一起玩esp8266】flash的擦除方法——专治疑难杂症
- 随时随地办公新常态 还需安全来相伴
- javascript设计模式之发布订阅模式
- 系统架构设计-项目管理
- Python+旧衣物捐赠系统 毕业设计-附源码290942
- pdf转换html器 免费版,pdf转换成html转换器
- 什么是用户代理样式表
- openwrt 使用ebtables限制设备访问外网或内网
- 电脑 及 电脑应用软件背景色、字体的设置(护眼色)
- 游戏辅助制作核心--植物大战僵尸逆向之阳光修改(一)
- 2016SDAU编程练习二1009
- 2022CCPC桂林站感想与反思
- python程序run不了_无法通过计划程序正确运行Python脚本
- 路缘石成型机的安装使用和施工
- 【数据分析】数据分析方法论
- python爬取文字和图片_Python学习第七天之爬虫的学习与使用(爬取文字、图片、 视频)...
- Google大数据三大论文-MapReduce--中文翻译
- java同步和异步机制_JAVA学习过程中的知识——java多线程的同步和异步
热门文章
- mysql 数据库快照迁移_快照方式备份mysql 数据库
- 入门月薪8k,3年经验年薪30w,大数据薪酬待遇竟这么高?
- python垂直输出hello_python - 我如何获得直方图以垂直输出数据? - SO中文参考 - www.soinside.com...
- mysql+dump+选项_mysqldump 备份常用选项以及备份脚本
- 渗透测试是否需要学习Linux
- 惠斯通电桥信号调理芯片_用惠斯通电桥测电阻
- 图论 + 数论 ---- CF1325E E. Ehab‘s REAL Number Theory Problem (约数个数 + 枚举 + bfs找最小环)[从图结构优化搜索]
- codeforces1700数学:E2. Close Tuples (hard version)[组合计数 逆向统计] D. Circle Game[对称博弈考虑对称状态的胜负]
- 2019秦皇岛ccpc A题:Angle Beats[计算几何:统计符合直角三角形的个数]+[向量hash+3hash]
- appium java类库下载,appium 简明教程 (4)——appium client 的安装