展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Copy {
public static void main(String[] args){
//记得先把s1.txt创建在C盘
File source = new File("c:/s1.txt");
File target = new File("c:/s2.txt");
//如果s2不存在则创建s2
if(!target.exists()){
try {
target.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedReader br = new BufferedReader(new FileReader(source));
BufferedWriter bw = new BufferedWriter(new FileWriter(target));
char[] buffer = new char[128];
System.out.println("文件复制开始");
while(br.read(buffer) != -1){
bw.write(buffer);
}
bw.close();
br.close();
System.out.println("文件复制结束");
} catch (Exception e) {
e.printStackTrace();
}
}
}
追问
大哥“ BufferedReader br = new BufferedReader(new FileReader(source));"这里面的(new FileReader(source));能说一下这是什么吗
char[] buffer = new char[128];后面这个[128]是怎么算出来的呢
追答
128是自己设置的,设置为多少都行
展开全部
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
public class AA
{
public static void main(String[] args) throws IOException, Exception
{
File file = new File("src/s1.txt");
// 读取文件
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
"src/s2.txt")));
System.out.println("复制文件开始。。");
while (true)
{
String value = reader.readLine();
// 读到文件末尾
if (null == value)
break;
writer.write(value);
//换行
writer.newLine();
}
System.out.println("复制文件结束。。");
reader.close();
writer.close();
}
}
在src目录下建立s1.txt就可以了!
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URISyntaxException;
public class AA
{
public static void main(String[] args) throws IOException, Exception
{
File file = new File("src/s1.txt");
// 读取文件
BufferedReader reader = new BufferedReader(new FileReader(file));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
"src/s2.txt")));
System.out.println("复制文件开始。。");
while (true)
{
String value = reader.readLine();
// 读到文件末尾
if (null == value)
break;
writer.write(value);
//换行
writer.newLine();
}
System.out.println("复制文件结束。。");
reader.close();
writer.close();
}
}
在src目录下建立s1.txt就可以了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询