文件内容复制
来源:互联网 发布:淘宝禁售兴奋剂 编辑:程序博客网 时间:2024/05/21 15:48
package FileIO;
import java.io.*;
import java.util.*;
public class CopyFileDemo {
public static void main(String[] args) {
File source=new File("demo.txt");
File objfile=new File("objfile.txt");
int b;
Scanner scan=new Scanner(System.in);
System.out.println("选择读取方式(字符数组读取为0,字符读取为1:)");
b=scan.nextInt();
if(b==1){
Copy1(source,objfile);
}
else{
Copy2(source,objfile);
}
}
//读一个字符复制一个
public static void Copy1(File source,File objfile){
FileReader fr = null;
FileWriter fw = null;
try {
// 声明读,写流对象
fr = new FileReader(source);
fw = new FileWriter(objfile,true);
int ch = 0;
int count=0;
// 开始读
while ((ch = fr.read()) != -1) {
count++;
System.out.println("正在读取"+count+"行");
fw.write(ch);
fw.write("\r\n"); //换行
System.out.println("正在写入"+count+"行");
}
System.out.println("读写完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//数组方式读取复制
public static void Copy2(File source,File objfile){
FileReader fr=null;
FileWriter fw=null;
try{
//声明读,写流对象
fr=new FileReader(source);
fw=new FileWriter(objfile,true);
int ch=0;
int count=0;
char[]buf=new char[1024];
//开始读
while((ch=fr.read(buf))!=-1){
count++;
System.out.println("正在读取"+count+"行");
fw.write(buf,0,ch);
fw.write("\r\n");//换行
System.out.println("正在写入"+count+"行");
}
System.out.println("读写完成!");
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(fw!=null){
try{
fw.close();
}catch(IOException e){
e.printStackTrace();
}
if(fr!=null){
try{
fr.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
}
import java.io.*;
import java.util.*;
public class CopyFileDemo {
public static void main(String[] args) {
File source=new File("demo.txt");
File objfile=new File("objfile.txt");
int b;
Scanner scan=new Scanner(System.in);
System.out.println("选择读取方式(字符数组读取为0,字符读取为1:)");
b=scan.nextInt();
if(b==1){
Copy1(source,objfile);
}
else{
Copy2(source,objfile);
}
}
//读一个字符复制一个
public static void Copy1(File source,File objfile){
FileReader fr = null;
FileWriter fw = null;
try {
// 声明读,写流对象
fr = new FileReader(source);
fw = new FileWriter(objfile,true);
int ch = 0;
int count=0;
// 开始读
while ((ch = fr.read()) != -1) {
count++;
System.out.println("正在读取"+count+"行");
fw.write(ch);
fw.write("\r\n"); //换行
System.out.println("正在写入"+count+"行");
}
System.out.println("读写完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//数组方式读取复制
public static void Copy2(File source,File objfile){
FileReader fr=null;
FileWriter fw=null;
try{
//声明读,写流对象
fr=new FileReader(source);
fw=new FileWriter(objfile,true);
int ch=0;
int count=0;
char[]buf=new char[1024];
//开始读
while((ch=fr.read(buf))!=-1){
count++;
System.out.println("正在读取"+count+"行");
fw.write(buf,0,ch);
fw.write("\r\n");//换行
System.out.println("正在写入"+count+"行");
}
System.out.println("读写完成!");
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(fw!=null){
try{
fw.close();
}catch(IOException e){
e.printStackTrace();
}
if(fr!=null){
try{
fr.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}
}
1 0
- 复制更改文件内容
- vim复制文件内容
- 复制txt文件内容
- 文件内容复制
- linux 文件内容的复制
- 复制文件中的内容到另一个文件
- 文件内容复制,生成新的文件
- java IO-文件流-文件内容复制
- IO流读取文件内容并复制
- linux 编程之文件内容复制
- // 复制源文件内容到目标文件
- 字符流的形式复制文件内容
- 复制控制台内容到TXT文件
- vi复制全部文件内容拷贝
- linux文件内容的复制与粘贴
- vim两个文件之间内容复制粘贴
- 将文件内容先替换后复制
- Python——复制文件内容
- vertical-align用法 垂直居中
- Linux常用命令
- CentOS 7开机自启动脚本
- Java基础学习之jar打包
- CodeForces
- 文件内容复制
- String类型数组与Integer类型数组之间转化
- 深入分析java线程池的实现原理
- 详解交换机端口级联连接方式
- caffe,用VS2013生成解决方案error : CommonSettings.props not found!
- 【Java每日一题】20170323
- Centos7设置静态IP地址
- 【面试题】剑指Offer-25-二叉树中和为某一路径的值
- Android常用的编译命令【转】