java 打印流与commons-IO
2024-09-08 15:22:48
一 打印流
1.打印流的概述
打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式.
打印流根据流的分类:
字节打印流 PrintStream
字符打印流 PrintWriter
方法:
void print(String str): 输出任意类型的数据,
void println(String str): 输出任意类型的数据,自动写入换行操作
代码演示:
/*
* 需求:把指定的数据,写入到printFile.txt文件中
*
* 分析:
* 1,创建流
* 2,写数据
* 3,关闭流
*/
public class PrintWriterDemo {
public static void main(String[] args) throws IOException {
//创建流
//PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"));
PrintWriter out = new PrintWriter("printFile.txt");
//2,写数据
for (int i=0; i<5; i++) {
out.println("helloWorld");
}
//3,关闭流
out.close();
}
}
2.打印流完成数据自动刷新
可以通过构造方法,完成文件数据的自动刷新功能
构造方法:
开启文件自动刷新写入功能
public PrintWriter(OutputStream out, boolean autoFlush)
public PrintWriter(Writer out, boolean autoFlush)
代码演示:
/*
* 分析:
* 1,创建流
* 2,写数据
*/
public class PrintWriterDemo2 {
public static void main(String[] args) throws IOException {
//创建流
PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"), true);
//2,写数据
for (int i=0; i<5; i++) {
out.println("helloWorld");
}
//3,关闭流
out.close();
}
}
二 commons-IO
1.FileUtils
提供文件操作(移动文件,读取文件,检查文件是否存在等等)的方法。
常用方法:
readFileToString(File file):读取文件内容,并返回一个String;
writeStringToFile(File file,String content):将内容content写入到file中;
copyDirectoryToDirectory(File srcDir,File destDir);文件夹复制
copyFile(File srcFile,File destFile);文件复制
代码演示:
/*
* 完成文件的复制
*/
public class CommonsIODemo01 {
public static void main(String[] args) throws IOException {
//method1("D:\\test.avi", "D:\\copy.avi"); //通过Commons-IO完成了文件复制的功能
FileUtils.copyFile(new File("D:\\test.avi"), new File("D:\\copy.avi"));
} //文件的复制
private static void method1(String src, String dest) throws IOException {
//1,指定数据源
BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
//2,指定目的地
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
//3,读
byte[] buffer = new byte[1024];
int len = -1;
while ( (len = in.read(buffer)) != -1) {
//4,写
out.write(buffer, 0, len);
}
//5,关闭流
in.close();
out.close();
}
} /*
* 完成文件、文件夹的复制
*/
public class CommonsIODemo02 {
public static void main(String[] args) throws IOException {
//通过Commons-IO完成了文件复制的功能
FileUtils.copyFile(new File("D:\\test.avi"), new File("D:\\copy.avi")); //通过Commons-IO完成了文件夹复制的功能
//D:\基础班 复制到 C:\\abc文件夹下
FileUtils.copyDirectoryToDirectory(new File("D:\\基础班"), new File("C:\\abc"));
}
}
最新文章
- C#获取执行存储过程的"; 返回值";代码
- Android -- 自定义权限
- 向列布局动态添加F7
- JavaScript获取onclick、onchange等事件值的代码
- Java:集合工具类-Collections
- ${param.origin}
- 关于iPhone多点触控
- USACO Section 2.2: Party Lamps
- poj-2376 Cleaning Shifts (排序+贪心)
- java面试题—精选30道Java笔试题解答(一)
- [LeetCode] 1-bit and 2-bit Characters 一位和两位字符
- sublime 基本的配置
- java.lang.Boolean 类源码解析
- VS code golang 开发环境搭建
- 自定义你的 Confluence 6 站点
- python传参是传值还是传引用
- C#中删除集合中符合条件的元素以及需注意属相
- Yii1使用Gii生成模块实现CURD
- ios中NSObject分类
- 20170711xlVBA批量制图一例
热门文章
- Python2爬取学生名单
- .NET Core微服务开发服务间调用篇-GRPC
- 导出数据到Excel的时候报JAVA.LANG.NOSUCHMETHODERROR: ORG.APACHE.POI.SS.USERMODEL.CELLSTYLE.SETVERTICALALIGNMENT(LORG/APACHE/POI/SS/USERMODEL/VERTICALALIGNMENT;)V
- p44_IP数据包格式
- Python虚拟环境(virtualenv)
- 占个坑 未来学qt的时候专用
- Mybatis——Mapper解析
- element-ui的el-progress组件增加修改status状态
- # SpringBoot-环境搭建
- three.js 制作魔方