package testWrite;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
public class write {
InputStream inStream ;
FileOutputStream fs ;
int bytesum = 0;
int byteread = 0;
String data ;
StringBuffer dd;
public static byte[] readdata(String filePath) {
byte[] data = new byte[2048];// 用于存储读取的文件内容
try {
File file = new File(filePath);
if (file.exists()) {
FileInputStream fis = new FileInputStream(file);
fis.read(data);
fis.close();
}else{
System.out.println("文件不存在");
}
} catch ( Exception e) {
}
return data;
}
public void writeFile(String fileCopyPath,byte [] data){
File file = new File(fileCopyPath);
try {
if (!file.exists()) {
file.createNewFile(); //创建文件
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
}else{
System.out.println("文件已经存在");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package testWrite;
public class writeOne extends write {
public static void main(String[] args) {
//第一种
// writeOne a=new writeOne ();
// byte [] data=a.readdata("D:\\Users\\workspace\\testWrite\\src\\testWrite\\write.java");
// a.writeFile("D:\\Users\\workspace\\testWrite\\src\\testWrite\\writenew.java",data);
//第二种
writeOne a2=new writeOne ();
System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径
String path=System.getProperty("user.dir");
System.out.println(path);
String writefile="\\src\\testWrite\\write.java";
String writenewfile="\\src\\testWrite\\writenew.java";
String w1=path+writefile; //读取文件的路径
String w2=path+writenewfile; //写入文件路径
byte [] data=a2.readdata(w1); //读取文件
a2.writeFile(w2 ,data); //写入文件
}
}
本地已经调试通过可以复制write.java 文件 ,注意包,和文件的路径 直接运行第二个java文件就可以了,运行后刷新一下就可以看到复制的文件(备注:我是使用myeclipse环境进行调试)