1. public class FileAccess
  2. {
  3.  
  4. public static boolean Move(File srcFile, String destPath)
  5. {
  6. // Destination directory
  7. File dir = new File(destPath);
  8.  
  9. // Move file to new directory
  10. boolean success = srcFile.renameTo(new File(dir, srcFile.getName()));
  11.  
  12. return success;
  13. }
  14.  
  15. public static boolean Move(String srcFile, String destPath)
  16. {
  17. // File (or directory) to be moved
  18. File file = new File(srcFile);
  19.  
  20. // Destination directory
  21. File dir = new File(destPath);
  22.  
  23. // Move file to new directory
  24. boolean success = file.renameTo(new File(dir, file.getName()));
  25.  
  26. return success;
  27. }
  28.  
  29. public static void Copy(String oldPath, String newPath)
  30. {
  31. try {
  32. int bytesum = 0;
  33. int byteread = 0;
  34. File oldfile = new File(oldPath);
  35. if (oldfile.exists()) {
  36. InputStream inStream = new FileInputStream(oldPath);
  37. FileOutputStream fs = new FileOutputStream(newPath);
  38. byte[] buffer = new byte[1444];
  39. int length;
  40. while ( (byteread = inStream.read(buffer)) != -1) {
  41. bytesum += byteread;
  42. System.out.println(bytesum);
  43. fs.write(buffer, 0, byteread);
  44. }
  45. inStream.close();
  46. }
  47. }
  48. catch (Exception e) {
  49. System.out.println( "error ");
  50. e.printStackTrace();
  51. }
  52. }
  53. public static void Copy(File oldfile, String newPath)
  54. {
  55. try {
  56. int bytesum = 0;
  57. int byteread = 0;
  58. //File oldfile = new File(oldPath);
  59. if (oldfile.exists()) {
  60. InputStream inStream = new FileInputStream(oldfile);
  61. FileOutputStream fs = new FileOutputStream(newPath);
  62. byte[] buffer = new byte[1444];
  63. while ( (byteread = inStream.read(buffer)) != -1) {
  64. bytesum += byteread;
  65. System.out.println(bytesum);
  66. fs.write(buffer, 0, byteread);
  67. }
  68. inStream.close();
  69. }
  70. }
  71. catch (Exception e) {
  72. System.out.println( "error ");
  73. e.printStackTrace();
  74. }
  75. }
  76. }

自己做了个demo

  1. import java.io.*;
  2. public class FileAccess {
  3. public static void Copy(String oldPath, String newPath)
  4. {
  5. try {
  6. int bytesum = 0;
  7. int byteread = 0;
  8. File oldfile = new File(oldPath);
  9. if (oldfile.exists()) {
  10. InputStream inStream = new FileInputStream(oldPath);
  11. FileOutputStream fs = new FileOutputStream(newPath);
  12. byte[] buffer = new byte[1444];
  13. int length;
  14. while ( (byteread = inStream.read(buffer)) != -1) {
  15. bytesum += byteread;
  16. System.out.println(bytesum);
  17. fs.write(buffer, 0, byteread);
  18. }
  19. inStream.close();
  20. }
  21. }
  22. catch (Exception e) {
  23. System.out.println( "error ");
  24. e.printStackTrace();
  25. }
  26. }
  27.  
  28. public static void main(String argv[]){
  29. String oldfile = "C:\\aa.txt";
  30. String newPath = "D:\\bb.txt";
  31. Copy( oldfile, newPath);
  32. }
  33. }

java移动/赋值文件 copy/move file的更多相关文章

  1. java SE :文件基本处理 File、FileFilter、FileNameFilter

    File    对目录及文件的创建.重命名.删除.文件列表.判断是否存在 构造函数 // 完整的目录或文件路径 public File(String pathname) //父级目录/文件路径+子级目 ...

  2. Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流)

    1.File类:对硬盘上的文件和目录进行操作的类.    File类是文件和目录路径名抽象表现形式  构造函数:        1) File(String pathname)       Creat ...

  3. java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .

    背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...

  4. JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习

    JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...

  5. Java——文件及目录File操作

    API file.listFiles(); //列出目录下所有文件及子目录fileList[i].isFile() //判断是否为文件 fileList[i].isDirectory() //判断是否 ...

  6. Java精选笔记_IO流【File(文件)类、遍历目录下的文件、删除文件及目录】

    File(文件)类 File类用于封装一个路径,该路径可以是从系统盘符开始的绝对路径,也可以是相对于当前目录而言的相对路径 File类内部封装的路径可以指向一个文件,也可以指向一个目录,在使用File ...

  7. 012PHP文件处理——copy rename file set_include_path

    <?php //copy rename file set_include_path /*file() 以行为单位返回数组 * */ /*$arr=file('b.txt'); foreach ( ...

  8. Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)

    1.File类:对硬盘上的文件和目录进行操作的类.    File类是文件和目录路径名抽象表现形式  构造函数:        1) File(String pathname)       Creat ...

  9. Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件

    package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...

随机推荐

  1. mysql InnoDB 索引小记

    0.索引结构 1).MyISAM与InnoDB索引结构比较,如下: 2).MyISAM的索引结构 主键索引和二级索引结构很像,叶子存储的都是索引以及数据存储的物理地址,其他节点存储的仅仅是索引信息.其 ...

  2. QAQ高精度模板笔记√

    #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #i ...

  3. 【Linux高频命令专题(19)】vi/vim

    概述 其实在Linux中一切命令或者软件都是文件,所以把vi/vim作为高频命令专题之一,也没什么不妥.虽然大家都称之为编辑器~~ vim是vi的高级版本,比如有代码高亮,也就是说可以把vim定位为程 ...

  4. Apache与Tomcat整合

    Apache与Tomcat整合   一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache ...

  5. springmvc常用注解之@Controller和@RequestMapping

    对于各种注解而言,排第一的当然是“@Controller”,表明某类是一个controller. “@RequestMapping”请求路径映射,如果标注在某个controller的类级别上,则表明访 ...

  6. JDK,JRE,JVM区别与联系

    JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...

  7. Android The content of the adapter has changed but ListView did not receive a notification

    The content of the adapter has changed but ListView did not receive a notification. Make sure the co ...

  8. Django admin site(三)InlineModelAdmin

    InlineModelAdmin class InlineModelAdminclass TabularInlineclass StackedInline 举例,有两个Model: from djan ...

  9. PostgreSql中如何kill掉正在执行的sql语句

    虽然可以使用 kill -9 来强制删除用户进程,但是不建议这么去做. 因为:对于执行update的语句来说,kill掉进程,可能会导致Postgres进入到recovery mode 而在recov ...

  10. Generic Data Access Layer泛型的数据访问层

    http://www.codeproject.com/Articles/630277/Generic-Data-Access-Layer-GDA-Part-I http://www.codeproje ...