java——IO流01
2024-08-26 00:32:25
移动文件有一种简单方法,不需要复制文件再删除文件。
package com.unir.test01; import java.io.File;
import java.io.IOException; public class Test02 { public static void main(String[] args) throws IOException {
//创建文件
File f=new File("d:\\developer\\6.txt");
f.renameTo(new File("E:\\英雄时刻"));//移动到e盘
f.delete();//删除
} }
使用FileInputStream类的read(byte[])方法和FileOutputStream类的write(byte[])方法实现文件移动。
已知文件:d:\\developer\\56.txt
目标地址:e:\\英雄时刻\\56.txt
package com.unir.test01; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class Test01 { /* 已知文件:d:/developer/12345.txt
目标地址:e:/英雄时刻/12345.txt
*/ public static void main(String[] args) throws IOException {
//创建文件
File f=new File("d:\\developer\\12345.txt");
//创建字符
String txt="nice.";
byte[] b=txt.getBytes();
FileOutputStream fos=new FileOutputStream(f);
fos.write(b); //创建输入流
FileInputStream input = new FileInputStream("d:/developer/12345.txt");
//创建输出流
FileOutputStream output = new FileOutputStream("e:/英雄时刻/12345.txt");
int len = 0;
byte[] buf = new byte[1024];
if((len = input.read(buf)) > -1){
output.write(buf, 0 , len);
} fos.close();
input.close();
output.close();
f.delete(); } }
使用BufferedInputStream类的read方法和BufferedOutputStream类的write方法实现文件移动。
已知文件:d:\\developer\\56.txt
目标地址:e:\\英雄时刻\\56.txt
package com.unir.test01; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; public class Test04 {
public static void main(String[] args) throws IOException {
FileInputStream f=new FileInputStream("D:\\developer\\9.txt");
InputStreamReader i=new InputStreamReader(f,"utf-8");
FileOutputStream f1=new FileOutputStream("e:\\英雄时刻\\9.txt",true);
BufferedOutputStream b=new BufferedOutputStream(f1); int len = 0;
byte[] buf = new byte[1024];
if((len = i.read()) != -1){
b.write(buf, 0 , len);
}
b.close();
i.close();
File file=new File("D:\\developer\\9.txt");
file.delete();
} }
从键盘读入“Java IO流的分类”,并将这些文字写入文件d:\\developer\\9.txt,然后将该文件复制到e:\\英雄时刻\\9.txt
package com.unir.test01; import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader; public class Test04 {
public static void main(String[] args) throws IOException {
FileInputStream f=new FileInputStream("D:\\developer\\9.txt");
InputStreamReader i=new InputStreamReader(f,"utf-8");
FileOutputStream f1=new FileOutputStream("e:\\英雄时刻\\9.txt",true);
BufferedOutputStream b=new BufferedOutputStream(f1); int len = 0;
byte[] buf = new byte[1024];
if((len = i.read()) != -1){
b.write(buf, 0 , len);
}
b.close();
i.close();
File file=new File("D:\\developer\\9.txt");
file.delete();
} }
中。
最新文章
- Splash页面跳转主页面,去掉主页面标题栏
- iOS打包ipa给客户测试流程
- JBoss AS
- 【温故而知新-Javascript】使用数组
- grunt安装_
- SPI的通信试验 --verilog (从机-全双工)
- SparseArray,dip &; px
- iOS预处理指令
- HTML5部分元素
- HighCharts之2D堆条状图
- JCE安装使用报错
- 启动期间的内存管理之pagging_init初始化分页机制--Linux内存管理(十四)
- 使用mbedtls的使用说明和AES加密方法(原来的PolarSSL)
- 基于timestamp和nonce的防重放攻击
- css设置文字不能选中状态
- ASP.NET MVC5+EF6+LayUI实战教程,通用后台管理系统框架(2)
- C语言字节对齐 __align(),__attribute((aligned (n))),#pragma pack(n)【转】
- 一个简单的Linux后门程序的实现
- vscode 常用配置
- Leetcode题库——25.k个一组翻转链表