java移动/赋值文件 copy/move file
- 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的更多相关文章
- java SE :文件基本处理 File、FileFilter、FileNameFilter
File 对目录及文件的创建.重命名.删除.文件列表.判断是否存在 构造函数 // 完整的目录或文件路径 public File(String pathname) //父级目录/文件路径+子级目 ...
- Android(java)学习笔记167:Java中操作文件的类介绍(File + IO流)
1.File类:对硬盘上的文件和目录进行操作的类. File类是文件和目录路径名抽象表现形式 构造函数: 1) File(String pathname) Creat ...
- java读取xml文件报“org.xml.sax.SAXParseException: Premature end of file” .
背景:java读取xml文件,xml文件内容只有“<?xml version="1.0" encoding="UTF-8"?>”一行 java读取该 ...
- JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...
- Java——文件及目录File操作
API file.listFiles(); //列出目录下所有文件及子目录fileList[i].isFile() //判断是否为文件 fileList[i].isDirectory() //判断是否 ...
- Java精选笔记_IO流【File(文件)类、遍历目录下的文件、删除文件及目录】
File(文件)类 File类用于封装一个路径,该路径可以是从系统盘符开始的绝对路径,也可以是相对于当前目录而言的相对路径 File类内部封装的路径可以指向一个文件,也可以指向一个目录,在使用File ...
- 012PHP文件处理——copy rename file set_include_path
<?php //copy rename file set_include_path /*file() 以行为单位返回数组 * */ /*$arr=file('b.txt'); foreach ( ...
- Android(java)学习笔记110:Java中操作文件的类介绍(File + IO流)
1.File类:对硬盘上的文件和目录进行操作的类. File类是文件和目录路径名抽象表现形式 构造函数: 1) File(String pathname) Creat ...
- Java基础面试操作题: File IO 文件过滤器FileFilter 练习 把一个文件夹下的.java文件复制到另一个文件夹下的.txt文件
package com.swift; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File ...
随机推荐
- mysql InnoDB 索引小记
0.索引结构 1).MyISAM与InnoDB索引结构比较,如下: 2).MyISAM的索引结构 主键索引和二级索引结构很像,叶子存储的都是索引以及数据存储的物理地址,其他节点存储的仅仅是索引信息.其 ...
- QAQ高精度模板笔记√
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #i ...
- 【Linux高频命令专题(19)】vi/vim
概述 其实在Linux中一切命令或者软件都是文件,所以把vi/vim作为高频命令专题之一,也没什么不妥.虽然大家都称之为编辑器~~ vim是vi的高级版本,比如有代码高亮,也就是说可以把vim定位为程 ...
- Apache与Tomcat整合
Apache与Tomcat整合 一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache ...
- springmvc常用注解之@Controller和@RequestMapping
对于各种注解而言,排第一的当然是“@Controller”,表明某类是一个controller. “@RequestMapping”请求路径映射,如果标注在某个controller的类级别上,则表明访 ...
- JDK,JRE,JVM区别与联系
JDK : Java Development ToolKit(Java开发工具包).JDK是整个JAVA的核心,包括了Java运行环境(Java Runtime Envirnment),一堆Java工 ...
- 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 ...
- Django admin site(三)InlineModelAdmin
InlineModelAdmin class InlineModelAdminclass TabularInlineclass StackedInline 举例,有两个Model: from djan ...
- PostgreSql中如何kill掉正在执行的sql语句
虽然可以使用 kill -9 来强制删除用户进程,但是不建议这么去做. 因为:对于执行update的语句来说,kill掉进程,可能会导致Postgres进入到recovery mode 而在recov ...
- Generic Data Access Layer泛型的数据访问层
http://www.codeproject.com/Articles/630277/Generic-Data-Access-Layer-GDA-Part-I http://www.codeproje ...