日期:2014-05-20  浏览次数:20827 次

关于文件!
用程序实现:修改一个目录下的文件及其子目录下的文件的内容。
例如:将code目录下的所有Java源文件中的字符串"frame"替换为"myFrame"!

以下是一个参考例子,但是它仅是用于搜索文件中含有的关键字符,并不能对文件进行修改。
如果那们朋友知道怎样实现这个操作,请帮个忙!非常感谢

------解决方案--------------------
从oldfile读一行 到 strLine
newLine = strLine.replace("frame","myFrame")
将newLine写到 newfile

完了删了oldfile 将newfile重命名为oldfile
------解决方案--------------------
啊,还用到了线程.code下还有没有文件夹?在学校的时候老师叫我做一个给一个路径,然后显示这个路径下的所有文件.
------解决方案--------------------
这段代码写得很不错,用到的全是 JDK 1.5 中的新特性,还用到了
java.util.concurrent 包(这可能是 JDK 1.5 中最深奥的一个
包了)。

有意思,如果要替换的话,除了读文件之外,还需要写文件,下面的
参考以你的代码为基础,仅对 java 文件作操作。

Java code
import java.io.BufferedWriter; 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class BlockingQueueTest {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter base directory (e.g. f:/test): ");
String directory = in.nextLine();
System.out.print(" Enter original string (e.g. frame): ");
String keyword = in.nextLine();
System.out.print("  Enter replace with (e.g. MyFrame): ");
String replace = in.nextLine();
final int FILE_QUEUE_SIZE = 10;
final int SEARCH_THREADS = 100;
BlockingQueue <File> queue = new ArrayBlockingQueue <File>(FILE_QUEUE_SIZE);
FileEnumerationTask enumerator = new FileEnumerationTask(queue, new File(directory));
new Thread(enumerator).start();
for (int i = 1; i <= SEARCH_THREADS; i++)
new Thread(new SearchTask(queue, keyword, replace)).start();
}
}

/**
* This task enumerates all files in a directory and its subdirectories.
*/
class FileEnumerationTask implements Runnable {
/**
* Constructs a FileEnumerationTask.
*
* @param queue the blocking queue to which the enumerated files are added
* @param startingDirectory the directory in which to start the enumeration
*/
public FileEnumerationTask(BlockingQueue <File> queue, File startingDirectory) {
this.queue = queue;
this.startingDirectory = startingDirectory;
}

public void run() {
try {
enumerate(startingDirectory);
queue.put(DUMMY);
} catch (InterruptedException e) {
}
}

/**
* Recursively enumerates all files in a given directory and its
* subdirectories
*
* @param directory the directory in which to start
*/
public void enumerate(File directory) throws InterruptedException {
File[] files = directory.listFiles();
for (File file : files) {
if (file.isDirectory())
enumerate(file);
else
queue.put(file);
}
}

public static File DUMMY = new File("");

private BlockingQueue <File> queue;
private File startingDirectory;
}

/**
* This task searches files for a given keyword.
*/
class SearchTask implements Runnable {
/**
* Constructs a SearchTask.
*
* @param queue the queue from which to take files
* @param keyword the keyword to look for
*/
public SearchTask(BlockingQueue <File> queue, String keyword, String replace) {