A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

李军辉

初级黑马

  • 黑马币:4

  • 帖子:19

  • 精华:0

© 李军辉 初级黑马   /  2014-4-1 17:08  /  1556 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

package com.itheima;
/*
* 第九题:编写程序,将指定目录下所有.java文件拷贝到另一个目的中,并将扩展名改为.txt
* 晕了
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.net.ssl.SSLContext;

public class text9 {
        public static File file;
        public static File file1;
        public static void main(String[] args) {
                file = new File("D:\\毕向东java预热");
            file1 = new File("D:\\text");
            File ff = splittxt1(file,file1);
            if(ff == null ){
                    return ;
            }else{
            Copy(file, ff);
          }
        }
   public static void Copy(File file,File file1){  
           FileInputStream fps = null;
           FileOutputStream fos = null;
           String temp = null;
           File fff = null;
             if(file.exists()){
                     File[] s = file.listFiles();
                     File[] ss = file1.listFiles();

                     for(int x = 0; x< s.length; x++){
                   
                             try {
                                        fps = new FileInputStream(s[x]);
                                } catch (FileNotFoundException e) {
                                        // TODO Auto-generated catch block
                                        System.out.println("文件读取失败,未找到该文件");
                                }
                             try {
                                        fos = new FileOutputStream(ss[x]);
                                } catch (FileNotFoundException e) {
                                        System.out.println("文件写入失败文件不存在");
                                }
                             byte[] by = new byte[1024];
                             int len = 0;
                             try {
                                        while((len = fps.read(by)) != -1){         
                                                         fos.write(by, 0, len);
                                                         
                                         }
                                }catch(NullPointerException e){
                                         System.out.println("缓存区未建立");
                                } catch (IOException e) {
                                         System.out.println("文件内容读写失败");
                                }
                         
                           try {
                                        fos.flush();
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                }
                    }           
                    try {
                                fos.close();
                                fps.close();
                        } catch (IOException e) {
                                System.out.println("输入输出流关闭失败");
                        }             
          }
             
   }  
      //修改后缀名与存入另一个文件夹
      public static File splittxt1(File file11,File file1){
                //文件路径名数组
                File[] sc = file1.listFiles();
                //字符串文件路径名数组
                    String[] ss = file11.list();
                    //后缀名
                    String tep = ".txt";
                    String[] sp = null;
                    File sr = null;
       
            for(int i=0; i<ss.length;i++){
                    try{    //文件如果存在返回null并结束函数
                            if(sc.exists()){
                                    System.out.println("文件已存在");
                                    return null;
                            }
                    }catch(ArrayIndexOutOfBoundsException e13){
                            //System.out.println(sc);
                            //字符串数组路径名切割
                    sp = ss.split("\\.");
               try {
                                    //合并成新的路径名并存入指定文件夹
                                sr = file11.createTempFile(sp[0], tep, file1);
                                //System.out.println(sp[0]);
                                //System.out.println(tep);
                                //System.out.println(sr);
                       
                        } catch (IllegalArgumentException e) {
                                System.out.println("文件名少于三个字符");
                        }catch(IOException e1){
                                System.out.println("重命名失败");
                        }
                  }
            }
                return file1;
                    }
}
重命名前

重命名后


求解这些多出来的数字






















评分

参与人数 1技术分 +1 收起 理由
菜小徐 + 1

查看全部评分

7 个回复

正序浏览
sc.exists() 这个应该改成 sc[i].exists() 吧
sp = ss.split("\\.");应该改成sp = ss[i].split("\\.");
不然语法上都通不过
回复 使用道具 举报
  1. package com.allfile;

  2. import java.io.File;
  3. import java.io.FilenameFilter;
  4. import java.io.IOException;
  5. import java.util.ArrayList;

  6. public class readAllFile {

  7.         /**
  8.          * @param args
  9.          * @throws Exception
  10.          */
  11.         public static void main(String[] args) throws Exception {
  12.                 /*
  13.                  * 第九题:编写程序,将指定目录下所有.java文件拷贝到另一个目的中, 并将扩展名改为.txt
  14.                  */
  15.                 /**
  16.                  * 思路:
  17.                  * 1.先指定获取的文件源
  18.                  * 2.题目要求需要目录下所有的,那就是子目录子目录的子目录都要获取,所以要使用递归
  19.                  * 3.然后在设定过滤条件,再把过滤好的文件,全部放入集合中,
  20.                  * 然后再把集合中的文件依次创建再指定的文件中
  21.                  */
  22.                
  23.                
  24.                
  25.                 //指定文件源
  26.                 File file = new File(
  27.                                 "E:\\Users\\Administrator\\Workspaces\\MyEclipse 8.5\\Thread");
  28.         //递归读取文件
  29.                 bian(file);
  30.                
  31.                 //这里是过滤好的文件,依次创建
  32.                 for (File fl : arrayList) {
  33.                         File file2=new File("D:\\飞七Game",fl.getName());
  34.                         file2.createNewFile();
  35.                 }
  36.         }

  37.         static ArrayList<File> arrayList = new ArrayList<File>();

  38.         public static void bian(File file) {
  39.                
  40.                 //这里面就是过滤条件
  41.                 File[] files = file.listFiles(new FilenameFilter() {

  42.                         @Override
  43.                         public boolean accept(File dir, String name) {

  44.                                 File file = new File(dir.getPath(), name);

  45.                                 if (name.endsWith(".java")) {

  46.                                         return true;
  47.                                 }

  48.                                 if (file.isDirectory()) {

  49.                                         return true;
  50.                                 }

  51.                                 return false;
  52.                         }
  53.                 });
  54.                
  55.                
  56.                 for (File file2 : files) {
  57.                         if (file2.isDirectory()) {
  58.                                 bian(file2);
  59.                         } else {

  60.                                 arrayList.add(file2);

  61.                         }
  62.                 }

  63.         }
  64. }
复制代码
回复 使用道具 举报
兄弟 刚写的 还热乎着呢

给你参考,加上我刚才的思路,你想一下.
  1. package NoUseArea;

  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;

  8. public class text9 {
  9.         public static void main(String[] args) throws IOException {
  10.                
  11.                 File source = new File("D:\\downloads\\JAVA\\毕向东视频\\毕向东视频Java基础源代码Codes\\day01");
  12.                 File target = new File("D:\\myjava\\myself\\1");
  13.                
  14.                 fileCheck(source,target);
  15.         }
  16.        
  17.        
  18.         public static void fileCheck(File source,File target) throws IOException{
  19.                 File[] files = source.listFiles();
  20.                 for(File file : files){
  21.                         if(file.isFile()){
  22.                                 if(file.getName().endsWith(".java"))
  23.                                         fileCopy(file,target);
  24.                         }else{
  25.                                 fileCheck(source,target);
  26.                         }
  27.                 }
  28.         }
  29.        
  30.         public static void fileCopy(File source,File target) throws IOException{
  31.                 String Name=source.getName().replace(".java", ".txt");
  32.                 File newTarget=new File(target,Name);
  33.                 BufferedInputStream bfis = new BufferedInputStream(new FileInputStream(source));
  34.                 BufferedOutputStream bfos = new BufferedOutputStream(new FileOutputStream(newTarget));
  35.        
  36.                 int len = 0;
  37.                 while((len=bfis.read())!=-1){
  38.                         bfos.write(len);
  39.                 }
  40.                
  41.                 bfis.close();
  42.                 bfos.close();
  43.         }
  44. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

回复 使用道具 举报 1 0
osully 发表于 2014-4-1 17:30
你还是自己改代码吧...头都看大了....

要是我写这个...

啊哈哈,{:2_34:}   新手就这样啊
回复 使用道具 举报
osully 发表于 2014-4-1 17:30
你还是自己改代码吧...头都看大了....

要是我写这个...

啊哈哈,{:2_34:}

点评

真心的看的头大 你这个 要不我写个给你看看吧  发表于 2014-4-1 17:38
回复 使用道具 举报
你还是自己改代码吧...头都看大了....

要是我写这个...
思路:
遍历源文件夹中所有的文件,
判断是不是java文件,
是就读取,写入到新路径
而文件的名字可以用file.getName().replace(".java",".txt") 就可以实现了

你这个真心看的头疼
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马