java实现:将一个记事本文件内容复制到另一个文件
3个回答
展开全部
主要是用到java里面的i/o流。代码例子如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* java读写文件,复制文件
* 读取d:/1.txt文件内容,写入f:/text.txt文件中.
* @author young
*
*/
public class FileWriterTest {
// 读写文件
public static void rwFile(){
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter("f:\\text.txt", true);
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\1.txt"), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("文件内容: " + line);
fw.write(line);
fw.flush();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
rwFile();
}
}
首先在D盘新建文件1.txt,输入任意内容。然后执行java代码即可。
展开全部
new File("E:/abc.txt")renameTo(new File("F:/abc.txt"))方法,一行解决问题。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyMove {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CopyMove cm = new CopyMove();
cm.copy("h:\\1.txt", "c:\\2.txt");
cm.move("h:\\3.txt", "h:\\r\\e\\4.txt");
}
//文件复制前必须得到当前文件夹名字且要有\
//当复制深层次路径时要用mkdirs方法
public void copy(String path1,String path2){
try {
FileInputStream fis = new FileInputStream(path1);
int l = fis.available();
byte[] c = new byte[l];
fis.read(c);
fis.close();
int i = path2.lastIndexOf("\\")+1;
System.out.println(i);
String path = path2.substring(0, i);
System.out.println(path);
File f = new File(path);
f.mkdirs();
// System.out.println(f.getAbsolutePath());
// System.out.println(f.isDirectory());
FileOutputStream fos = new FileOutputStream(path2);
fos.write(c);
fos.close();
System.out.println("文件复制成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件复制失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void move(String path1,String path2){
try {
FileInputStream fis = new FileInputStream(path1);
int l = fis.available();
byte[] c = new byte[l];
fis.read(c);
fis.close();
int i = path2.lastIndexOf("\\");
//System.out.println(i);
String path = path2.substring(0, i);
System.out.println(path);
File f = new File(path);
f.mkdirs();
// System.out.println(f.getAbsolutePath());
// System.out.println(f.isDirectory());
FileOutputStream fos = new FileOutputStream(path2);
fos.write(c);
fos.close();
System.out.println("文件移动成功");
File f2 = new File(path1);
f2.delete();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件移动失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyMove {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
CopyMove cm = new CopyMove();
cm.copy("h:\\1.txt", "c:\\2.txt");
cm.move("h:\\3.txt", "h:\\r\\e\\4.txt");
}
//文件复制前必须得到当前文件夹名字且要有\
//当复制深层次路径时要用mkdirs方法
public void copy(String path1,String path2){
try {
FileInputStream fis = new FileInputStream(path1);
int l = fis.available();
byte[] c = new byte[l];
fis.read(c);
fis.close();
int i = path2.lastIndexOf("\\")+1;
System.out.println(i);
String path = path2.substring(0, i);
System.out.println(path);
File f = new File(path);
f.mkdirs();
// System.out.println(f.getAbsolutePath());
// System.out.println(f.isDirectory());
FileOutputStream fos = new FileOutputStream(path2);
fos.write(c);
fos.close();
System.out.println("文件复制成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件复制失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void move(String path1,String path2){
try {
FileInputStream fis = new FileInputStream(path1);
int l = fis.available();
byte[] c = new byte[l];
fis.read(c);
fis.close();
int i = path2.lastIndexOf("\\");
//System.out.println(i);
String path = path2.substring(0, i);
System.out.println(path);
File f = new File(path);
f.mkdirs();
// System.out.println(f.getAbsolutePath());
// System.out.println(f.isDirectory());
FileOutputStream fos = new FileOutputStream(path2);
fos.write(c);
fos.close();
System.out.println("文件移动成功");
File f2 = new File(path1);
f2.delete();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("文件移动失败");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询