`
lichangwei
  • 浏览: 74936 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

java file(io) operations

    博客分类:
  • java
阅读更多
	public boolean assembleFile(String sFrom, String sTo) throws IOException {

		File formFile = new File(sFrom);
		File toFile = new File(sTo);

		// if the destination file is null or isdirectory then do nothing
		if (toFile == null || toFile.isDirectory()) {
			return false;
		}
		if (formFile.isFile()) {
			BufferedReader br = new BufferedReader(new FileReader(formFile));
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
					toFile)));
			String sTemp = null;
			while ((sTemp = br.readLine()) != null)
				pw.println("<BR>"+sTemp);
			pw.close();
			br.close();
		} else {
			String[] childrenFiles = formFile.list();
			File tempFile = null;
			BufferedReader br = null;
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
					toFile)));
			String sTemp = null;
			for (int i = 0; i < childrenFiles.length; i++) {
				tempFile = new File(sFrom +File.separatorChar
						+ childrenFiles[i]);
				if (tempFile.isFile()) {
					br = new BufferedReader(new FileReader(tempFile));
					pw.println("<br><br><br><br>");
					while ((sTemp = br.readLine()) != null){
						pw.println("<br>"+sTemp);
					}
					
				}
				pw.flush();
			}
			pw.close();
			br.close();
		}
		return true;
	}

	public void assembleFile(File fFrom, File fTo) throws IOException {
		if (fFrom == null || fTo == null || fFrom.isDirectory()
				|| fTo.isDirectory()) {
			return;
		}
		try {
			BufferedReader br = new BufferedReader(new FileReader(fFrom));
			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(
					fTo)));
			String sTemp = null;
			while ((sTemp = br.readLine()) != null)
				pw.println(sTemp);
			pw.close();
			br.close();
		} catch (EOFException ex) {
			throw ex;
		} catch (IOException ex) {
			throw ex;
		}
	}
	
	/**
	 * Delete all the file or directory whose name contains nameCondition.
	 * 
	 * @param destinationFile specifying the destination file
	 * @param nameCondition specifying search condition
	 * @throws IOException
	 */
	public void deleteFileByName(File destinationFile, String nameCondition) throws IOException{
		//
		if(nameCondition.toLowerCase().indexOf(destinationFile.getName().toLowerCase())>-1){
			deleteFileAlways(destinationFile);
			return;
		}
		File[] files = destinationFile.listFiles();
		for(File file :files){//i=0;i<files.length;i++
			if(nameCondition.toLowerCase().indexOf(file.getName().toLowerCase())>-1){
				deleteFileAlways(file);
			}else if(file.isDirectory()){
				deleteFileByName(file,nameCondition);
			}
		}
	}
	
	/**
	 * Delete the destination file, If it is a directory, delete all child file
	 * of it and then delete it.
	 */
	public boolean deleteFileAlways(File destinationFile) throws IOException {
		if (destinationFile == null) {
			return false;
		} else if (destinationFile.isFile()) {
			destinationFile.delete();
			return true;
		} else {
			File[] files = destinationFile.listFiles();
			for (File file : files) {
				if (file.isFile()) {
					file.delete();
				} else {
					deleteFileAlways(file);
					file.delete();
				}
			}
			destinationFile.delete();
		}
		return true;
	}

public boolean moveFile(String sFrom,String sTo) {
    if(sFrom== null || sTo==null){
    	return false;
    }
    File source_file = new File(sFrom);   
    File dest_file = new File(sTo);   
    if(!source_file.exists()){
         return false;
    }
    return source_file.renameTo(dest_file);   
} 
分享到:
评论

相关推荐

    java另存为下载xml

    Java的`java.io.File`类提供了删除文件的方法: ```java import java.io.File; public class FileOperations { public static void deleteFile(String filePath) { File file = new File(filePath); if (file....

    \\(^_^)/ Java io 结构

    5. **文件操作(File Operations)**: Java提供了File类来处理文件和目录。File类提供创建、删除、重命名文件和目录,以及获取文件属性的方法。 6. **管道流(Pipe Stream)**: 管道流允许线程间的数据通信。...

    java-file-operations:JMH 测试不同的 java 文件操作

    Java提供了一系列内置类来进行文件操作,如`java.io.File`,`java.nio.file.Files`和`java.nio.file.Paths`。`File`类主要用于文件和目录的创建、删除、重命名等管理操作,而`Files`和`Paths`类提供了更高级别的...

    TDD-file-operations

    在这个名为"TDD-file-operations"的项目中,重点是使用TDD来处理Java中的文件输入/输出(I/O)操作。下面我们将深入探讨TDD的核心原则、Java中的文件操作以及如何将两者结合。 **TDD的核心原则** 1. **红-绿-重构*...

    javanotes7.pdf

    - 文件IO介绍(Introduction to File I/O) - 其他文本IO功能(Other Text IO Features) - 使用Scanner进行输入(Using Scanner for Input) 4. 控制结构(Control) - 算术运算符(Arithmetic Operators) -...

    Java实现spooling假脱机技术

    可以使用`java.io.File`类来创建和管理这些文件。 3. 设备驱动程序模拟: 设备驱动程序是Java程序中的一个模块,负责从输入井获取任务,执行打印操作,并将结果放入输出井。这个过程可以通过多线程实现,确保并发...

    Java - The Well-Grounded Java Developer

    - **Asynchronous File Channel**: Explanation of asynchronous file channels, which allow non-blocking read/write operations, significantly improving performance in IO-bound applications. #### PART 2:...

    intelJ Idea cucumber java Demo

    在IntelJ IDEA中,选择“File” &gt; “New” &gt; “Project”,在新建项目向导中选择“Java”,然后点击“Next”。在下一步中,可以为项目命名,例如“CucumberJavaDemo”,并选择合适的项目位置,接着点击“Finish”。 ...

    JAVA记事本源程序

    4. **文件操作(File Operations)**:记事本程序需要读写文件,这涉及到`java.io`包中的类,如`File`, `BufferedReader`, `BufferedWriter`等。当你选择“打开”或“保存”时,这些类将用于读取或写入文本文件。 5. ...

    JAVA记事本源代码 收藏

    - **IO Operations**: 输入输出操作,用于文件的读写等。 ### 二、类结构 #### 1. 类定义 ```java public class Notepad4 extends JFrame implements ActionListener, DocumentListener { ``` 这是一个名为`...

    JDK 1.7 API

    7. **文件系统API(File System API)**: 引入`java.nio.file`包,提供了一套符合POSIX标准的文件系统操作接口。 8. **增强的枚举类型(Enhanced Enums)**: 可以为枚举类型添加方法,使其具有更多的功能。 9. **...

    java递归读取目录下所有文件的方法

    import java.io.File; import java.util.ArrayList; import java.util.List; public class ReadFile { private static void test(String fileDir) { List&lt;File&gt; fileList = new ArrayList&lt;File&gt;(); File file = ...

    java面试大全

    import java.io.*; public class StreamExample { public static void main(String[] args) throws IOException { File file = new File("test.txt"); // 写入数据 try (FileOutputStream fos = new ...

    Java 文件上传实例

    // Save file to server or perform other operations } } } catch (SizeLimitExceededException | FileUploadException e) { // Handle exception } } ``` 6. **安全性考虑**: - 验证上传文件的大小和...

    电话薄管理系统java代码.doc

    10. **文件操作(File Operations)**: - 尽管代码中未直接包含文件操作,但根据描述,系统应具有将信息链表保存到磁盘文件以及从文件中读取的功能。这通常可以通过序列化(Serialization)或自定义的文件读写机制...

    web相关的各种Test例子

    Java的java.io包提供了丰富的类来支持文件操作,如File、FileReader/Writer、BufferedReader/Writer等,同时NIO(New I/O)在Java 7引入,提供了更高效、非阻塞的I/O操作。 3. **XML解析**:XML(eXtensible Markup...

    use-geotools.zip

    import java.io.File; import java.util.HashMap; import java.util.Map; public class ShapefileGenerator { public static void main(String[] args) throws Exception { File shpFile = new File("output.shp...

    如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java )

    import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.regex.Pattern; public class BatchDelete { public static void main(String[] ...

    restful restful所需要的jar包

    * Supports asynchronous request processing, decoupled from IO operations. Unlike the Servlet API, the Restlet applications don't have a direct control on the outputstream, they only provide output ...

    Redis集群搭建及使用

    - 访问Redis官网 (https://redis.io/) 的下载页面。 - 选择适合操作系统的稳定版本进行下载。 - 对于Linux系统,通常下载.tar.gz格式的压缩包。 2. **编译安装**: - 解压下载的压缩包。 - 进入解压后的目录,...

Global site tag (gtag.js) - Google Analytics