IO流----操作文件的9种方法代码实现
IO流----操作文件的9种方法代码实现:
1:使用字节流读写数据:
四种方式:
method1: 每次读写一个字节,边读边写:
/*
* 复制文本文件。
*
* 数据源:从哪里来
* a.txt -- 读取数据 -- FileInputStream
*
* 目的地:到哪里去
* b.txt -- 写数据 -- FileOutputStream
*
* java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)
*
*边读边写:
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("a.txt");
// 封装目的地
FileOutputStream fos = new FileOutputStream("b.txt"); int by = 0;
while ((by = fis.read()) != -1) {
fos.write(by);
} // 释放资源(先关谁都行)
fos.close();
fis.close();
}
}
method2:每次读写一个字节数组:
/*
* 需求:把c:\\a.txt内容复制到d:\\b.txt中
*
* 数据源:
* c:\\a.txt -- 读取数据 -- FileInputStream
* 目的地:
* d:\\b.txt -- 写出数据 -- FileOutputStream
*/
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("c:\\a.txt");
FileOutputStream fos = new FileOutputStream("d:\\b.txt"); // 复制数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
//0:从0角标开始读取,len:每次读取的长度:
fos.write(bys, 0, len);
} // 释放资源
fos.close();
fis.close();
}
}
method3,method4:使用缓冲区高效率读写:
读取:
/*
* 注意:虽然我们有两种方式可以读取,但是,请注意,这两种方式针对同一个对象在一个代码中只能使用一个。
*/
public class BufferedInputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"bos.txt"));
//一次读取一个字节:
// 读取数据
// int by = 0;
// while ((by = bis.read()) != -1) {
// System.out.print((char) by);
// }
// System.out.println("---------");
//一次读取一个字节数组:
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
System.out.print(new String(bys, 0, len));
} // 释放资源
bis.close();
}
}
写数据:
/
* 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
* 原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。
*/
public class BufferedOutputStreamDemo {
public static void main(String[] args) throws IOException {
// BufferedOutputStream(OutputStream out)
// FileOutputStream fos = new FileOutputStream("bos.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fos);
// 简单写法
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("bos.txt")); // 写数据
bos.write("hello".getBytes()); // 释放资源
bos.close();
}
}
2:使用字符流读写数据:
5种方式:
method1、method2 :一次读写一个字符,一次读写一个字符数组:
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
// 封装数据源
FileReader fr = new FileReader("OnlyFileTest/Demo2.txt");
// 封装目的地
FileWriter fw = new FileWriter("a.txt");
// 读写数据
// 方式1 :一次读取一个字符:
// int ch = 0;
// while ((ch = fr.read()) != -1) {
// fw.write(ch);
// } // 方式2 一次读取一个字符数组:
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
//刷新缓冲区:
// fw.flush();
}
// 释放资源
fr.close();
fw.close();
}
}
method3、method4 :使用缓冲区高效读写:
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader fr = new BufferedReader(new FileReader("OnlyFileTest/Demo2.txt"));
// 封装目的地
BufferedWriter fw = new BufferedWriter(new FileWriter("a.txt"));
// 读写数据
// 方式3
// int ch = 0;
// while ((ch = fr.read()) != -1) {
// fw.write(ch);
// } // 方式4
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
//刷新缓冲区:
fw.flush();
}
// 释放资源
fr.close();
fw.close();
}
method5:一次读写一行:
/*
* 需求:把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中
*
* 数据源:
* a.txt -- 读取数据 -- 字符转换流 -- InputStreamReader -- FileReader -- BufferedReader
* 目的地:
* b.txt -- 写出数据 -- 字符转换流 -- OutputStreamWriter -- FileWriter -- BufferedWriter
*/
public class CopyFileDemo2 {
public static void main(String[] args) throws IOException {
// 封装数据源
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
// 封装目的地
BufferedWriter bw = new BufferedWriter(new FileWriter("b.txt")); // 读写数据
String line = null;
while ((line = br.readLine()) != null) {
//写入一行数据到“b.txt”中:
bw.write(line);
//换行写入另一行:
bw.newLine();
//刷新缓冲区:
bw.flush();
} // 释放资源
bw.close();
br.close();
}
}
注意:如果使用的相对路径(a.txt),创建的文件或文件夹默认在当前项目下面,使用绝对路径(“E:\\program\\file”)才可以指定到目录下;
3:打印流:
@Test
public void test() throws IOException {
//字符输入流读取文件:
BufferedReader br = new BufferedReader(new FileReader("OnlyFileTest/Demo2.txt"));
//打印流对象:
PrintWriter pw = new PrintWriter(new FileWriter("OnlyFileTest/Demo3.txt"),true);
String line = null;
while ((line=br.readLine())!=null){
pw.println(line);
}
}
文件覆盖问题解决:
注意:一般我们在使用输出流(FileOutputStream)的时候会覆盖掉文件里面原先以存在的数据:
解决:
IO流----操作文件的9种方法代码实现的更多相关文章
- .net(C#)操作文件的几种方法汇总
.net(C#)操作文件的几种方法汇总 System.IO命名空间下类的用法:在System.IO名称空间中包含了用于文件输入输出的主要类.File:实用类,提供许多静态方法,用于移动.复制和删除文件 ...
- golang操作文件的四种方法
golang追加内容到文件末尾 字数349 阅读54 评论0 喜欢2 golang读写文件,网上很多教程了但是今天有个需求,想要把内容追加写到文件末尾google了好久,没有查到研究了一会儿file库 ...
- android IO流操作文件(存储和读取)
存储文件: public class FileOperate extends Activity { private static final String FILENAME = "mydat ...
- java IO流 对文件操作的代码集合
Io流 按照分类 有两种分类 流向方向: 有输入流和输出流 按照操作类型有:字节流和字符流 按照流向方向 字节流的一些操作 //读文件 FileInputStream fis = new FileIn ...
- java io流 对文件夹的操作
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
- Java基础 IO流的文件和目录的五类主要操作
笔记: /** IO流的 文件和目录的操作 * 1.路径需要 需要两个反斜杠 或者一个单斜杠! * 绝对路径:包括盘符在内的完整的路径名! * 相对路径:在当前目录文件下的路径! * 2.File 是 ...
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- Java基础教程:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
随机推荐
- 微信开发使用webstorm&phpstrom 配置
材料: 下载链接:https://pan.baidu.com/s/1pLn6jFl 密码:fgo5 -----(将其中的wecharCode.jar 下载下来,然后在webStorm 的 File ...
- 05 Zabbix4.0触发器表达式Trigger expression支持的函数
点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 05 Zabbix4.0触发器表达式Trigger expression支持的函数 所有函数返回值 ...
- 【Luogu2664】树上游戏(点分治)
[Luogu2664]树上游戏(点分治) 题面 洛谷 题解 很好的一道点分治题. 首先直接点分治,考虑过每个分治重心的链的贡献. 我们从分治重心开始找每种颜色,强制令一种颜色只在其到分治重心的链上第一 ...
- photoshop学习4
蒙版 路径学习 一.蒙版 蒙版可以理解为一层在图层上的遮挡布,为什么要将图层遮住呢,有什么好处.好处在于容易编辑. 在一个图层上建立一个蒙版之后,可以用再删掉不需要的部分,从而露出原图层的部分.那么这 ...
- bzoj3514(LCT+主席树)
题目描述 N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. 题解 对于一个截止时间来说,越晚的变越好. 所以我们可以维护一颗以边的序号为关键字的最大生成树,然后用主席树维 ...
- hasattr
语法: hasattr(object,name) 检测类对象是否具有该name属性或方法
- selenium js
这几天的任务量比较大,还有一个挺棘手的网站cfda,不巧的是数据量还挺大,40W关于企业信息.上来就是debugger pause,调试中断,开始还是挺懵逼的,但这个还算简单毕竟google,百度,就 ...
- nio 阻塞 非阻塞 同步 异步
https://mp.weixin.qq.com/s/5SKgdkC0kaHN495psLd3Tg 说在前面 上篇NIO相关基础篇二,主要介绍了文件锁.以及比较关键的Selector,本篇继续NIO相 ...
- 组件之间的数据传递--Vuex
安装Vuex: npm install Vuex -S 在main.js中引入 import Vue from 'vue' import App from './App' import Vuex fr ...
- A1046. Shortest Distance
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...