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 ...
随机推荐
- IP地址总结
1.网际协议IP : 网际协议 IP 是 TCP/IP 体系中两个最主要的协议之一.与 IP 协议配套使用的还有四个协议: 地址解析协议 ARP (Address Resolution Protoco ...
- Android开发--使用真机进行USB调试程序
在android小程序的开发过程中,使用eclipse中的虚拟机进行程序开发速度较慢,用真机开发可以显著提高调试的速度. 这里我用的操作系统是win7专业版,手机型号HM1S: 进行USB调试的主要步 ...
- 简单易懂的现代魔法——Play Framework攻略3
接前文:http://www.cnblogs.com/Kassadin/p/4343682.html 目前为止,我们已经研究了Play Framework的体系结构以及Web应用程序的工作原理.本来今 ...
- sql查询数据库中所有表的记录条数,以及占用磁盘空间大小。
SELECT TableName = obj.name, TotalRows = prt.rows, [SpaceUsed(KB)] = SUM(alloc.used_pages)* FROM sys ...
- linux入门教程(九) 文本编辑工具vim
前面多次提到过vim这个东西,它是linux中必不可少的一个工具.没有它很多工作都无法完成.早期的Unix都是使用的vi作为系统默认的编辑器的.你也许会有疑问,vi与vim有什么区别?可以这样简单理解 ...
- N皇后//搜索入门
P1080 N皇后 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 检查一个如下的6 x 6的跳棋棋盘,有六个棋子被放置在棋盘上,使得每行.每列只有一个,每条 ...
- mvn 安装ojdbc6.jar
mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion= - Dpackaging=jar -Dfile ...
- Delphi判断字符串是否是数字、字母、大小写字母
function IsNumberic(Vaule:String):Boolean; //判断Vaule是不是数字 var i:integer; begin result:=true; //设置返回值 ...
- C++:虚函数的详解
5.4.2 虚函数详解 1.虚函数的定义 虚函数就是在基类中被关键字virtual说明,并在派生类重新定义的函数.虚函数的作用是允许在派生类中重新定义与基类同名的函数,并且可以通过基类指针或引用来访问 ...
- Java API —— Math类
1.Math类概述 Math 类包含用于执行基本数学运算的方法,如初等指数.对数.平方根和三角函数. 2.成员变量 public static final doubl ...