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();
}
}}
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();
}
}}
解决方案 »
- Integer.parseInt(stdin.readline()) JAVA 里有这个指令吗
- 111
- hashmap嵌套取值问题
- 有没有在was6.1下跑过联通短信接口的???
- 急求一个WEB服务器!
- 阅读java源码有什么好的方法?
- ssh 整合二级页面无法找到action错误求解
- 求《The JasperReports Ultimate Guide》的PDF电子文档
- 控件大小不能控制的问题(100)
- java Swing组件不能显示问题,在JTextArea读取java文件,添加读取的文件语句后不能显示组件,添加之前可以显示,新人求教啊 谢谢
- 急急急,劳烦各位大虾帮下忙,关于如何在子类中访问父类中的私有属性!
- Argo UML怎么用啊?
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at io_rehearsal.MyCopy.copy(MyCopy.java:37)
at io_rehearsal.MyCopy.main(MyCopy.java:22)错误在这里。传入一个文件路径就OK,目录的话就出现这错误,反正没法拷贝,是不是思路错了?
*
*写一个文件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;
}}
你看一下你这个方法的声明部分,第一个参数,是源文件的全路径名,你只输入一个文件名,
没有路径部分,无法找到该文件;第二个参数,是目标文件的全路径名,路径错了,取成源路径了
应该取目标路径(str2)。
//进行文件读写操作
byte[] buffer = new byte[1024*2];
int len = 0;
while((len = fis.read(buffer))!= -1)
{
fos.write(buffer, 0, len);
}拷贝文件数据的代码,应该要close掉输入输出流。
1.递归调用的copy方法所用的参数有问题,创建File对象需要这个文件的绝对路径值才能真正找到这个文件并创建正确的对象,如果用相对路径只能在这个Class文件所在的当前路径中创建文件。
2.在将源文件内容写入目标文件中时,目标文件得是文件才行(楼主的目标文件是目录)
归整了下jike316的代码,
/*
*
*写一个文件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;
}}
// 创建目标文件夹
(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();
}
}