编写一个程序,把这个目录里边的所有的带.java文件都拷贝到另一个目录里边,拷贝成功以后,把后缀名是.java改成.txt
===========================================
- package com.heima.test;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- public class Test3 {
- /**
- * 9:编写一个程序,把这个目录里边的所有的带.java文件都拷贝到另一个目录里边,拷贝成功以后,把后缀名是.java改成.txt
- * @throws IOException
- *
- *
- */
- public static void main(String[] args) throws IOException {
- //获取文件路径
- File f1 = new File("F:/1");
- //目标路径
- File f2 = new File("F:/2");
- //创建文件对象数组
- File[] f3 = f1.listFiles();
- //遍历数组如果是文件,而且,后缀名是.Java,就复制
- for (File f4 : f3) {
- if(f4.getName().endsWith(".java")){
- //==================================
- String s = f4.getName().replace(".java", ".txt");
- System.out.println(s);
-
-
- BufferedInputStream bis =
- new BufferedInputStream(new FileInputStream(f4));
- BufferedOutputStream bos =
- new BufferedOutputStream(new FileOutputStream(new File(f2,s)));
- int b;
- while ((b=bis.read())!=-1){
- bos.write(b);
- }
- //关流
- bis.close();
- bos.close();
- }
- }
- System.out.println("复制好了..");
-
- }
- }
复制代码 |
|