Click here to Skip to main content
15,881,812 members

search and replace in java

Member 9534201 asked:

Open original thread
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 !?

Java
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]code from own answer - formatted[/torsten]
Tags: Java

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900