移动文件有一种简单方法,不需要复制文件再删除文件。

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();
} }

  

中。

最新文章

  1. Splash页面跳转主页面,去掉主页面标题栏
  2. iOS打包ipa给客户测试流程
  3. JBoss AS
  4. 【温故而知新-Javascript】使用数组
  5. grunt安装_
  6. SPI的通信试验 --verilog (从机-全双工)
  7. SparseArray,dip & px
  8. iOS预处理指令
  9. HTML5部分元素
  10. HighCharts之2D堆条状图
  11. JCE安装使用报错
  12. 启动期间的内存管理之pagging_init初始化分页机制--Linux内存管理(十四)
  13. 使用mbedtls的使用说明和AES加密方法(原来的PolarSSL)
  14. 基于timestamp和nonce的防重放攻击
  15. css设置文字不能选中状态
  16. ASP.NET MVC5+EF6+LayUI实战教程,通用后台管理系统框架(2)
  17. C语言字节对齐 __align(),__attribute((aligned (n))),#pragma pack(n)【转】
  18. 一个简单的Linux后门程序的实现
  19. vscode 常用配置
  20. Leetcode题库——25.k个一组翻转链表

热门文章

  1. C++实现 电子邮件客户端程序(简易版)
  2. 结对项目——黄金分割点游戏(陈香宇&蔡春燕)
  3. Javascript中的this指向。
  4. 温度转换-java
  5. mybatis(一、原理,一对多,多对一查询)
  6. 条件分支语句(SWICH语句)
  7. cstdlib和stdlib.h区别
  8. 工作中 sql 整理(一)
  9. C++11如何减少内存拷贝次数
  10. js+css jQuery实现页面后退执行 & 遮罩弹框