在java中如何将一个文件复制到另外一个地方(我想文件内容一样,名字不一样)?急啊
本文导语: 各位老大,急用啊,给个代码? | package myutils; import java.io.*; import java.util.*; /** 目录文件拷贝(jdk) @version v1.0 2001-9-27 @auther Qixy */ public class Copy { /** Null & s...
|
import java.io.*;
import java.util.*;
/**
目录文件拷贝(jdk)
@version v1.0 2001-9-27
@auther Qixy
*/
public class Copy {
/**
Null & simple construct .
*/
public Copy()
{
//void to construct for a class !
}
/**
Application main 使用方法
usage:java Copy [-x(包含子目录)] s_dirorfile(源文件夹/文件) t_dirorfile(目标文件夹/文件)
*/
public static void usage()
{
System.out.println("usage:java Copy -x [s_dirorfile] [t_dirorfile]");
System.exit(0);
return;
}
/**
Application main entrance
@see usage
*/
public static void main(String[] args) throws IOException {
/** To be corrected ... */
boolean bx=false;
Copy c=new Copy();
//test ...
File ff=new File("e:\eeeee\dfe\s.x");
System.out.println("ff.getAbsolutePath()="+ff.getAbsolutePath());
System.out.println("ff.getName()="+ff.getName());
System.out.println("ff.getPath()="+ff.getPath());
DayTime dt=new DayTime();
for(int ii=0;ii filepointer)
{
filepointer = fff.getFilePointer();
sss = fff.readLine();
System.out.println("filepointer : "+filepointer+" "+sss);
}
fff.close();
}
catch(FileNotFoundException dd)
{
System.out.println("no file");
}
catch(IOException s)
{
System.out.println("read file error");
}
save to
RandomAccessFile fffd;
String file = "d:\aa.txt";
try
{
fffd =new RandomAccessFile(file,"rw");
fffd.writeChars(sss) ;
fff.close();
}
catch(FileNotFoundException dd)
{
System.out.println("no file");
}
catch(IOException s)
{
System.out.println("read file error");
}
|
public class Copy {
public static void main(String[] args) throws IOException {
File inputFile = new File("farrago.txt");
File outputFile = new File("outagain.txt");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
}
}
|
void copyFile(File destfile, File srcfile){
byte[] bytearray = new byte[512];
int len = 0;
FileInputStream input = new FileInputStream(srcfile);
FileOutputStream output = new FileOutputStream(destfile);
try {
while ((len = input.read(bytearray)) != -1) {
output.write(bytearray, 0, len);
}
} catch (FileNotFoundException exc) {
exc.printStackTrace();
} catch (SecurityException exc) {
exc.printStackTrace();
} finally {
input.close();
output.close();
}
|
import java.io.*;
public class jCOPY {
public static void main(String args[]){
try {
jCOPY j = new jCOPY();
j.CopyFile(new File(args[0]),new File(args[1]));
}
catch (Exception e) {
e.printStackTrace();
}
}
public void CopyFile(File in, File out) throws Exception {
FileInputStream fis = new FileInputStream(in);
FileOutputStream fos = new FileOutputStream(out);
byte[] buf = new byte[1024];
int i = 0;
while((i=fis.read(buf))!=-1) {
fos.write(buf, 0, i);
}
fis.close();
fos.close();
}
}