java如何实现将一个记事本文件内容复制到另一个文件?
1个回答
展开全部
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();
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询