java移动/赋值文件 copy/move file
2013-06-27 10:35
633 查看
public class FileAccess { public static boolean Move(File srcFile, String destPath) { // Destination directory File dir = new File(destPath); // Move file to new directory boolean success = srcFile.renameTo(new File(dir, srcFile.getName())); return success; } public static boolean Move(String srcFile, String destPath) { // File (or directory) to be moved File file = new File(srcFile); // Destination directory File dir = new File(destPath); // Move file to new directory boolean success = file.renameTo(new File(dir, file.getName())); return success; } public static void Copy(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { InputStream inStream = new FileInputStream(oldPath); FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println( "error "); e.printStackTrace(); } } public static void Copy(File oldfile, String newPath) { try { int bytesum = 0; int byteread = 0; //File oldfile = new File(oldPath); if (oldfile.exists()) { InputStream inStream = new FileInputStream(oldfile); FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println( "error "); e.printStackTrace(); } } }
自己做了个demo
import java.io.*; public class FileAccess { public static void Copy(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); if (oldfile.exists()) { InputStream inStream = new FileInputStream(oldPath); FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ( (byteread = inStream.read(buffer)) != -1) { bytesum += byteread; System.out.println(bytesum); fs.write(buffer, 0, byteread); } inStream.close(); } } catch (Exception e) { System.out.println( "error "); e.printStackTrace(); } } public static void main(String argv[]){ String oldfile = "C:\\aa.txt"; String newPath = "D:\\bb.txt"; Copy( oldfile, newPath); } }
相关文章推荐
- java移动/复制文件 copy/move file
- php移动文件的函数 move_uploaded_file()和copy
- php移动文件的函数 move_uploaded_file()和copy
- java 文件移动 常规四种方法 和利用FileCopyUtils.copy( , )方法
- java 文件移动 常规四种方法 和利用FileCopyUtils.copy( , )方法
- Java基础之访问文件与目录——移动或复制文件和目录(MoveAndCopyFiles)
- php移动文件的函数 move_uploaded_file()和copy
- php移动文件的函数 move_uploaded_file()和copy
- 使用asmcmdcp命令把datafile从文件系统移动(move)到asm磁盘组中 针对11gR2
- java中对文件file的操作 新建文件 移动文件 复制文件 删除文件
- SharePoint 2013 Rest and SP.js Copyto and Moveto (Rest 和SP.js 基本用法之 复制和移动文件)
- java File的基本操作实例(目录及文件的 新建/拷贝/移动)
- java File(dir, delete, move...) 文件操作
- 一天一篇批处理之——Part3:复制/移动文件命令move,copy,xcopy
- 4 Ways to Copy File in Java -- Java 复制文件的方法
- java 移动文件到其他文件夹(move)
- move_uploaded_file()移动文件失败?
- PHP的移动文件move_uploaded_file..竟然移动了没文件?
- Java利用File类中rename()移动文件
- JAVA - File类中移动文件