62,621
社区成员
发帖
与我相关
我的任务
分享package io_rehearsal;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*
*
*写一个文件copy方法:
*如果copy的对象是文件,则copy src文件到目标dest文件;
*如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。
*/
public class MyCopy
{
public static void main(String[] args)
{
try
{
copy("F:/text","E:/java常用文件");
} catch (IOException e)
{
e.printStackTrace();
}
}
//st1为读路径,str2为写路径
public static void copy(String str1,String str2) throws IOException
{
try
{
File f1 = new File(str1);
File f2 = new File(str2);
FileInputStream fis = new FileInputStream(f1);
FileOutputStream fos = new FileOutputStream(f2);
if(f1.exists())
{
//判断是否是文件
if(f1.isDirectory())
{
//把该目录下的文件用一个File接受,然后再用递归方法重新判断
File[] arr = f1.listFiles();
if(arr != null){
for (File file2 : arr)
{
//返回路径
copy(file2.getName(),str1+File.separator+file2.getName());
}
}
}
//判断是否文件
else if(f1.isFile())
{
//进行文件读写操作
byte[] buffer = new byte[1024*2];
int len = 0;
while((len = fis.read(buffer))!= -1)
{
fos.write(buffer, 0, len);
}
}
}
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*
*
*写一个文件copy方法:
*如果copy的对象是文件,则copy src文件到目标dest文件;
*如果copy的对象是目录,则copy src目录下的所有文件和文件夹(包括子文件夹)到dest目录下。
*/
public class MyCopy {
public static void main(String[] args) {
try {
copy("D:\\test1", "D:\\test2");
} catch (IOException e) {
e.printStackTrace();
}
}
// st1为读路径,str2为写路径
public static void copy(String str1, String str2) throws IOException {
try {
File f1 = new File(str1);
if (f1.exists()) {
// 判断是否是文件
if (f1.isDirectory()) {
// 把该目录下的文件用一个File接受,然后再用递归方法重新判断
File[] arr = f1.listFiles();
if (arr != null) {
for (File file2 : arr) {
// 返回路径
copy(str1+File.separator+file2.getName(),str2);
}
}
}
// 判断是否文件
else if (f1.isFile()) {
// 进行文件读写操作
copyFile(str1,str2);
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static boolean copyFile(String file1,String dir) throws IOException{
FileInputStream fis = new FileInputStream(file1);
File srcFile = new File(file1);
File dFile = new File(dir+File.separator+srcFile.getName());
if(dFile.exists()){
System.out.println("Already exists not copy");
return false;
}
FileOutputStream fos = new FileOutputStream(dir+File.separator+srcFile.getName());
int b =0;
while((b=fis.read())!=-1){
fos.write(b);
}
fis.close();
fos.close();
return false;
}
} copy(file2.getName(),str1+File.separator+file2.getName());这个方法的参数有问题。
你看一下你这个方法的声明部分,第一个参数,是源文件的全路径名,你只输入一个文件名,
没有路径部分,无法找到该文件;第二个参数,是目标文件的全路径名,路径错了,取成源路径了
应该取目标路径(str2)。
//进行文件读写操作
byte[] buffer = new byte[1024*2];
int len = 0;
while((len = fis.read(buffer))!= -1)
{
fos.write(buffer, 0, len);
}拷贝文件数据的代码,应该要close掉输入输出流。
public static void copyDir(String sourceDirPath, String targetDirPath) throws IOException {
// 创建目标文件夹
(new File(targetDirPath)).mkdirs();
// 获取源文件夹当前下的文件或目录
File[] file = (new File(sourceDirPath)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
// 复制文件
String type = file[i].getName().substring(file[i].getName().lastIndexOf(".") + 1);
if (type.equalsIgnoreCase("txt"))
FileUtil.copyFile(file[i], new File(targetDirPath + file[i].getName()), MTOServerConstants.CODE_UTF_8,
MTOServerConstants.CODE_GBK);
else
FileUtil.copyFile(file[i], new File(targetDirPath + file[i].getName()));
}
if (file[i].isDirectory()) {
// 复制目录
String sourceDir = sourceDirPath + File.separator + file[i].getName();
String targetDir = targetDirPath + File.separator + file[i].getName();
FileUtil.copyDirectiory(sourceDir, targetDir);
}
}
}
/**
* 读取文件中内容
*
* @param path
* @return
* @throws IOException
*/
public static String readFileToString(String path) throws IOException {
String resultStr = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
byte[] inBuf = new byte[2000];
int len = inBuf.length;
int off = 0;
int ret = 0;
while ((ret = fis.read(inBuf, off, len)) > 0) {
off += ret;
len -= ret;
}
resultStr = new String(new String(inBuf, 0, off, MTOServerConstants.CODE_GBK).getBytes());
} finally {
if (fis != null)
fis.close();
}
return resultStr;
}
/**
* 文件转成字节数组
*
* @param path
* @return
* @throws IOException
*/
public static byte[] readFileToBytes(String path) throws IOException {
byte[] b = null;
InputStream is = null;
File f = new File(path);
try {
is = new FileInputStream(f);
b = new byte[(int) f.length()];
is.read(b);
} finally {
if (is != null)
is.close();
}
return b;
}
/**
* 将byte写入文件中
*
* @param fileByte
* @param filePath
* @throws IOException
*/
public static void byteToFile(byte[] fileByte, String filePath) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(new File(filePath));
os.write(fileByte);
os.flush();
} finally {
if (os != null)
os.close();
}
}