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 把一个文本文档的内容复制到另一个文本文档的更多相关文章

  1. java把一个文件的内容复制到另外一个文件

    /** * java把一个文件的内容复制到另外一个文件 */import java.io.File;import java.io.FileInputStream;import java.io.File ...

  2. Path,Files巩固,题目:从键盘接收两个文件夹路径,把其中一个文件夹中(包含内容)拷贝到另一个文件夹中

    这个题目用传统的File,InputStream可以做,但是如果用Files,Path类做,虽然思路上会困难一些,但是代码简洁了很多,以下是代码: import java.io.IOException ...

  3. 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 ...

  4. 下拉框——把一个select框中选中内容移到另一个select框中遇到的问题

    在使用jQuery实现把一个select框中选中内容移到另一个select框中功能时遇到了一个问题,就是点击按钮时内容可以到另一个select框中,但是到了另一个select框中的内容却很快闪退回原来 ...

  5. C++将一个vector中的内容复制到另一个vector结尾

    在使用vector容器的时候,需要将一个vector中的内容复制到另一个vector结尾,如何实现呢? 使用vector的insert方法 template <class InputIterat ...

  6. Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹

    Linux将一个文件夹或文件夹下的所有内容复制到另一个文件夹     1.将一个文件夹下的所有内容复制到另一个文件夹下 cp -r /home/packageA/* /home/cp/packageB ...

  7. C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)

    C# 把一个文件夹下所有文件复制到另一个文件夹下   public static void CopyDirectory(string srcPath, string destPath) { try { ...

  8. Excel VBA 从一个工作簿查找另一个一个工作簿中的一些内容复制到另外一个工作簿

    帮朋友来写个Excel VBA 以前写过ASP,所以对vb略微熟悉,但VBA 没有仔细研究过. 以前只研究过 vba 写一个 计算个人所得税的程序. 这次写的功能也算是简单,但也耗费了两天的功夫. 需 ...

  9. php学习笔记:读取文档的内容,利用php修改文档内容

    直接上代码 <?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/9/10 0010 * Time: 20:27 ...

随机推荐

  1. CrackME 2011 # 2 逆向练习解题思路

    CrackME 2011 # 2 逆向练习解题思路 做题背景: 从朋友那里得到一道逆向题名字叫package,作为小菜的我当然要看一看啦,这名字辨识度太低我就按照运行的名字改成CrackME 2011 ...

  2. 【多视图几何】TUM 课程 第5章 双视图重建:线性方法

    课程的 YouTube 地址为:https://www.youtube.com/playlist?list=PLTBdjV_4f-EJn6udZ34tht9EVIW7lbeo4 .视频评论区可以找到课 ...

  3. 解决Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 问题

    通过这里的回答,我们可以知道: Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http头的验证. 具体来说,就是添加了些规则去限制HTTP头的规范性 参考这里 具体来说 ...

  4. Palindromic Numbers LightOJ - 1205

    题目大意: 求区间内的回文数个数 题目思路: 数位dp,先枚举前一半数字,然后填上相应的后一半数字. #include<cstdio> #include<cstring> #i ...

  5. Thymeleaf相关补充

    ⒈理解Thymeleaf Java模板引擎.能够处理HTML.XML.JavaScript.CSS甚至纯文本.类似JSP.Freemarker 自然模板.原型即页面 语法优雅易懂,OGNL.Sprin ...

  6. python 之路,Day 1 python基础 之 课后随笔

    首先是抱着被忽悠的心态,购买了老男孩的什么什么什么(你懂得!!),开始了一周一堂课时的听,然后就是做,自己的博客,首先附上整理的内容吧. 1day .... 一. Hell world 程序 在lin ...

  7. Tickets HDU - 1260 水DP

    HDU - 1260 现在有n个人要买电影票,如果知道每个人单独买票花费的时间, 还有和前一个人一起买花费的时间,问最少花多长时间可以全部买完票. 直接dp就行,注意下输出和初始化 每次从dp[i-1 ...

  8. os.date

    代码中有一段如下: local date = os.date("*t", set) if date then           luci.sys.call("date ...

  9. python操作三大主流数据库(2)python操作mysql②python对mysql进行简单的增删改查

    python操作mysql②python对mysql进行简单的增删改查 1.设计mysql的数据库和表 id:新闻的唯一标示 title:新闻的标题 content:新闻的内容 created_at: ...

  10. Light OJ 1095

    题意: 给你 N 个数, 总共有 N! 种排列, 现在 要你统计前 M 个数 刚好 有K 个数 在原来的位置上 的排列个数 思路: 首先 M 中选 K C(m,k): 则 共 剩下 n - k 个数, ...