黑马程序员技术交流社区
标题:
有没有人救救我啊!!!
[打印本页]
作者:
21Guns。
时间:
2014-9-8 01:51
标题:
有没有人救救我啊!!!
//编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
我只会递归得到 绝对路路径下的后缀名.java文件. 然后题目还说要把.java文件写到指定目录, IO读写我都会
就是不明白怎么把方法里面的东西写进指定目录下... 好恶心 想半天想不出来谁救救我啊
public class Test9 {
public static void main(String[] args) throws IOException{
File f =new File("E:\\0725\\11");
Lujing(f);
}
public static void Lujing(File f)throws IOException{
File [] src =f.listFiles();
for(File src1 :src){
if(src1.isDirectory()){
Lujing(src1);
}
else{
if(src1.getName().endsWith(".java")){
System.out.println(src1.getName()+src1.getAbsolutePath());
}
}
}
}
}
作者:
java_dream
时间:
2014-9-8 02:01
public class FileCopy {
public static void main(String[] args) throws IOException{
File file = new File("E:/dw");
recursion(file,"G:/");
}
public static void recursion(File file,String targetPath) throws IOException{
if(!file.exists()){
System.out.println("文件或者文件夹不存在!");
return;
}
if(file.isFile()){
copy(file,targetPath+"/"+file.getName());
}
if(file.isDirectory()){
File dir = new File(targetPath+"/"+file.getName());
dir.mkdir();
File[] files = file.listFiles();
for(File f : files){
recursion(f,targetPath+"/"+file.getName());
}
}
}
public static void copy(File file,String targetPath) throws IOException{
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(targetPath);
int len = 0;
byte[] buf = new byte[1024];
while((len=fis.read(buf)) != -1){
fos.write(buf,0,len);
}
fis.close();
fos.close();
}
}
复制代码
作者:
孙雯
时间:
2014-9-8 23:18
马,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
作者:
WakeUp
时间:
2014-9-9 02:45
按你的要求写了一个
/*
需求:
将指定目录下所有.java文件拷贝到另一个目录中,并将扩展名改为.txt
*/
import java.io.*;
import java.util.*;
class FileCopy
{
public static void main(String[] args)
{
File src = new File("D:\\test\\JavaTest");//指定目标文件夹
File des = new File("D:\\test\\copy");//指定目的文件夹
copyToTxt(src,".java",des);
}
public static void copyToTxt(File src,String key,File des)
{
if (!des.exists())
des.mkdir();//如果目的文件夹不存在则创建目录
ArrayList<File> al = new ArrayList<File>();
fileFilter(src,key,al);//过滤目标文件夹,将符合要求的文件添加到集合中
for(File file:al)
{
System.out.println("File:"+file+"...copy:"+copyFile(file,key,des));//遍历集合,复制文件
}
}
public static void fileFilter(File src,final String key,ArrayList<File> al)//过滤文件
{
File[] list = src.listFiles(new FileFilter()
{
//定义过滤器,返回目录和以key结尾的文件
public boolean accept(File pathname)
{
if (pathname.isDirectory())
return true;
else
return pathname.getName().endsWith(key);
}
});
for(File f:list)
{
if (f.isDirectory())
fileFilter(f,key,al);//若是目录则递归
else
{
al.add(f);//将过滤后的文件添加进指定集合
}
}
}
public static boolean copyFile(File file,String key,File des)//复制文件
{
BufferedReader bufr = null;
BufferedWriter bufw = null;
try
{
bufr = new BufferedReader(new FileReader(file));
bufw = new BufferedWriter(new FileWriter(new File(des,file.getName().replace(key,".txt"))));//用.txt替换key
String line = null;
while ((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
bufw.flush();
}
return true;
}
catch (IOException e)
{
throw new RuntimeException("复制失败");
}
finally
{
try
{
if(bufr!=null)
bufr.close();
}
catch (IOException e)
{
throw new RuntimeException("流关闭失败");
}
try
{
if(bufw!=null)
bufw.close();
}
catch (IOException e)
{
throw new RuntimeException("流关闭失败");
}
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2