Click here to Skip to main content
12,455,701 members (84,543 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
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]code from own answer - formatted[/torsten]
Posted 1-Jan-13 6:34am
Updated 1-Jan-13 8:29am
TorstenH.34.9K
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You're welcome.

Please load a decent IDE like Eclipse or Netbeans (both are free / simple version would be enough) and create a project.
It's pretty easy.

Creating your first project using Eclipse[^]

dito for Netbeans[^]

oh - the coding. We do not provide coding as long as you do not pay us.
But we can help when you've got specific questions. We like to see some coding effort, so please feel free to provide some code along with a well formulated question.

Have fun!
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

OK now we have got some code:


First of all - let's break the static main function. This is common and should be first thing to do:

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();
  }
}

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();
	}
	
}
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 1 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100