- 浏览: 225912 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (268)
- oracle (15)
- 设计模式 (4)
- java (16)
- 线程(Thread) (1)
- 常用 (4)
- PL/SQL (1)
- SWING (10)
- 架构 (4)
- 正则表达式 (5)
- Linux (16)
- PostgreSQL (1)
- FTP (1)
- mysql (4)
- TOMCAT (5)
- 素材 (2)
- Hibernate (3)
- 报表打印 (1)
- 高并发,大数据量处理 (6)
- UML (1)
- memcache (3)
- JMF (1)
- 通信 (2)
- window常见问题处理 (5)
- eclipse (7)
- 数据库 (2)
- java内存 (4)
- maven (4)
- Spring (12)
- JavaScript (22)
- nodejs (5)
- OSGI (1)
- 其他 (1)
- 企业开发平台 (1)
- web页面懒加载 (3)
- VMware (2)
- hadoop (2)
- hadoop.hadoop学习笔记 (1)
- web前端 (32)
- vim (6)
- CSS (21)
- web前端.widget (2)
- Activiti (1)
- BPMN (1)
- Cookie (1)
- nigix (1)
- SVN (1)
最新评论
-
woodding2008:
太棒了
用一段代码演示马云双十一晚会上玩的纸牌魔术 -
nihaonihao1987:
[b][/b]
特别响、非常近——BPMN2新规范与Activiti5 -
coosummer:
推荐使用http://buttoncssgenerator.c ...
CSS Button -
tw_wangzhengquan:
ahua186186 写道compile 'com.oracl ...
ext4,spring,hibernate构建企业开发平台 -
ahua186186:
compile 'com.oracle:ojdbc14:10. ...
ext4,spring,hibernate构建企业开发平台
Java读写文件 FileOperate
- 博客分类:
- java
FileOperate.java MergeFile.java import java.io.*; //private String srcfile; } }
import java.io.*;
public class FileOperate {
public FileOperate() {
}
/**
* 新建目录
* @param folderPath String 如 c:/fqf
* @return boolean
*/
public void newFolder(String folderPath) {
try {
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
if (!myFilePath.exists()) {
myFilePath.mkdir();
}
}
catch (Exception e) {
System.out.println( "新建目录操作出错 ");
e.printStackTrace();
}
}
/**
* 新建文件
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt
* @param fileContent String 文件内容
* @return boolean
*/
public void newFile(String filePathAndName, String fileContent) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter(myFilePath);
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
resultFile.close();
}
catch (Exception e) {
System.out.println( "新建目录操作出错 ");
e.printStackTrace();
}
}
/**
* 删除文件
* @param filePathAndName String 文件路径及名称 如c:/fqf.txt
* @param fileContent String
* @return boolean
*/
public void delFile(String filePathAndName) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
java.io.File myDelFile = new java.io.File(filePath);
myDelFile.delete();
}
catch (Exception e) {
System.out.println( "删除文件操作出错 ");
e.printStackTrace();
}
}
/**
* 删除文件夹
* @param filePathAndName String 文件夹路径及名称 如c:/fqf
* @param fileContent String
* @return boolean
*/
public void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //删除空文件夹
}
catch (Exception e) {
System.out.println( "删除文件夹操作出错 ");
e.printStackTrace();
}
}
/**
* 删除文件夹里面的所有文件
* @param path String 文件夹路径 如 c:/fqf
*/
public void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
}
else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path+ "/ "+ tempList[i]);//先删除文件夹里面的文件
delFolder(path+ "/ "+ tempList[i]);//再删除空文件夹
}
}
}
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(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( "复制单个文件操作出错 ");
e.printStackTrace();
}
}
/**
* 复制整个文件夹内容
* @param oldPath String 原文件路径 如:c:/fqf
* @param newPath String 复制后路径 如:f:/fqf/ff
* @return boolean
*/
public void copyFolder(String oldPath, String newPath) {
try {
(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/ " +
(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ( (len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夹
copyFolder(oldPath+ "/ "+file[i],newPath+ "/ "+file[i]);
}
}
}
catch (Exception e) {
System.out.println( "复制整个文件夹内容操作出错 ");
e.printStackTrace();
}
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public void moveFile(String oldPath, String newPath) {
copyFile(oldPath, newPath);
delFile(oldPath);
}
/**
* 移动文件到指定目录
* @param oldPath String 如:c:/fqf.txt
* @param newPath String 如:d:/fqf.txt
*/
public void moveFolder(String oldPath, String newPath) {
copyFolder(oldPath, newPath);
delFolder(oldPath);
}
}
import java.util.*;
public class MergeFile {
private String descfile="E:\\merge";
MergeFile(){
}
public void merge(List<File> srcfiles,String descfile){
if(descfile!=null&&!descfile.isEmpty()){
this.descfile=descfile;
}
File mergefile=new File(descfile);
if(!mergefile.exists()){
mergefile.mkdirs();
}
for(File file:srcfiles){
if(file!=null){
for(File fille2:file.listFiles()){
try {
recursion( file.getPath(), fille2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
public void recursion(String root,File vFile) throws IOException{
if(vFile.isFile()){
File descfile=new File(getRelativePath(root,vFile.getPath()));
copy( vFile, descfile);
}
else {
/*System.out.println(" path="+vFile.getPath());
System.out.println("CanonicalPath="+vFile.getCanonicalPath());
System.out.println(" absolutePath="+vFile.getAbsolutePath());
System.out.println(" relativePath="+getRelativePath(root,vFile.getPath()));*/
File directoryFile=new File(getRelativePath(root,vFile.getPath()));
if(!directoryFile.exists()){
directoryFile.mkdir();
}
File[] files=vFile.listFiles();
if(files!=null && files.length>0){
for(File file:files){
recursion(root,file);
}
}
else{
return;
}
}
}
/**
* 复制单个文件
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(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( "复制单个文件操作出错 ");
e.printStackTrace();
public void copy(File srcFile,File descFile){
copyFile( srcFile.getPath(), descFile.getPath());
/*FileOutputStream out=null;
FileInputStream in=null;
try {
out=new FileOutputStream(descFile);
in=new FileInputStream(srcFile);
byte[] buffer=new byte[1024*8];
while(in.read(buffer)!=-1){
out.write(buffer);
out.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if(in!=null){
in.close();
}
if(out!=null){
out.flush();
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}
public String getRelativePath(String root,String file){
return descfile+file.substring(root.length());
}
public static void main(String[] args){
List<File> files=new ArrayList<File>();
/*files.add(new File("E:\\test\\AppFramework.src"));
files.add(new File("E:\\test\\javaws.src"));
files.add(new File("E:\\test\\swing-worker.src"));
files.add(new File("E:\\test\\swingx.src"));
files.add(new File("E:\\test\\TimingFramework.src"));
files.add(new File("C:\\Documents and Settings\\Administrator\\桌面\\新建文件夹 (2)\\SwingSet3"));
*/
files.add(new File("E:\\test2\\a"));
files.add(new File("E:\\test2\\b"));
new MergeFile().merge(files, "E:\\merge2");
System.out.println("merge成功!");
}
}
发表评论
-
2012-05-19 16:23 native2ascii.exe 转码工具使用说明
2014-04-24 18:25 6331. native2ascii.exe的介绍: nativ ... -
电力行业E语言解析包
2014-02-22 17:44 1154电力行业E语言解析包,见附件 -
Java虚拟机内存模型
2013-10-30 15:40 394转:http://blog.csdn.net/zapldy ... -
获取java 抛出的异常信息的字符串
2013-06-01 10:39 1178如何获取java 抛出的异常信息的字符串,as follow ... -
用java的JTable实现类似Excel的报表控件(转)
2012-05-24 17:08 1495用java的JTable实现类似Excel的报表控件( ... -
FTP客户端(利用sun.net.ftp.FtpClient实现)-转
2012-03-30 14:03 767转:http://wuhongyu.iteye.com/blo ... -
java生成数据库表序列号
2012-03-25 10:26 1430插入数据库数据时往往要生成自增的序列号,该代码提供了方便生产序 ... -
数据导入小工具源码
2012-03-09 12:03 991数据导入小工具源码 -
JMX
2012-03-08 16:58 701http://www.ibm.com/developerwor ... -
序列化
2012-03-06 11:01 575InputStream fis = new FileIn ... -
JAVA调用系统命令或可执行程序
2012-02-24 15:35 771转:http://wuhongyu.iteye.com/blo ... -
java中的transient(转)
2011-11-01 15:46 854转:http://www.blogjava.net/liuga ... -
POI导出报表 ExcelUtil
2011-09-29 17:37 1220import java.io.IOException;im ... -
报表导出ExcelUtil2
2011-09-29 17:34 833import java.io.IOException ... -
Java DecimalFormat 格式化数字
2011-09-29 17:32 774我们经常要将数字进行格式化,比如取2位小数,这是最常见的 ...
相关推荐
JAVA FileOperate
主要对文件进行复制、移动、新建等操作。该文件为一个Jar包,点击即可运行
重庆大学11级计算机学院的第一个JAVA实验: In this project, you are required to implement a file manager on the command line. This manager should include the following functions: Create/Delete a ...
MFC读写二进制文件,1.VS2010编译通过2.C++3.文件写3个浮点数,英文字符,中文字符4.文件读3个浮点数,英文字符,中文字符5.基于MFC的实例
FileOperate
this by vsk last modify 2010 10 13 一个是批量上传FileUpload文件夹内 一个是jsmart的单个文件上传jsmartupload ...如果不存在此目录请调用com.vsked.fileoperate包下FilesOperate中 createDir方法
这是关于文件流操作的程序,简单,很容易理解。
男同胞懂的都懂哈......小视频,小电影,查找微信所有文件夹里面的指定...自已写的批量操作子文件助手, 有批量复制指定文件, 批量移动指定文件, 批量删除空文件夹,批量删除指定文件, 同时有源码需要的请在主页另外下载.
这个C#类封装了我们经常能用到的文件操作方法,包括读写文件、获取文件扩展名、复制文件、追加内容到文件、删除文件、移动文件、创建目录、递归删除文件及目录、列目录、列文件等,不可多得。 using System; using ...
class fileoperate { var $path; var $name; var $result; function creat_file($path,$name) { $filename=$path.$name; if (file_exists($filename)) { echo "文件已经存在,请...
public class FileOperate { #region 删除文件 /// /// 删除文件 /// /// 文件的全路径. /// <returns>bool public static bool DeleteFile(string FileFullPath) { if (File.Exists...
文本操作方法OpenFile and operateFile is very usful if you want to study C#
以下为调用方法 <?php include("./function/... $ss=new fileoperate(); //$ss->creat_file("./log/","aa3.txt"); $ss->write_file("./logpath/","ss.txt","this is the file content",1); ?>
AsyncTaskAssistant异步任务类 CacheUtil缓存 DateUtil日期转换 ...FileOperate文件 HideSoftKeyBoard软键盘 LogUtil日志打印 MyTask线程 NetUtil网络判断 SPUtilsp存储 StorageUtils文件 ToastUtil 弹出
关于文件操作的一些常用方法。
mime读取帮助类 QuotedPrintableEncoding mimeEncoding帮助类 9.PDF 转化类 PDFOperation PDFOperation--C#PDF文件操作帮助类 类主要功能有1.构造函数2.私有字段3.设置字体4.设置页面大小 5.实例化文档6.打开...
FileOperate INIFile 27.序列化 Serialize SerializeHelper 28.压缩解压缩 SharpZip 29.验证码 YZMHelper Captcha 验证码类,一个很个性的验证码类 30.页面辅助类 HTMLHelper UploadEventArgs JavaScriptPlus ...
對給出的文件里的表達式分別進行計算,分別將結果添加在后面:運行環境 TURBOC。里面用到了栈,还有表达式求值。。。。。
│ FileOperate.cs │ INIFile.cs │ ├─时间戳 │ TimeHelper.cs │ ├─条形码 │ BarCodeToHTML.cs │ ├─正则表达式 │ RegexHelper.cs │ ├─汉字转拼音 │ EcanConvertToCh.cs │ PinYin.cs │ ├─...
实现百度文库的效果,写了一个进程,用户上传的txt,pdf,office等文档,可以直接转成swf格式,主要类为FileOperate.cs。同时包含了FlashPaper2.2和swftools转换工具。