Java io文件操作(按字符、字节、行、随机读取,追加,文件操作)

来源:互联网 发布:福建广电网络人工电话 编辑:程序博客网 时间:2024/04/19 18:16

什么是流:

 

流是一个抽象的概念。当Java程序需要从数据源读取数据时,会开启一个到数据源的流。数据源可以是文件,内存或者网络等。同样,当程序需要输出数据到目的地时也一样会开启一个流,数据目的地也可以是文件、内存或者网络等。流的创建是为了更方便地处理数据的输入输出。

 

流分为字节流和字符流。字节流也称为原始数据,需要用户读入后进行相应的编码转换。而字符流的实现是基于自动转换的,读取数据时会把数据按照JVM的默认编码自动转换成字符。

 

字节流由InputStream和OutputStream处理,而字符流由Reader和Writer处理。Reader和Writer是Java后加入的处理类,出于让数据的处理更方便的目的。

字节流处理类概述:

 

字节流的处理类有很多,他们都继承自InputStream或者OutputStream抽象类。

 

输入流:

 

先谈谈输入流,输入流中跟数据源直接接触的类有:FileInputStream和ByteArrayInputStream,他们分别实现了从文件或者内存中的字节数组读入数据到输入流。

 

其他的输入流处理类都是装饰类(Decorator模式),下面对他们进行一下简单介绍:

 

BufferedInputStream: 提供了缓冲功能。

DataInputStream: 允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型。应用程序可以使用数据输出流写入稍后由数据输入流读取的数据。

PipedInputStream: 允许以管道的方式来处理流。当连接到一个PipedOutputStream后,它会读取后者输出到管道的数据。

PushbackInputStream: 允许放回已经读取的数据。

SequenceInputStream: 能对多个inputstream进行顺序处理。

 

输出流:

 

基本上每个输入流类都有一个相应的输出流类,提供相应的输出流处理。

同样,跟数据目的地直接接触的类有:FileOutputStream和ByteArrayOutputStream,前者实现了把数据流写入文件的功能,后者实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用toByteArray() 和 toString() 获取数据。

 

下面对其它的装饰类做一下简单介绍:

BufferedOutputStream: 提供了缓冲功能的输出流,在写出完成之前要调用flush来保证数据的输出。

DataOutputStream: 数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。然后,应用程序可以使用数据输入流将数据读入。

PipedOutputStream: 允许以管道的方式来处理流。可以将管道输出流连接到管道输入流来创建通信管道。管道输出流是管道的发送端。通常,数据由某个线程写入PipedOutputStream 对象,并由其他线程从连接的 PipedInputStream 读取。

PrintStream: 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式。我们经常用到的System.out或者System.err都是PrintStream。

字符流处理类概述:

 

输入流:

 

跟数据源直接接触的类:

CharArrayReader: 从内存中的字符数组中读入数据,以对数据进行流式读取。

StringReader:从内存中的字符串读入数据,以对数据进行流式读取。

FileReader:从文件中读入数据。注意这里读入数据时会根据JVM的默认编码对数据进行内转换,而不能指定使用的编码。所以当文件使用的编码不是JVM默认编码时,不要使用这种方式。要正确地转码,使用InputStreamReader。

 

装饰类:

BufferedReader:提供缓冲功能,可以读取行:readLine();

LineNumberReader: 提供读取行的控制:getLineNumber()等方法。

InputStreamReader: 字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符。

 

输出流:

 

根数据目的相关的类:

CharArrayWriter:把内存中的字符数组写入输出流,输出流的缓冲区会自动增加大小。输出流的数据可以通过一些方法重新获取。

StringWriter: 一个字符流,可以用其回收在字符串缓冲区中的输出来构造字符串。

FileWriter:把数据写入文件。

 

装饰类:

BufferedWriter:提供缓冲功能。

OutputStreamWriter:字符流通向字节流的桥梁:可使用指定的 charset 将要写入流中的字符编码成字节。

PrintWriter: 向文本输出流打印对象的格式化表示形式。

Writer或者OutputStream中的flush(): 刷新该流的缓冲,用于确保数据的输出。

 close(): 关闭流并释放与之关联的所有系统资源

其实大家看到下面的图应该就很明白了。

看到这里相信大家对字节流、字符流有一个大致认识了,也对操作流的类有了一个清晰的认识,下面还是通过写代码的方式让大家理解如何操作的。

 

一、多种方式读文件内容。

1、按字节读取文件内容
2、按字符读取文件内容
3、按行读取文件内容
4、随机读取文件内容

[java] view plaincopy
  1. package test.file;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileReader;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.RandomAccessFile;  
  11. import java.io.Reader;  
  12.   
  13. public class ReadFromFile {  
  14.   
  15.     /**   
  16.  
  17.        * 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。   
  18.  
  19.        * @param fileName 文件的名   
  20.  
  21.        */   
  22.   
  23.     public static void readFileByBytes(String fileName){    
  24.   
  25.        File file = new File(fileName);    
  26.   
  27.        InputStream in = null;    
  28.   
  29.        try {    
  30.   
  31.         System.out.println("以字节为单位读取文件内容,一次读一个字节:");    
  32.   
  33.         // 一次读一个字节    
  34.   
  35.         in = new FileInputStream(file);    
  36.   
  37.         int tempbyte;    
  38.   
  39.         while((tempbyte=in.read()) != -1){    
  40.   
  41.          System.out.write(tempbyte);    
  42.   
  43.         }    
  44.   
  45.         in.close();    
  46.   
  47.        } catch (IOException e) {    
  48.   
  49.         e.printStackTrace();    
  50.   
  51.         return;    
  52.   
  53.        }    
  54.   
  55.        try {    
  56.   
  57.         System.out.println("以字节为单位读取文件内容,一次读多个字节:");    
  58.   
  59.         //一次读多个字节    
  60.   
  61.         byte[] tempbytes = new byte[100];    
  62.   
  63.         int byteread = 0;    
  64.   
  65.         in = new FileInputStream(fileName);    
  66.   
  67.         ReadFromFile.showAvailableBytes(in);    
  68.   
  69.         //读入多个字节到字节数组中,byteread为一次读入的字节数    
  70.   
  71.         while ((byteread = in.read(tempbytes)) != -1){    
  72.   
  73.          System.out.write(tempbytes, 0, byteread);    
  74.   
  75.         }    
  76.   
  77.        } catch (Exception e1) {    
  78.   
  79.         e1.printStackTrace();    
  80.   
  81.        } finally {    
  82.   
  83.         if (in != null){    
  84.   
  85.          try {    
  86.   
  87.           in.close();    
  88.   
  89.          } catch (IOException e1) {    
  90.   
  91.          }    
  92.   
  93.         }    
  94.   
  95.        }    
  96.   
  97.     }    
  98.   
  99.     /**   
  100.  
  101.        * 以字符为单位读取文件,常用于读文本,数字等类型的文件   
  102.  
  103.        * @param fileName 文件名   
  104.  
  105.        */   
  106.   
  107.     public static void readFileByChars(String fileName){    
  108.   
  109.        File file = new File(fileName);    
  110.   
  111.        Reader reader = null;    
  112.   
  113.        try {    
  114.   
  115.         System.out.println("以字符为单位读取文件内容,一次读一个字节:");    
  116.   
  117.         // 一次读一个字符    
  118.   
  119.         reader = new InputStreamReader(new FileInputStream(file));    
  120.   
  121.         int tempchar;    
  122.   
  123.         while ((tempchar = reader.read()) != -1){    
  124.   
  125.          //对于windows下,/r/n这两个字符在一起时,表示一个换行。    
  126.   
  127.          //但如果这两个字符分开显示时,会换两次行。    
  128.   
  129.          //因此,屏蔽掉/r,或者屏蔽/n。否则,将会多出很多空行。    
  130.   
  131.          if (((char)tempchar) != '\r'){    
  132.   
  133.           System.out.print((char)tempchar);    
  134.   
  135.          }    
  136.   
  137.         }    
  138.   
  139.         reader.close();    
  140.   
  141.        } catch (Exception e) {    
  142.   
  143.         e.printStackTrace();    
  144.   
  145.        }    
  146.   
  147.        try {    
  148.   
  149.         System.out.println("以字符为单位读取文件内容,一次读多个字节:");    
  150.   
  151.         //一次读多个字符    
  152.   
  153.         char[] tempchars = new char[30];    
  154.   
  155.         int charread = 0;    
  156.   
  157.         reader = new InputStreamReader(new FileInputStream(fileName));    
  158.   
  159.         //读入多个字符到字符数组中,charread为一次读取字符数    
  160.   
  161.         while ((charread = reader.read(tempchars))!=-1){    
  162.   
  163.          //同样屏蔽掉/r不显示    
  164.   
  165.          if ((charread == tempchars.length)&&(tempchars[tempchars.length-1] != '\r')){    
  166.   
  167.           System.out.print(tempchars);    
  168.   
  169.          }else{    
  170.   
  171.           for (int i=0; i<charread; i++){    
  172.   
  173.            if(tempchars[i] == '\r'){    
  174.   
  175.             continue;    
  176.   
  177.            }else{    
  178.   
  179.             System.out.print(tempchars[i]);    
  180.   
  181.            }    
  182.   
  183.           }    
  184.   
  185.          }    
  186.   
  187.         }    
  188.   
  189.             
  190.   
  191.        } catch (Exception e1) {    
  192.   
  193.         e1.printStackTrace();    
  194.   
  195.        }finally {    
  196.   
  197.         if (reader != null){    
  198.   
  199.          try {    
  200.   
  201.           reader.close();    
  202.   
  203.          } catch (IOException e1) {    
  204.   
  205.          }    
  206.   
  207.         }    
  208.   
  209.        }    
  210.   
  211.     }    
  212.   
  213.     /**   
  214.  
  215.        * 以行为单位读取文件,常用于读面向行的格式化文件   
  216.  
  217.        * @param fileName 文件名   
  218.  
  219.        */   
  220.   
  221.     public static void readFileByLines(String fileName){    
  222.   
  223.        File file = new File(fileName);    
  224.   
  225.        BufferedReader reader = null;    
  226.   
  227.        try {    
  228.   
  229.         System.out.println("以行为单位读取文件内容,一次读一整行:");    
  230.   
  231.         reader = new BufferedReader(new FileReader(file));    
  232.   
  233.         String tempString = null;    
  234.   
  235.         int line = 1;    
  236.   
  237.         //一次读入一行,直到读入null为文件结束    
  238.   
  239.         while ((tempString = reader.readLine()) != null){    
  240.   
  241.          //显示行号    
  242.   
  243.          System.out.println("line " + line + ": " + tempString);    
  244.   
  245.          line++;    
  246.   
  247.         }    
  248.   
  249.         reader.close();    
  250.   
  251.        } catch (IOException e) {    
  252.   
  253.         e.printStackTrace();    
  254.   
  255.        } finally {    
  256.   
  257.         if (reader != null){    
  258.   
  259.          try {    
  260.   
  261.           reader.close();    
  262.   
  263.          } catch (IOException e1) {    
  264.   
  265.          }    
  266.   
  267.         }    
  268.   
  269.        }    
  270.   
  271.     }    
  272.   
  273.     /**   
  274.  
  275.        * 随机读取文件内容   
  276.  
  277.        * @param fileName 文件名   
  278.  
  279.        */   
  280.   
  281.     public static void readFileByRandomAccess(String fileName){    
  282.   
  283.        RandomAccessFile randomFile = null;    
  284.   
  285.        try {    
  286.   
  287.         System.out.println("随机读取一段文件内容:");    
  288.   
  289.         // 打开一个随机访问文件流,按只读方式    
  290.   
  291.         randomFile = new RandomAccessFile(fileName, "r");    
  292.   
  293.         // 文件长度,字节数    
  294.   
  295.         long fileLength = randomFile.length();    
  296.   
  297.         // 读文件的起始位置    
  298.   
  299.         int beginIndex = (fileLength > 4) ? 4 : 0;    
  300.   
  301.         //将读文件的开始位置移到beginIndex位置。    
  302.   
  303.         randomFile.seek(beginIndex);    
  304.   
  305.         byte[] bytes = new byte[10];    
  306.   
  307.         int byteread = 0;    
  308.   
  309.         //一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。    
  310.   
  311.         //将一次读取的字节数赋给byteread    
  312.   
  313.         while ((byteread = randomFile.read(bytes)) != -1){    
  314.   
  315.          System.out.write(bytes, 0, byteread);    
  316.   
  317.         }    
  318.   
  319.        } catch (IOException e){    
  320.   
  321.         e.printStackTrace();    
  322.   
  323.        } finally {    
  324.   
  325.         if (randomFile != null){    
  326.   
  327.          try {    
  328.   
  329.           randomFile.close();    
  330.   
  331.          } catch (IOException e1) {    
  332.   
  333.          }    
  334.   
  335.         }    
  336.   
  337.        }    
  338.   
  339.     }    
  340.   
  341.     /**   
  342.  
  343.        * 显示输入流中还剩的字节数   
  344.  
  345.        * @param in   
  346.  
  347.        */   
  348.   
  349.     private static void showAvailableBytes(InputStream in){    
  350.   
  351.        try {    
  352.   
  353.         System.out.println("当前字节输入流中的字节数为:" + in.available());    
  354.   
  355.        } catch (IOException e) {    
  356.   
  357.         e.printStackTrace();    
  358.   
  359.        }    
  360.   
  361.     }    
  362.   
  363.          
  364.   
  365.     public static void main(String[] args) {    
  366.   
  367.        String fileName = "C:/temp/newTemp.txt";    
  368.   
  369.        ReadFromFile.readFileByBytes(fileName);    
  370.   
  371.        ReadFromFile.readFileByChars(fileName);    
  372.   
  373.        ReadFromFile.readFileByLines(fileName);    
  374.   
  375.        ReadFromFile.readFileByRandomAccess(fileName);    
  376.   
  377.     }    
  378.   
  379. }  


 

二、将内容追加到文件尾部

[java] view plaincopy
  1. package test.file;  
  2.   
  3. import java.io.FileWriter;  
  4. import java.io.IOException;  
  5. import java.io.RandomAccessFile;  
  6.   
  7. public class AppendToFile {  
  8.   
  9.     /**   
  10.  
  11.        * A方法追加文件:使用RandomAccessFile   
  12.  
  13.        * @param fileName 文件名   
  14.  
  15.        * @param content 追加的内容   
  16.  
  17.        */   
  18.   
  19.     public static void appendMethodA(String fileName, String content){    
  20.   
  21.        try {    
  22.   
  23.         // 打开一个随机访问文件流,按读写方式    
  24.   
  25.         RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");    
  26.   
  27.         // 文件长度,字节数    
  28.   
  29.         long fileLength = randomFile.length();    
  30.   
  31.         //将写文件指针移到文件尾。    
  32.   
  33.         randomFile.seek(fileLength);    
  34.   
  35.         randomFile.writeBytes(content);    
  36.   
  37.         randomFile.close();    
  38.   
  39.        } catch (IOException e){    
  40.   
  41.         e.printStackTrace();    
  42.   
  43.        }    
  44.   
  45.     }    
  46.   
  47.     /**   
  48.  
  49.        * B方法追加文件:使用FileWriter   
  50.  
  51.        * @param fileName   
  52.  
  53.        * @param content   
  54.  
  55.        */   
  56.   
  57.     public static void appendMethodB(String fileName, String content){    
  58.   
  59.        try {    
  60.   
  61.         //打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件    
  62.   
  63.         FileWriter writer = new FileWriter(fileName, true);    
  64.   
  65.         writer.write(content);    
  66.   
  67.         writer.close();    
  68.   
  69.        } catch (IOException e) {    
  70.   
  71.         e.printStackTrace();    
  72.   
  73.        }    
  74.   
  75.     }    
  76.   
  77.          
  78.   
  79.     public static void main(String[] args) {    
  80.   
  81.        String fileName = "C:/temp/newTemp.txt";    
  82.   
  83.        String content = "new append!";    
  84.   
  85.        //按方法A追加文件    
  86.   
  87.        AppendToFile.appendMethodA(fileName, content);    
  88.   
  89.        AppendToFile.appendMethodA(fileName, "append end. /n");    
  90.   
  91.        //显示文件内容    
  92.   
  93.        ReadFromFile.readFileByLines(fileName);    
  94.   
  95.        //按方法B追加文件    
  96.   
  97.        AppendToFile.appendMethodB(fileName, content);    
  98.   
  99.        AppendToFile.appendMethodB(fileName, "append end. /n");    
  100.   
  101.        //显示文件内容    
  102.   
  103.        ReadFromFile.readFileByLines(fileName);    
  104.   
  105.     }    
  106. }  


二、文件的各种操作类

[java] view plaincopy
  1. package test.file;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.FileWriter;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.PrintWriter;  
  11.   
  12. public class FileOperate {  
  13.   
  14.     public FileOperate(){    
  15.           
  16.     }    
  17.   
  18.     /**   
  19.  
  20.     * 新建目录   
  21.  
  22.     */   
  23.   
  24.     public void newFolder(String folderPath)    
  25.   
  26.     {    
  27.   
  28.     try   
  29.   
  30.     {    
  31.   
  32.     String filePath = folderPath;    
  33.   
  34.     filePath = filePath.toString();    
  35.   
  36.     File myFilePath = new File(filePath);    
  37.   
  38.     if(!myFilePath.exists())    
  39.   
  40.     {    
  41.   
  42.     myFilePath.mkdir();    
  43.   
  44.     }    
  45.   
  46.     System.out.println("新建目录操作 成功执行");    
  47.   
  48.     }    
  49.   
  50.     catch(Exception e)    
  51.   
  52.     {    
  53.   
  54.     System.out.println("新建目录操作出错");    
  55.   
  56.     e.printStackTrace();    
  57.   
  58.     }    
  59.   
  60.     }    
  61.   
  62.     /**   
  63.  
  64.     * 新建文件   
  65.  
  66.     */   
  67.   
  68.     public void newFile(String filePathAndName, String fileContent)    
  69.   
  70.     {    
  71.   
  72.     try   
  73.   
  74.     {    
  75.   
  76.     String filePath = filePathAndName;    
  77.   
  78.     filePath = filePath.toString();    
  79.   
  80.     File myFilePath = new File(filePath);    
  81.   
  82.     if (!myFilePath.exists())    
  83.   
  84.     {    
  85.   
  86.     myFilePath.createNewFile();    
  87.   
  88.     }    
  89.   
  90.     FileWriter resultFile = new FileWriter(myFilePath);    
  91.   
  92.     PrintWriter myFile = new PrintWriter(resultFile);    
  93.   
  94.     String strContent = fileContent;    
  95.   
  96.     myFile.println(strContent);    
  97.   
  98.     resultFile.close();    
  99.   
  100.     System.out.println("新建文件操作 成功执行");    
  101.   
  102.     }    
  103.   
  104.     catch (Exception e)    
  105.   
  106.     {    
  107.   
  108.     System.out.println("新建目录操作出错");    
  109.   
  110.     e.printStackTrace();    
  111.   
  112.     }    
  113.   
  114.     }    
  115.   
  116.     /**   
  117.  
  118.     * 删除文件   
  119.  
  120.     */   
  121.   
  122.     public void delFile(String filePathAndName)    
  123.   
  124.     {    
  125.   
  126.     try   
  127.   
  128.     {    
  129.   
  130.     String filePath = filePathAndName;    
  131.   
  132.     filePath = filePath.toString();    
  133.   
  134.     File myDelFile = new File(filePath);    
  135.   
  136.     myDelFile.delete();    
  137.   
  138.     System.out.println("删除文件操作 成功执行");    
  139.   
  140.     }    
  141.   
  142.     catch (Exception e)    
  143.   
  144.     {    
  145.   
  146.     System.out.println("删除文件操作出错");    
  147.   
  148.     e.printStackTrace();    
  149.   
  150.     }    
  151.   
  152.     }    
  153.   
  154.     /**   
  155.  
  156.     * 删除文件夹   
  157.  
  158.     */   
  159.   
  160.     public void delFolder(String folderPath)    
  161.   
  162.     {    
  163.   
  164.     try   
  165.   
  166.     {    
  167.   
  168.     delAllFile(folderPath); //删除完里面所有内容    
  169.   
  170.     String filePath = folderPath;    
  171.   
  172.     filePath = filePath.toString();    
  173.   
  174.     File myFilePath = new File(filePath);    
  175.   
  176.     if(myFilePath.delete()) { //删除空文件夹    
  177.   
  178.     System.out.println("删除文件夹" + folderPath + "操作 成功执行");    
  179.   
  180.     } else {    
  181.   
  182.     System.out.println("删除文件夹" + folderPath + "操作 执行失败");    
  183.   
  184.     }    
  185.   
  186.     }    
  187.   
  188.     catch (Exception e)    
  189.   
  190.     {    
  191.   
  192.     System.out.println("删除文件夹操作出错");    
  193.   
  194.     e.printStackTrace();    
  195.   
  196.     }    
  197.   
  198.     }    
  199.   
  200.     /**   
  201.  
  202.     * 删除文件夹里面的所有文件   
  203.  
  204.     * @param path String 文件夹路径 如 c:/fqf   
  205.  
  206.     */   
  207.   
  208.     public void delAllFile(String path)    
  209.   
  210.     {    
  211.   
  212.     File file = new File(path);    
  213.   
  214.     if(!file.exists())    
  215.   
  216.     {    
  217.   
  218.     return;    
  219.   
  220.     }    
  221.   
  222.     if(!file.isDirectory())    
  223.   
  224.     {    
  225.   
  226.     return;    
  227.   
  228.     }    
  229.   
  230.     String[] tempList = file.list();    
  231.   
  232.     File temp = null;    
  233.   
  234.     for (int i = 0; i < tempList.length; i++)    
  235.   
  236.     {    
  237.   
  238.     if(path.endsWith(File.separator))    
  239.   
  240.     {    
  241.   
  242.     temp = new File(path + tempList[i]);    
  243.   
  244.     }    
  245.   
  246.     else   
  247.   
  248.     {    
  249.   
  250.     temp = new File(path + File.separator + tempList[i]);    
  251.   
  252.     }    
  253.   
  254.     if (temp.isFile())    
  255.   
  256.     {    
  257.   
  258.     temp.delete();    
  259.   
  260.     }    
  261.   
  262.     if (temp.isDirectory())    
  263.   
  264.     {    
  265.   
  266.     //delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件    
  267.   
  268.     delFolder(path+ File.separatorChar + tempList[i]);//再删除空文件夹    
  269.   
  270.     }    
  271.   
  272.     }    
  273.   
  274.     System.out.println("删除文件操作 成功执行");    
  275.   
  276.     }    
  277.   
  278.     /**   
  279.  
  280.     * 复制单个文件   
  281.  
  282.     * @param oldPath String 原文件路径 如:c:/fqf.txt   
  283.  
  284.     * @param newPath String 复制后路径 如:f:/fqf.txt   
  285.  
  286.     */   
  287.   
  288.     public void copyFile(String oldPath, String newPath)    
  289.   
  290.     {    
  291.   
  292.     try   
  293.   
  294.     {    
  295.   
  296.     int bytesum = 0;    
  297.   
  298.     int byteread = 0;    
  299.   
  300.     File oldfile = new File(oldPath);    
  301.   
  302.     if (oldfile.exists())    
  303.   
  304.     {    
  305.   
  306.     //文件存在时    
  307.   
  308.     InputStream inStream = new FileInputStream(oldPath); //读入原文件    
  309.   
  310.     FileOutputStream fs = new FileOutputStream(newPath);    
  311.   
  312.     byte[] buffer = new byte[1444];    
  313.   
  314.     while ( (byteread = inStream.read(buffer)) != -1)    
  315.   
  316.     {    
  317.   
  318.     bytesum += byteread; //字节数 文件大小    
  319.   
  320.     System.out.println(bytesum);    
  321.   
  322.     fs.write(buffer, 0, byteread);    
  323.   
  324.     }    
  325.   
  326.     inStream.close();    
  327.   
  328.     }    
  329.   
  330.     System.out.println("删除文件夹操作 成功执行");    
  331.   
  332.     }    
  333.   
  334.     catch (Exception e)    
  335.   
  336.     {    
  337.   
  338.     System.out.println("复制单个文件操作出错");    
  339.   
  340.     e.printStackTrace();    
  341.   
  342.     }    
  343.   
  344.     }    
  345.   
  346.     /**   
  347.  
  348.     * 复制整个文件夹内容   
  349.  
  350.     * @param oldPath String 原文件路径 如:c:/fqf   
  351.  
  352.     * @param newPath String 复制后路径 如:f:/fqf/ff   
  353.  
  354.     */   
  355.   
  356.     public void copyFolder(String oldPath, String newPath)    
  357.   
  358.     {    
  359.   
  360.     try   
  361.   
  362.     {    
  363.   
  364.     (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹    
  365.   
  366.     File a=new File(oldPath);    
  367.   
  368.     String[] file=a.list();    
  369.   
  370.     File temp=null;    
  371.   
  372.     for (int i = 0; i < file.length; i++)    
  373.   
  374.     {    
  375.   
  376.     if(oldPath.endsWith(File.separator))    
  377.   
  378.     {    
  379.   
  380.     temp=new File(oldPath+file[i]);    
  381.   
  382.     }    
  383.   
  384.     else   
  385.   
  386.     {    
  387.   
  388.     temp=new File(oldPath+File.separator+file[i]);    
  389.   
  390.     }    
  391.   
  392.     if(temp.isFile())    
  393.   
  394.     {    
  395.   
  396.     FileInputStream input = new FileInputStream(temp);    
  397.   
  398.     FileOutputStream output = new FileOutputStream(newPath + "/" +    
  399.   
  400.     (temp.getName()).toString());    
  401.   
  402.     byte[] b = new byte[1024 * 5];    
  403.   
  404.     int len;    
  405.   
  406.     while ( (len = input.read(b)) != -1)    
  407.   
  408.     {    
  409.   
  410.     output.write(b, 0, len);    
  411.   
  412.     }    
  413.   
  414.     output.flush();    
  415.   
  416.     output.close();    
  417.   
  418.     input.close();    
  419.   
  420.     }    
  421.   
  422.     if(temp.isDirectory())    
  423.   
  424.     {    
  425.   
  426.     //如果是子文件夹    
  427.   
  428.     copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]);    
  429.   
  430.     }    
  431.   
  432.     }    
  433.   
  434.     System.out.println("复制文件夹操作 成功执行");    
  435.   
  436.     }    
  437.   
  438.     catch (Exception e)    
  439.   
  440.     {    
  441.   
  442.     System.out.println("复制整个文件夹内容操作出错");    
  443.   
  444.     e.printStackTrace();    
  445.   
  446.     }    
  447.   
  448.     }    
  449.   
  450.     /**   
  451.  
  452.     * 移动文件到指定目录   
  453.  
  454.     * @param oldPath String 如:c:/fqf.txt   
  455.  
  456.     * @param newPath String 如:d:/fqf.txt   
  457.  
  458.     */   
  459.   
  460.     public void moveFile(String oldPath, String newPath)    
  461.   
  462.     {    
  463.   
  464.     copyFile(oldPath, newPath);    
  465.   
  466.     delFile(oldPath);    
  467.   
  468.     }    
  469.   
  470.     /**   
  471.  
  472.     * 移动文件到指定目录   
  473.  
  474.     * @param oldPath String 如:c:/fqf.txt   
  475.  
  476.     * @param newPath String 如:d:/fqf.txt   
  477.  
  478.     */   
  479.   
  480.     public void moveFolder(String oldPath, String newPath)    
  481.   
  482.     {    
  483.   
  484.     copyFolder(oldPath, newPath);    
  485.   
  486.     delFolder(oldPath);    
  487.   
  488.     }    
  489.   
  490.     public static void main(String args[])    
  491.   
  492.     {    
  493.   
  494.     String aa,bb;    
  495.   
  496.     boolean exitnow=false;    
  497.   
  498.     System.out.println("使用此功能请按[1] 功能一:新建目录");    
  499.   
  500.     System.out.println("使用此功能请按[2] 功能二:新建文件");    
  501.   
  502.     System.out.println("使用此功能请按[3] 功能三:删除文件");    
  503.   
  504.     System.out.println("使用此功能请按[4] 功能四:删除文件夹");    
  505.   
  506.     System.out.println("使用此功能请按[5] 功能五:删除文件夹里面的所有文件");    
  507.   
  508.     System.out.println("使用此功能请按[6] 功能六:复制文件");    
  509.   
  510.     System.out.println("使用此功能请按[7] 功能七:复制文件夹的所有内容");    
  511.   
  512.     System.out.println("使用此功能请按[8] 功能八:移动文件到指定目录");    
  513.   
  514.     System.out.println("使用此功能请按[9] 功能九:移动文件夹到指定目录");    
  515.   
  516.     System.out.println("使用此功能请按[10] 退出程序");    
  517.   
  518.     while(!exitnow)    
  519.   
  520.     {    
  521.   
  522.     FileOperate fo=new FileOperate();    
  523.   
  524.     try   
  525.   
  526.     {    
  527.   
  528.     BufferedReader Bin=new BufferedReader(new InputStreamReader(System.in));    
  529.   
  530.     String a=Bin.readLine();    
  531.   
  532.     int b=Integer.parseInt(a);    
  533.   
  534.     switch(b)    
  535.   
  536.     {    
  537.   
  538.     case 1:System.out.println("你选择了功能一 请输入目录名");    
  539.   
  540.     aa=Bin.readLine();    
  541.   
  542.     fo.newFolder(aa);    
  543.   
  544.     break;    
  545.   
  546.     case 2:System.out.println("你选择了功能二 请输入文件名");    
  547.   
  548.     aa=Bin.readLine();    
  549.   
  550.     System.out.println("请输入在"+aa+"中的内容");    
  551.   
  552.     bb=Bin.readLine();    
  553.   
  554.     fo.newFile(aa,bb);    
  555.   
  556.     break;    
  557.   
  558.     case 3:System.out.println("你选择了功能三 请输入文件名");    
  559.   
  560.     aa=Bin.readLine();    
  561.   
  562.     fo.delFile(aa);    
  563.   
  564.     break;    
  565.   
  566.     case 4:System.out.println("你选择了功能四 请输入文件名");    
  567.   
  568.     aa=Bin.readLine();    
  569.   
  570.     fo.delFolder(aa);    
  571.   
  572.     break;    
  573.   
  574.     case 5:System.out.println("你选择了功能五 请输入文件名");    
  575.   
  576.     aa=Bin.readLine();    
  577.   
  578.     fo.delAllFile(aa);    
  579.   
  580.     break;    
  581.   
  582.     case 6:System.out.println("你选择了功能六 请输入文件名");    
  583.   
  584.     aa=Bin.readLine();    
  585.   
  586.     System.out.println("请输入目标文件名");    
  587.   
  588.     bb=Bin.readLine();    
  589.   
  590.     fo.copyFile(aa,bb);    
  591.   
  592.     break;    
  593.   
  594.     case 7:System.out.println("你选择了功能七 请输入源文件名");    
  595.   
  596.     aa=Bin.readLine();    
  597.   
  598.     System.out.println("请输入目标文件名");    
  599.   
  600.     bb=Bin.readLine();    
  601.   
  602.     fo.copyFolder(aa,bb);    
  603.   
  604.     break;    
  605.   
  606.     case 8:System.out.println("你选择了功能八 请输入源文件名");    
  607.   
  608.     aa=Bin.readLine();    
  609.   
  610.     System.out.println("请输入目标文件名");    
  611.   
  612.     bb=Bin.readLine();    
  613.   
  614.     fo.moveFile(aa,bb);    
  615.   
  616.     break;    
  617.   
  618.     case 9:System.out.println("你选择了功能九 请输入源文件名");    
  619.   
  620.     aa=Bin.readLine();    
  621.   
  622.     System.out.println("请输入目标文件名");    
  623.   
  624.     bb=Bin.readLine();    
  625.   
  626.     fo.moveFolder(aa,bb);    
  627.   
  628.     break;    
  629.   
  630.     case 10:exitnow=true;    
  631.   
  632.     System.out.println("程序结束,请退出");    
  633.   
  634.     break;    
  635.   
  636.     default:System.out.println("输入错误.请输入1-10之间的数");    
  637.   
  638.     }    
  639.   
  640.     System.out.println("请重新选择功能");    
  641.   
  642.     }    
  643.   
  644.     catch(Exception e)    
  645.   
  646.     {    
  647.   
  648.     System.out.println("输入错误字符或程序出错");    
  649.   
  650.     }    
  651.   
  652.     }    
  653.   
  654.     }    
  655. }  
  656. http://blog.csdn.net/jzhf2012/article/details/8458812
0 0
原创粉丝点击