- 浏览: 401139 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
handong1587:
代码有一处错.query函数最后一行return的应该是:re ...
RMQ -
yuandong0828:
简洁的特别透彻细致,多谢,
虚函数、虚指针和虚表 -
adam_zs:
谢谢分享!
括号匹配问题 -
hongloumengyanzxw:
good[b][/b]
dup和dup2函数 -
chriszeng87:
最后第二种情况右下角的那个点是不是可以看作相交点的?上面的那种 ...
判断两个链表是否相交
使用Java实现一些文件操作。
import java.io.*; public class FileOperator { public FileOperator() { } /** * Create new folder * @param folderPath String as c:/test */ public void newFolder(String folderPath) { try { File myFilePath = new File(folderPath); if (!myFilePath.exists()) { myFilePath.mkdir(); } } catch (Exception e) { System.out.println("error in creating folder"); e.printStackTrace(); } } /** * Create new file * @param filePath String as c:/test.txt * @param fileContent */ public void newFile(String filePath, String fileContent) { try { File file = new File(filePath); if (!file.exists()) { file.createNewFile(); } FileWriter writer = new FileWriter(filePath); PrintWriter out = new PrintWriter(writer); out.println(fileContent); writer.close(); } catch (Exception e) { System.out.println("error in creating new file"); e.printStackTrace(); } } /** * Delete a file * @param filePath */ public void delFile(String filePath) { try { File file = new File(filePath); file.delete(); } catch (Exception e) { System.out.println("error in deleting file"); e.printStackTrace(); } } /** * Delete folder * @param folderPath */ public void delFolder(String folderPath) { try { delAllFile(folderPath); //删除完里面所有内容 File folder = new File(folderPath); folder.delete(); //删除空文件夹 } catch (Exception e) { System.out.println("error in deleting folder"); e.printStackTrace(); } } /** * Delete all files * @param folderPath */ public void delAllFile(String folderPath) { try { File folder = new File(folderPath); if (!folder.exists()) { return; } if (!folder.isDirectory()) { return; } String[] fileList = folder.list(); String path = null; if (!folderPath.endsWith(File.separator)) { path = folderPath + File.separator; } for (int i = 0; i < fileList.length; i++) { File file = new File(path + fileList[i]); if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { delAllFile(path + fileList[i]); //先删除文件夹里面的文件 delFolder(path + fileList[i]); //再删除空文件夹 } } } catch (Exception e) { System.out.println("error in deleting all files"); e.printStackTrace(); } } /** * Copy a file * @param sourcePath * @param destPath */ public void copyFile(String sourcePath, String destPath) { try { int bytesum = 0; int byteread = 0; File file = new File(sourcePath); if (file.exists()) { //文件存在时 InputStream in = new FileInputStream(sourcePath); FileOutputStream out = new FileOutputStream(destPath); byte[] buffer = new byte[1024]; while ((byteread = in.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 System.out.println(bytesum); out.write(buffer, 0, byteread); } out.flush(); out.close(); in.close(); } } catch (Exception e) { System.out.println("error in copy file"); e.printStackTrace(); } } /** * Copy folder * @param sourcePath * @param destPath */ public void copyFolder(String sourcePath, String destPath) { try { new File(destPath).mkdir(); File folder = new File(sourcePath); String[] fileList = folder.list(); String source = null; String dest = null; if (!sourcePath.endsWith(File.separator)) source = sourcePath + File.separator; if (!destPath.endsWith(File.separator)) dest = destPath + File.separator; for (int i = 0; i < fileList.length; i++) { File file = new File(source + fileList[i]); if (file.isFile()) { copyFile(source+fileList[i], dest+fileList[i]); } else if (file.isDirectory()) { copyFolder(source+fileList[i], dest+fileList[i]); } } } catch (Exception e) { System.out.println("error in copying folder"); e.printStackTrace(); } } /** * Move file * @param sourcePath * @param destPath */ public void moveFile(String sourcePath, String destPath) { copyFile(sourcePath, destPath); delFile(sourcePath); } /** * Move folder * @param sourcePath * @param destPath */ public void moveFolder(String sourcePath, String destPath) { copyFolder(sourcePath, destPath); delFolder(sourcePath); } /** * displayFiles * @param path */ public void displayFiles(String path) { File file = new File(path); if (file.isFile()) { System.out.println(file.getName()); } else { System.out.println("<DIR> " + file.getName()); String[] fileList = file.list(); String pathName = file.getAbsolutePath(); for (int i = 0; i < fileList.length; i++) { displayFiles(pathName + File.separator + fileList[i]); } } } /** * Read a file by char * @param filePath */ public void readFile(String filePath) { try { InputStream in = new FileInputStream(filePath); byte b; while ((b = (byte)in.read()) != -1) { System.out.print((char)b); } } catch (Exception e) { System.out.println("error in read file"); e.printStackTrace(); } } }
发表评论
-
Java类加载器
2011-10-21 18:57 1181Java虚拟机中可以安装多 ... -
stop() 和 suspend() 方法为何不推荐使用
2011-10-21 15:39 1758反对使用stop(),是因为它不安全。它会解除由线程获取 ... -
使用阻塞缓冲区的生产者消费者
2011-10-07 19:59 1204Repository 重点在于Rep ... -
生产者消费者问题
2011-10-07 19:47 1635生产者消费者问题 生产者消费者问题是研究多线程程序 ... -
Java中从方法中返回多个值
2011-09-28 21:42 6508有些时候,我们需要从一个方法中返回多个值。那么在Java中该怎 ... -
遍历List时删除元素
2011-09-14 13:23 2375一般调用删除方法都是集合的方法,例如: List ... -
closure和callback
2011-09-12 22:24 1398闭包(closure)是一个可调用的对象,它记录了一些信息 ... -
倒排索引的简单实现
2011-09-06 20:33 12752首先看一个例子: 假设有3篇文章,file1, ... -
Java是解释型还是编译型
2011-09-01 19:58 1153Java程序需要编译,但是没有直接编译成机器语言,而是编译成. ... -
Java Socket网络编程
2011-09-01 11:34 1479在网络编程中,最基本的操作就是在两台机器之间建立点对点通信,S ... -
使用Java调用C/C++
2011-08-31 23:36 8370我们知道Java是一种平台 ... -
super.getClass()陷阱
2011-08-31 15:25 1205先给出一个问题,下面程序的输出结果是多少? impor ... -
一些java的面试题
2011-08-31 11:46 13211、一个".java"源文件中是否可以包括 ... -
JDK1.5中的线程池
2011-08-29 22:50 903在JDK1.5中,线程池类为 ... -
实现java线程池
2011-08-29 22:43 1273线程池的作用: 线程池作用就是限制系统中执行 ... -
java thread中的run()和start()
2011-08-29 21:28 1305当调用start()后,此线程就处于就绪状态,但并没有运行。之 ... -
嵌套类和内部类的区别
2011-08-28 21:39 18371. 内部类的对象隐含一个外部类的对象引用,可以直接引用 ... -
JVM垃圾回收机制
2011-08-28 21:12 5851. JVM的gc概述 gc即垃圾收集机制是指 ... -
new class[0]
2011-07-28 23:59 1566new Class[0]表示有零个元素的Class数组,即空数 ... -
在两台计算机上使用RMI进行远程方法调用
2011-06-20 22:05 2202RMI概述 远程方法调用就是本地对象能够调 ...
相关推荐
java中文件操作大全 利用PrintStream写文件 文件重命名 转移文件目录
java中文件操作大全包含文件的读取,复制,删除,从命名
java中文件操作大全 覆盖大部分对文件操作的基本代码
java文件读写操作大全java文件读写操作大全java文件读写操作大全java文件读写操作大全java文件读写操作大全
java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java文件操作类java...
java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件java操作Excel文件
java视频教程 Java文件操作 JavaFile
io java 文件操作 命令 全部 io java 文件操作 命令 全部 io java 文件操作 命令 全部 io java 文件操作 命令 全部
Java文件操作大全
# java实现对文件的各种操作的工具类 ## 可以实现的操作有: 1. 删除单个文件 2. 删除文件夹及文件夹下的文件 3. 使用文件流对单个文件进行复制 4. 复制整个文件夹内容(包含子文件夹中的所有内容) 5. ...
java文件操作总结 详细介绍 java对文件的操作 每个都有对应的例子
此工具类只用于Java后端在操作Properties文件的时候写的工具类,方便properties文件的存取操作
java读取共享文件使用jcifs-1.1.11.jar 的SmbFile 类使用用户名密码共享目录操作共享文件
java实现文件读写操作,小例子,供大家参考
一些较为全面的java的文件操作的代码,在学习中有极大地帮助!!
java读写文件的方法,java操作文件
java 中关于文件的操作 比较基础 比较实用
Java7里提供的新的Path类很熟悉为前提,如果你不熟悉这个类,这里就简单说一句,Path是文件系统里对位置的一个逻辑概念,例如c:\ 和../foobar.txt都是Path。