本文介绍了在java中搜索和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我需要一个项目来搜索我的驱动器c中的目录中的文件,并搜索文件中的单词以替换它,请帮助!?



hello , i need a project to search files in directories inside my drive c , and search files for a word to replace it with others , any help please !?

package com.cp;

import java.io.*;
import java.util.*;

public class Main {

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String[] args) {
		// TODO code application logic here
		String path = "C:/repositories";
		File folder = new File(path);
		System.out.println("Reading files under the folder "
				+ folder.getAbsolutePath());

		if (folder.isDirectory()) {

			String directory[] = folder.list();
			for (String directoryName : directory) {
				System.out.printf("%s\n", directoryName);
			}

		}
		if (folder.isFile()) {
			int i = 1;
			String file[] = folder.list();
			for (String filename : file) {
				System.out.printf("%s\n", filename);
				// file[i].replaceAll("Fleo0012", "Fleo0013");
				// i++;
			}
		}
		File f = new File("C:\\repositories\\w");
		FileInputStream fs = null;
		InputStreamReader in = null;
		BufferedReader br = null;

		StringBuffer sb = new StringBuffer();

		String textinLine;

		try {
			fs = new FileInputStream(f);
			in = new InputStreamReader(fs);
			br = new BufferedReader(in);

			while (true) {
				textinLine = br.readLine();
				if (textinLine == null)
					break;
				sb.append(textinLine);
			}
			String textToEdit1 = "Fleo0012";
			int cnt1 = sb.indexOf(textToEdit1);
			sb.replace(cnt1, cnt1 + textToEdit1.length(), "Fleo0013");

			fs.close();
			in.close();
			br.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			FileWriter fstream = new FileWriter(f);
			BufferedWriter outobj = new BufferedWriter(fstream);
			outobj.write(sb.toString());
			outobj.close();

		} catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
		}
	}
}





[torsten]来自自己答案的代码 - 格式化[/ torsten ]



[torsten]code from own answer - formatted[/torsten]

推荐答案


public class Main{

  public Main(){
    // Application code goes here and in other 
  }

  // only trigger for JVM to start the app
  public static void main(String[] args) {
    new Main();
  }
}





现在我们可以使用非静态代码。



第一部分是好的!你正在读取文件夹中的文件。

之后它变得有点连线 - 我假设你测试了一些东西。



我添加了一个foreach循环来浏览文件夹中找到的文件。

此外,必须正确设置文件夹中文件的路径。

现在你可以在文件中搜索。



StringTokenizer仍然失败!我没有纠正 - 因为我希望你弄明白。

当您在Exception上设置断点时,你会发现错误很快(点击控制台中的链接,将设置一个你可以调试的断点)。



如果您还有其他问题,请随时询问。

玩得开心。





Now we are able to use non static code.

The first part is good! You are reading the files in the folder.
After that it get''s a bit wired - I assume you tested some things.

I added a foreach loop to browse through the files found in the folder.
Also the path to the files in the folder had to be set up correctly.
Now you can search in the files.

The StringTokenizer still fails! I did not correct that - cause I want you to figure that out.
You will find the error quite fast when you set up a break point on the Exception (click on the link in the console, that will set a break point that you can debug).

If you have any more questions, please feel free to ask.
Have fun.

public class Main {

	public Main(){
		// TODO code application logic here
		String path = "C:/repositories";
		File folder = new File(path);
		System.out.println("Reading files under the folder " + folder.getAbsolutePath());
		if (folder.isDirectory()) {
			String directory[] = folder.list();
			for (String directoryName : directory) {
				System.out.println(directoryName); // does not need printf as it is only a string to be presented
			}
		}
		for (String file : folder.list()) { // loop to browse the files
			System.out.println("This is the file " + file);

			// search for word in here
			File f = new File(path + "/" + file); // creating the absolute path
			System.out.println("The path is " + f.getAbsolutePath());
			FileInputStream fs = null;
			InputStreamReader in = null;
			BufferedReader br = null;

			StringBuffer sb = new StringBuffer();

			String textinLine;

			try {
				fs = new FileInputStream(f);
				in = new InputStreamReader(fs);
				br = new BufferedReader(in);

				while (true) {
					textinLine = br.readLine();
					if (textinLine == null)
						break;
					sb.append(textinLine);
				}
				String textToEdit1 = "Fleo0012";
				int cnt1 = sb.indexOf(textToEdit1);
				sb.replace(cnt1, cnt1 + textToEdit1.length(), "Fleo0013");

				fs.close();
				in.close();
				br.close();

			}
			catch (FileNotFoundException e) { e.printStackTrace();}
			catch (IOException e) { e.printStackTrace();}

			try {
				FileWriter fstream = new FileWriter(f);
				BufferedWriter outobj = new BufferedWriter(fstream);
				outobj.write(sb.toString());
				outobj.close();

			} catch (Exception e) {
				e.printStackTrace(); // always print the complete Stack when possible
//				System.err.println("Error: " + e.getMessage());
			}
		}
//		if (folder.isFile()) {
//			int i = 1;
//			String file[] = folder.list();
//			for (String filename : file) {
//				System.out.println(filename);
//				// file[i].replaceAll("Fleo0012", "Fleo0013");
//				// i++;
//			}
//		}



	}

	// only trigger for JVM to start the application - never executable code in here!
	public static void main(String[] args) {
		new Main();
	}

}


这篇关于在java中搜索和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 14:28
查看更多