当前位置:网站首页>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();
}
}
}
}
}
边栏推荐
- SQL必需掌握的100个重要知识点:IN 操作符
- VMware vSphere esxi 7.0 installation tutorial
- Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
- 2021全球独角兽榜发布:中国301家独角兽企业全名单来了!
- Codeforces Round #723 (Div. 2)
- Here are 12 commonly used function formulas for you. All used ones are good
- Contest 2050 and Codeforces Round #718 (Div. 1 + Div. 2)
- 基于微信小程序的高校毕业论文管理系统#毕业设计
- MySQL performance optimization index function, hidden, prefix, hash index usage (2)
- Model reasoning acceleration based on tensorrt
猜你喜欢

MySQL performance optimization index function, hidden, prefix, hash index usage (2)

Industry case | see the operation of bank digital transformation from the king of retail

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

Use the storcli tool to configure raid. Just collect this article

Unity3D Button根据文本内容自适应大小

Experience Navicat premium 16, unlimited reset, 14 day trial method (with source code)

Modify large online games through CE modifier

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

squid代理服务器

Codeforces Round #723 (Div. 2)
随机推荐
Unity3D Button根据文本内容自适应大小
ABC-Teleporter Setting-(思维+最短路)
Sharing | intelligent environmental protection - ecological civilization informatization solution (PDF attached)
DO280OpenShift访问控制--security policy和章节实验
Navicat Premium连接问题--- Host ‘xxxxxxxx‘ is not allowed to connect to this MySQL server
Flutter隐藏AppBar的返回按钮
MySQL performance optimization index function, hidden, prefix, hash index usage (2)
Unity3d button adapts the size according to the text content
Focus! Tips for installing fonts on domestic computers
Original translation | comparison of machine learning model service tools: kserve, Seldon core and bentoml
This is the same as data collection. Can you define a parameter as last month or the previous day, and then use this parameter in SQL?
Model reasoning acceleration based on tensorrt
Necessary software tools in embedded software development
事件相关电位ERP的皮层溯源分析
Serveur mandataire SQUID
100 important knowledge points that SQL must master: retrieving data
MySQL client tools are recommended. I can't imagine that it is best to use Juran
TypeScript学习
A set of system to reduce 10 times the traffic pressure in crowded areas
Share how I take notes