当前位置:网站首页>io流代码
io流代码
2022-06-27 19:20:00 【continueLR】
目录
拷贝:使用FileInputStream + FileOutputStream完成文件的拷贝。
使用FileReader FileWriter进行拷贝的话,只能拷贝“普通文本”文件。
FileInputStrea字节输入流
package com.bjpowernode.java.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
最终版,需要掌握。
*/
public class FileInputStreamTest04 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("chapter23/src/tempfile3");
// 准备一个byte数组
byte[] bytes = new byte[4];
/*while(true){
int readCount = fis.read(bytes);
if(readCount == -1){
break;
}
// 把byte数组转换成字符串,读到多少个转换多少个。
System.out.print(new String(bytes, 0, readCount));
}*/
int readCount = 0;
while((readCount = fis.read(bytes)) != -1) {
System.out.print(new String(bytes, 0, readCount));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}FileOutputStream字节输出流
package com.bjpowernode.java.io;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 文件字节输出流,负责写。
* 从内存到硬盘。
*/
public class FileOutputStreamTest01 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
// myfile文件不存在的时候会自动新建!
// 这种方式谨慎使用,这种方式会先将原文件清空,然后重新写入。
//fos = new FileOutputStream("myfile");
//fos = new FileOutputStream("chapter23/src/tempfile3");
// 以追加的方式在文件末尾写入。不会清空原文件内容。
fos = new FileOutputStream("chapter23/src/tempfile3", true);
// 开始写。
byte[] bytes = {97, 98, 99, 100};
// 将byte数组全部写出!
fos.write(bytes); // abcd
// 将byte数组的一部分写出!
fos.write(bytes, 0, 2); // 再写出ab
// 字符串
String s = "我是一个中国人,我骄傲!!!";
// 将字符串转换成byte数组。
byte[] bs = s.getBytes();
// 写
fos.write(bs);
// 写完之后,最后一定要刷新
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
FileReader字符输入流
package com.bjpowernode.java.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/*
FileReader:
文件字符输入流,只能读取普通文本。
读取文本内容时,比较方便,快捷。
*/
public class FileReaderTest {
public static void main(String[] args) {
FileReader reader = null;
try {
// 创建文件字符输入流
reader = new FileReader("tempfile");
//准备一个char数组
char[] chars = new char[4];
// 往char数组中读
reader.read(chars); // 按照字符的方式读取:第一次e,第二次f,第三次 风....
for(char c : chars) {
System.out.println(c);
}
/*// 开始读
char[] chars = new char[4]; // 一次读取4个字符
int readCount = 0;
while((readCount = reader.read(chars)) != -1) {
System.out.print(new String(chars,0,readCount));
}*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}FileWriter字符输出流
package com.bjpowernode.java.io;
import java.io.FileWriter;
import java.io.IOException;
/*
FileWriter:
文件字符输出流。写。
只能输出普通文本。
*/
public class FileWriterTest {
public static void main(String[] args) {
FileWriter out = null;
try {
// 创建文件字符输出流对象
//out = new FileWriter("file");
out = new FileWriter("file", true);
// 开始写。
char[] chars = {'我','是','中','国','人'};
out.write(chars);
out.write(chars, 2, 3);
out.write("我是一名java软件工程师!");
// 写出一个换行符。
out.write("\n");
out.write("hello world!");
// 刷新
out.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
拷贝:使用FileInputStream + FileOutputStream完成文件的拷贝。
package com.bjpowernode.java.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
使用FileInputStream + FileOutputStream完成文件的拷贝。
拷贝的过程应该是一边读,一边写。
使用以上的字节流拷贝文件的时候,文件类型随意,万能的。什么样的文件都能拷贝。
*/
public class Copy01 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 创建一个输入流对象
fis = new FileInputStream("D:\\course\\02-JavaSE\\video\\chapter01\\动力节点-JavaSE-杜聚宾-001-文件扩展名的显示.avi");
// 创建一个输出流对象
fos = new FileOutputStream("C:\\动力节点-JavaSE-杜聚宾-001-文件扩展名的显示.avi");
// 最核心的:一边读,一边写
byte[] bytes = new byte[1024 * 1024]; // 1MB(一次最多拷贝1MB。)
int readCount = 0;
while((readCount = fis.read(bytes)) != -1) {
fos.write(bytes, 0, readCount);
}
// 刷新,输出流最后要刷新
fos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 分开try,不要一起try。
// 一起try的时候,其中一个出现异常,可能会影响到另一个流的关闭。
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}使用FileReader FileWriter进行拷贝的话,只能拷贝“普通文本”文件。
package com.bjpowernode.java.io;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/*
使用FileReader FileWriter进行拷贝的话,只能拷贝“普通文本”文件。
*/
public class Copy02 {
public static void main(String[] args) {
FileReader in = null;
FileWriter out = null;
try {
// 读
in = new FileReader("chapter23/src/com/bjpowernode/java/io/Copy02.java");
// 写
out = new FileWriter("Copy02.java");
// 一边读一边写:
char[] chars = new char[1024 * 512]; // 1MB
int readCount = 0;
while((readCount = in.read(chars)) != -1){
out.write(chars, 0, readCount);
}
// 刷新
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
边栏推荐
- 抗洪救灾,共克时艰,城联优品驰援英德捐赠爱心物资
- 100 important knowledge points that SQL must master: using functions to process data
- 大促场景下,如何做好网关高可用防护
- 非常全面的DolphinScheduler(海豚调度)安装使用文档
- SQL必需掌握的100个重要知识点:用通配符进行过滤
- 动物养殖生产虚拟仿真教学系统|华锐互动
- MySQL client tools are recommended. I can't imagine that it is best to use Juran
- 白嫖红队goby&POC,叫你如何白嫖?
- 分享一次自己定位 + 解决问题的经历
- Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
猜你喜欢

308. 2D area and retrieval - variable segment tree / hash

ICML2022 | 可扩展深度高斯马尔可夫随机场

基于微信小程序的高校毕业论文管理系统#毕业设计

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new digital engine for all elements of traffic in Shenzhen

Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)

Zhongang Mining: the largest application field of new energy or fluorite

KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架

Release of global Unicorn list in 2021: the full list of 301 Unicorn enterprises in China is coming!

众昂矿业:新能源或成萤石最大应用领域

100 important knowledge points that SQL must master: using functions to process data
随机推荐
Full record of 2022 open source moment at Huawei partners and Developers Conference
Unity3d button adapts the size according to the text content
MySQL速成——第一天--基础入门
爱数课实验 | 第九期-利用机器学习方法进行健康智能诊断
VMware vSphere ESXi 7.0安装教程
Love math experiment | Issue 8 - building of Singapore house price prediction model
爱数课实验 | 第五期-基于机器学习方法的商品评论情感判定
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
抖音的兴趣电商已经碰到流量天花板?
农产品期货怎么做怎么开户,期货开户手续费多少,找谁能优惠手续费?
Flask----应用案例
SQL必需掌握的100个重要知识点:检索数据
Codeforces Round #716 (Div. 2)
# Leetcode 821. Minimum distance of characters (simple)
OpenSSL 编程 一:基本概念
SQL必需掌握的100个重要知识点:排序检索数据
基于微信小程序的警局报案便民服务平台#毕业设计
GoLand永久激活
MYSQL 性能优化 index 函数,隐藏,前缀,hash 索引 使用方法(2)
安装gatewayworker之后启动start.php