/** * @author ljt * * To change this generated comment edit the template variable "typecomment": * Window>Preferences>Java>Templates. * To enable and disable the creation of type comments go to * Window>Preferences>Java>Code Generation. */ public class ControlFile { //已有文件的路径,需要备份到那个路径的名字 String filePath, aimFilePath; //保存已有文件路径下的所有的文件的名字 存放String Vector vec; public ControlFile() { filePath = ""; aimFilePath = ""; vec = new Vector(); } public ControlFile(String filePath, String aimFilePath) { this.filePath = filePath; this.aimFilePath = aimFilePath; vec = new Vector(); } //得到目录下所有文件的名字 private void getFileName() { File f = new File(filePath); String str[] = f.list(); for (int i = 0; i < str.length; i++) { vec.addElement(str[i]); } }
} catch (FileNotFoundException e) { System.out.println("文件没有找到"); } catch (IOException e) { System.out.println("copyFile 出错"); } return true; } public static void main(String[] args) { ControlFile confile = new ControlFile("D:\\readFile\\", "D:\\work\\bakFile\\"); confile.getFileName(); Vector ve = new Vector(); ve = confile.vec; if (ve != null) for (int i = 0; i < ve.size(); i++) { System.out.println((String) ve.elementAt(i)); confile.bakFile((String) ve.elementAt(i)); } } }
public static void main(String[] args) { File oldFile = new File("c:/oldFile.txt"); File newFile = new File("c:/newFile.txt"); try { FileInputStream inPutStream = new FileInputStream(oldFile); FileOutputStream outPutStream = new FileOutputStream(newFile); byte[] byteArr = new byte[512]; while (inPutStream.read(byteArr) > 0) { outPutStream.write(byteArr); outPutStream.flush(); } outPutStream.close(); inPutStream.close(); } catch (Exception ex) { ex.printStackTrace(); } }
用通道不就得了?好多人问.... package test;import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.nio.channels.WritableByteChannel;public class Copyfile { public static void main (String args[]) { File f= new File ("D://eMule-0.47c-VeryCD1215-Setup.exe"); try { FileInputStream fin = new FileInputStream (f); FileChannel fc = fin.getChannel(); FileOutputStream fout = new FileOutputStream("D://1.exe"); WritableByteChannel to = fout.getChannel(); fc.transferTo(0, fc.size(),to ); } catch (FileNotFoundException e) { System.err.println("File not found!"); } catch (IOException e) { System.err.println("there are errors on processing "); }
} }
刚刚写了个程序:/** * @param args * @throws IOException * @throws IOException * function: 实现文件的COPY功能,把read.txt的内容拷贝到text.txt文件 */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File read = new File("D://read.txt"); System.out.println(read.length()); FileInputStream readstream = new FileInputStream(read);
byte[] b = new byte[(int)read.length()]; readstream.read(b); File s = new File("D://text.txt"); FileOutputStream writestream = new FileOutputStream(s);
给完整的代码吧! import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.FileInputStream;public class FileOutputTest { /** * @param args * @throws IOException * @throws IOException * function: 实现文件的COPY功能,把read.txt的内容拷贝到text.txt文件 */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub File read = new File("D://read.txt"); System.out.println(read.length()); FileInputStream readstream = new FileInputStream(read);
byte[] b = new byte[(int)read.length()]; readstream.read(b); File s = new File("D://text.txt"); FileOutputStream writestream = new FileOutputStream(s);
ControlFile.java 拷贝莫路径下的所有文件
import java.io.*;
import java.text.*;
import java.util.*;
/**
* @author ljt
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class ControlFile {
//已有文件的路径,需要备份到那个路径的名字
String filePath, aimFilePath;
//保存已有文件路径下的所有的文件的名字 存放String
Vector vec;
public ControlFile() {
filePath = "";
aimFilePath = "";
vec = new Vector();
}
public ControlFile(String filePath, String aimFilePath) {
this.filePath = filePath;
this.aimFilePath = aimFilePath;
vec = new Vector();
}
//得到目录下所有文件的名字
private void getFileName() {
File f = new File(filePath);
String str[] = f.list();
for (int i = 0; i < str.length; i++) {
vec.addElement(str[i]);
}
}
// 文件的拷贝:::测试成功
private boolean bakFile(String fileName) {
try {
//读文件
FileReader raf = new FileReader(filePath + fileName);
String detail = "";
BufferedReader buff = new BufferedReader(raf);
String temp = buff.readLine();
while (temp != null) {
detail += temp + "\n";
temp = buff.readLine();
}
raf.close();
System.out.println(detail);
//写文件
File file = new File(aimFilePath + fileName);
PrintWriter out = new PrintWriter(new FileWriter(file));
out.print(detail);
out.close();
} catch (FileNotFoundException e) {
System.out.println("文件没有找到");
} catch (IOException e) {
System.out.println("copyFile 出错");
}
return true;
}
public static void main(String[] args) {
ControlFile confile =
new ControlFile("D:\\readFile\\", "D:\\work\\bakFile\\");
confile.getFileName();
Vector ve = new Vector();
ve = confile.vec;
if (ve != null)
for (int i = 0; i < ve.size(); i++) {
System.out.println((String) ve.elementAt(i));
confile.bakFile((String) ve.elementAt(i));
}
}
}
File oldFile = new File("c:/oldFile.txt");
File newFile = new File("c:/newFile.txt");
try {
FileInputStream inPutStream = new FileInputStream(oldFile);
FileOutputStream outPutStream = new FileOutputStream(newFile);
byte[] byteArr = new byte[512];
while (inPutStream.read(byteArr) > 0) {
outPutStream.write(byteArr);
outPutStream.flush();
}
outPutStream.close();
inPutStream.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
package test;import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;public class Copyfile {
public static void main (String args[])
{
File f= new File ("D://eMule-0.47c-VeryCD1215-Setup.exe");
try {
FileInputStream fin = new FileInputStream (f);
FileChannel fc = fin.getChannel();
FileOutputStream fout = new FileOutputStream("D://1.exe");
WritableByteChannel to = fout.getChannel();
fc.transferTo(0, fc.size(),to );
} catch (FileNotFoundException e) {
System.err.println("File not found!");
} catch (IOException e) {
System.err.println("there are errors on processing ");
}
}
}
* @param args
* @throws IOException
* @throws IOException
* function: 实现文件的COPY功能,把read.txt的内容拷贝到text.txt文件
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub File read = new File("D://read.txt");
System.out.println(read.length());
FileInputStream readstream = new FileInputStream(read);
byte[] b = new byte[(int)read.length()];
readstream.read(b); File s = new File("D://text.txt"); FileOutputStream writestream = new FileOutputStream(s);
writestream.write(b);
readstream.close();
writestream.close();
writestream.flush(); }
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileInputStream;public class FileOutputTest { /**
* @param args
* @throws IOException
* @throws IOException
* function: 实现文件的COPY功能,把read.txt的内容拷贝到text.txt文件
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub File read = new File("D://read.txt");
System.out.println(read.length());
FileInputStream readstream = new FileInputStream(read);
byte[] b = new byte[(int)read.length()];
readstream.read(b); File s = new File("D://text.txt"); FileOutputStream writestream = new FileOutputStream(s);
writestream.write(b);
readstream.close();
writestream.close();
writestream.flush(); }