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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© daoqin 高级黑马   /  2014-9-14 18:23  /  676 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天看了看File相关操作,自己编写了几个常用的方法,整理一下。
1文件建立
2 文件移动
3 文件拷贝
4 文件夹拷贝
5 文件夹遍历文件
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.sql.Timestamp;

  7. public class SomeMethodForFile {

  8. /**
  9. * @param args
  10. */
  11. public static void main(String[] args) {
  12. // TODO Auto-generated method stub
  13. //File file=new File(E:\\test\\test.txt);//window系统下
  14. //File file=new File(E:/test/test.txt);//Linux系统下
  15. TestFile testFile=new TestFile();
  16. //创建新文件
  17. String path="I:\\test\\1\\testfile.txt";
  18. //testFile.createFile(path);
  19. //修改存放路径及名称,内容也一块复制过去了
  20.         String path2="I:/test2/2/testfile2.txt";
  21.         //testFile.refileName(path, path2);
  22.         
  23.         //复制一个文件
  24.         String src=path;
  25.         String toPath="I:\\test3";
  26.         //testFile.copyFile(src, toPath);
  27.         
  28.         //遍历文件夹
  29.         String targetPath="I:\\FGOSI";
  30.         //testFile.searchFile(targetPath);
  31.         
  32.         //拷贝一个文件夹
  33.         testFile.copyDir(targetPath, "I:\\FGOSI-bak");
  34. }

  35. }
  36. class TestFile{
  37. /**新建一个文件
  38. *
  39. * @param path
  40. */
  41. public void createFile(String path){
  42. File file=new File(path);
  43. if(!file.exists())//判断文件是否存在
  44. {
  45. try{
  46. new File(file.getParent()).mkdirs();//建立上层文件夹
  47. file.createNewFile();//创建文件,执行此语句才产生该文件
  48. }
  49. catch(IOException e){
  50. e.printStackTrace();
  51. }
  52. }
  53. else
  54. {
  55. System.out.println("文件已存在");
  56. }
  57. //获取文件名
  58. String filename=file.getName();
  59. //获取文件路径
  60. String filePath=file.getPath();
  61. //获取文件绝对路径
  62. String fileAbsolutePath=file.getAbsolutePath();
  63. //获取父亲文件路径
  64. String parentPath=file.getParent();
  65. //获取父亲文件名
  66. String parentName=new File(file.getParent()).getName();
  67. //获取文件大小
  68. long size=file.length();
  69. //获取最后一次修改时间
  70. long lastTime=file.lastModified();
  71. String filemsg="文件名:"+filename+"\n路径:"+filePath+"\n绝对路径:"+fileAbsolutePath+"\n父文件路径:"+parentPath;
  72. filemsg+="\n文件大小"+size+"\n最后修改时间:"+new Timestamp(lastTime);
  73. System.out.println(filemsg);
  74. System.out.println("父亲节点文件夹名称"+parentName);
  75. //file.delete();//删除文件
  76. }
  77. /**修改文件路径及名称,内容也一块移动过去了,相当于“移动”
  78. *
  79. * @param fromPath
  80. * @param toPath
  81. */
  82. public void refileName(String fromPath,String toPath){
  83. File file1=new File(fromPath);
  84. File file2=new File(toPath);
  85. //判断file2路径是否存在,不存在则创建
  86. if(!file2.exists()){
  87. new File(file2.getParent()).mkdirs();
  88. }
  89. file1.renameTo(file2);//修改文件名称及路径
  90. System.out.println(fromPath+"文件已移动及重命名到"+toPath);
  91. }
  92. /**复制一个文件
  93. *
  94. * @param src
  95. * @param to
  96. */
  97. public void copyFile(String src, String des) {  
  98. File file1 = new File(src);
  99. File fileDir=new File(des);//产生文件的上层文件夹对象
  100. String topath = des + "\\" + file1.getName();
  101. File file2 = new File(topath);//产生文件对象

  102. if(!fileDir.exists()){
  103. fileDir.mkdirs();//创建文件夹
  104. }
  105. if(file2.exists())
  106. {
  107. file2.delete();//目标位置有该文件,则删除
  108. }
  109. try{
  110. InputStream inStream = new FileInputStream(src); // 读入原文件
  111. FileOutputStream fs = new FileOutputStream(topath);
  112. byte[] buffer = new byte[4098];
  113. int length;
  114. int byteread=0;
  115. int bytesum=0;
  116. while ((byteread = inStream.read(buffer)) != -1) {
  117. bytesum += byteread; // 字节数 文件大小
  118. //System.out.println(bytesum);
  119. fs.write(buffer, 0, byteread);
  120. }
  121. inStream.close();
  122. fs.close();
  123. System.out.println("文件复制成功,新路径为"+topath);
  124. }
  125. catch(IOException e){
  126. e.printStackTrace();
  127. }
  128. }
  129. /**
  130. * 遍历一个文件夹下的文件,并显示文件名称
  131. * @param path
  132. */
  133. public void searchFile(String path){
  134. File file=new File(path);
  135. File[] files=file.listFiles();//file下的文件或文件夹
  136. System.out.println(path+"目录下的文件数有"+files.length);
  137. for(File f:files){
  138. if(f.isFile()){
  139. System.out.println(f.getName());
  140. }
  141. else if(f.isDirectory()){
  142. searchFile(f.getPath());//嵌套
  143. }
  144. }
  145. }
  146. /**
  147. * 复制一个文件夹,从src拷贝到des,src里面可能是文件夹或文件。
  148. * @param src
  149. * @param des
  150. */
  151. public void copyDir(String src,String des){
  152. File file1=new File(src);
  153. File file2=new File(des);
  154. if(!file2.exists()){
  155. file2.mkdirs();
  156. }
  157. File[] files=file1.listFiles();
  158. for(File f:files){
  159. if(f.isFile()){
  160. copyFile(f.getPath(),des+"\\"+new File(f.getParent()).getName());//注意,目标路径要加上这一层文件夹的名称,作为下次调用的新目标路径
  161. }
  162. else if(f.isDirectory()){
  163. copyDir(f.getPath(),des+"\\"+new File(f.getParent()).getName());//嵌套
  164. }
  165. }
  166. }

  167. }
复制代码



0 个回复

您需要登录后才可以回帖 登录 | 加入黑马