Java 把一个文本文档的内容复制到另一个文本文档
src.txt放在工程目录下,dest.txt可创建,也可不创建。一旦运行程序,如果dest.txt不存在,将自行创建这个文本文档,再将src.txt中的内容复制到dest.txt

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class IOTest04 { public static void main(String[] args) {
copy("src.txt", "dest.txt");
} public static void copy(String srcPath, String destPath) {
File src = new File(srcPath);
File dest = new File(destPath);
InputStream is = null;
OutputStream os = null; try {
is = new FileInputStream(src);
os = new FileOutputStream(dest, false); byte[] buffer = new byte[1024 * 1]; // 1k bytes
int length = -1;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
os.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
System.out.println("OutputStream Closed.");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
System.out.println("InputStream Closed.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
---- ---- ---- ---- ---- ---- ---- ----
读取文本文档A中的内容,先进行“加密”,将加密的内容写入到另一个文本文档B;完成加密和写入之后,再将文本文档B中(已加密)的内容复制、并覆盖文本文档A中原本的内容,删除文本文档B。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream; public class Encrypt { public static void main(String[] args) {
encrypt("src.txt", 1);
} public static void encrypt(String srcPath, int password) {
File src = new File(srcPath);
File dest = new File(src.getParent(), ("temp" + src.getName()));
InputStream is = null;
OutputStream os = null; try {
is = new FileInputStream(src);
os = new FileOutputStream(dest); byte[] buffer = new byte[1024 * 1]; // 1k bytes
int length = -1;
while ((length = is.read(buffer)) != -1) {
for (int i = 0; i < length; ++i) {
buffer[i] = (byte) (buffer[i] + password);
}
os.write(buffer, 0, length);
os.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} try {
is = new FileInputStream(dest);
os = new FileOutputStream(src); byte[] buffer = new byte[1024 * 1]; // 1k bytes
int length = -1;
while ((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
os.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
} dest.delete();
}
}
文本文档A中的内容:

对每一个字符执行+1(实际就是将字符所对应的编码进行+1)的操作,之后变成:

---- ---- ---- ---- ---- ---- ---- ----
分解一
16 File src = new File(srcPath);
17 File dest = new File(src.getParent(), ("temp" + src.getName()));
getParent() - 返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。
getName() - 返回由此抽象路径名表示的文件或目录的名称。
绝对路径名的定义与系统有关。在 UNIX 系统上,如果路径名的前缀是 "/",那么该路径名是绝对路径名。在 Microsoft Windows 系统上,如果路径名的前缀是后跟 "\\" 的盘符,或者是 "\\\\",那么该路径名是绝对路径名。
import java.io.File;
public class IO_Test01 {
public static void main(String[] args) {
File src = null;
File dest = null;
// Relative path
src = new File("src.txt");
dest = new File(src.getParent(), ("temp" + src.getName()));
System.out.println("Is absolute path: " + src.isAbsolute());
System.out.println("src's parent: " + src.getParent());
System.out.println("src's name: " + src.getName());
System.out.println("dest's parent: " + dest.getParent());
System.out.println("dest's name: " + dest.getName());
System.out.println("--------");
// Absolute path
src = new File("E:/Java/workspace/IO_Study02/src.txt");
dest = new File(src.getParent(), ("temp" + src.getName()));
System.out.println("Is absolute path: " + src.isAbsolute());
System.out.println("src's parent: " + src.getParent());
System.out.println("src's name: " + src.getName());
System.out.println("dest's parent: " + dest.getParent());
System.out.println("dest's name: " + dest.getName());
}
}
输出结果:
Is absolute path: false
src's parent: null
src's name: src.txt
dest's parent: null
dest's name: tempsrc.txt
--------
Is absolute path: true
src's parent: E:\Java\workspace\IO_Study02
src's name: src.txt
dest's parent: E:\Java\workspace\IO_Study02
dest's name: tempsrc.txt
---- ---- ---- ---- ---- ---- ---- ----
分解二
25 byte[] buffer = new byte[1024 * 1]; // 1k bytes
26 int length = -1;
27 while ((length = is.read(buffer)) != -1) {
28 for (int i = 0; i < length; ++i) {
29 buffer[i] = (byte) (buffer[i] + password);
30 }
31 os.write(buffer, 0, length);
32 os.flush();
33 }
其中的28~30行很好理解,这里不解释。
关键在于方法read(byte[] b)、write(byte[] b, int off, int len)。
public int read(byte[] b) - 从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。在某些输入可用之前,此方法将阻塞。返回:读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; public class IO_Test01 { public static void main(String[] args) {
File src = new File("E:/Java/workspace/IO_Study02/src.txt");
InputStream is = null; try {
is = new FileInputStream(src); byte[] buffer = new byte[1024 * 1]; // 1k bytes
int length = -1;
int times = 0;
while ((length = is.read(buffer)) != -1) {
++times;
System.out.println("length: " + length);
System.out.println("times: " + times);
}
System.out.println("length: " + length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果:
length: 47
times: 1
length: -1
length = 47,代表从src.txt中读取到的字节数,也就是有read()返回的。
times = 1,表示while()循环只执行了一次。
length = -1,是while()执行第二次判断,因为src.txt中的内容已经读取完毕了,所以read()返回-1。
public void write(byte[] b, int off, int len) - 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流。参数:b - 数据。 off - 数据中的起始偏移量。 len - 要写入的字节数。
---- ---- ---- ---- ---- ---- ---- ----
分解三
功力不够深厚,理解不了,写不出!!!
---- ---- ---- ---- ---- ---- ---- ----
分解X
86 dest.delete();
public boolean delete() - 删除此抽象路径名表示的文件或目录。如果此路径名表示一个目录,则该目录必须为空才能删除。返回: 当且仅当成功删除文件或目录时,返回 true;否则返回 false
---- ---- ---- ---- ---- ---- ---- ----
总结:待完善、纠错。
Java 把一个文本文档的内容复制到另一个文本文档的更多相关文章
- java把一个文件的内容复制到另外一个文件
/** * java把一个文件的内容复制到另外一个文件 */import java.io.File;import java.io.FileInputStream;import java.io.File ...
- Path,Files巩固,题目:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中
这个题目用传统的File,InputStream可以做,但是如果用Files,Path类做,虽然思路上会困难一些,但是代码简洁了很多,以下是代码: import java.io.IOException ...
- ZeroMQ接口函数之 :zmq_msg_copy - 把一个消息的内容复制到另一个消息中
ZeroMQ 官方地址 :http://api.zeromq.org/4-1:zmq_msg_copy zmq_msg_copy(3) ØMQ Manual - ØMQ/3.2.5 Name zm ...
- 下拉框——把一个select框中选中内容移到另一个select框中遇到的问题
在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来 ...
- C++将一个vector中的内容复制到另一个vector结尾
在使用vector容器的时候,需要将一个vector中的内容复制到另一个vector结尾,如何实现呢? 使用vector的insert方法 template <class InputIterat ...
- Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹
Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹 1.将一个文件夹下的所有内容复制到另一个文件夹下 cp -r /home/packageA/* /home/cp/packageB ...
- C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)
C# 把一个文件夹下所有文件复制到另一个文件夹下 public static void CopyDirectory(string srcPath, string destPath) { try { ...
- Excel VBA 从一个工作簿查找另一个一个工作簿中的一些内容复制到另外一个工作簿
帮朋友来写个Excel VBA 以前写过ASP,所以对vb略微熟悉,但VBA 没有仔细研究过. 以前只研究过 vba 写一个 计算个人所得税的程序. 这次写的功能也算是简单,但也耗费了两天的功夫. 需 ...
- php学习笔记:读取文档的内容,利用php修改文档内容
直接上代码 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/9/10 0010 * Time: 20:27 ...
随机推荐
- Javascript入门(二)变量、获取元素、操作元素
一.变量 Javascript 有五种基本数据类型 number.String.boolean.undefined.null 一种复合类型:object 二.使用getElementById方法获取元 ...
- 已安装nginx支持https配置 the "ssl" parameter requires ngx_http_ssl_module
原文链接:https://blog.seosiwei.com/detail/28 nginx已安装,ssl模块未安装的解决方法: 如果需要在linux中编译自己的nginx服务器,请参照:https: ...
- OGG初始加载过程概述
您可以使用Oracle GoldenGate来: 执行独立的批量加载以填充数据库表以进行迁移或其他用途. 将数据作为初始同步运行的一部分加载到数据库表中,以准备与Oracle GoldenGate进行 ...
- Python3学习笔记32-xlwt模块
xlwt模块是用来写入excel的第三方模块,需要下载安装后才能使用. 设置字体样式 import xlwt #初始化一个excel excel = xlwt.Workbook(encoding='u ...
- ubuntu16.04彻底删除nginx+php
1.1 删除nginx,–purge包括配置文件 sudo apt-get --purge remove nginx 1.2 自动移除全部不使用的软件包 sudo apt-get autoremove ...
- bzoj 1175: The stairways of Saharna
一道杨氏矩阵的题,萌新初入门,还不是很懂,这篇 blog 讲的超级好(就是看图有点麻烦) 据说这玩意儿可以代替堆和平衡树用,支持插入.删除.查询,跑得还挺快的(慢着,复杂度好像是 n^2 ? 而且空间 ...
- 012_k8s专题系列一之进入容器日常op
一.下面列出如何进入正在运行的k8s容器 <1> kubectl get pods #查看所有正在运行的pod NAME READY STATUS RESTARTS AGE nginx-5 ...
- centos7.4_x86_64安装grafana5.2.1并安装常用zabbix插件
获取并安装grafana5.2.1# wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-5.2.1-1. ...
- 【原创】Linux基础之windows linux双系统
1 下载iso opensuse 下载: http://download.opensuse.org/distribution/openSUSE-stable/iso/openSUSE-Leap-15. ...
- Flume集群搭建
0. 软件版本下载 http://mirror.bit.edu.cn/apache/flume/ 1. 集群环境 Master 172.16.11.97 Slave1 172.16.11.98 S ...