tags:

views:

203

answers:

5

My purpose is to rename one file. What I'm doing is: I'm searching a particular string in a text file. Among So many files and if that particular text is found then I want rename that text file with a given string.

Here is what I have tried:

    String opcode="OPCODES";   // String that I want to search.
    File file = new File("D:\\CFF1156");  // All files are inside this folder.
    System.out.println("The File Name is :"+file.getName());
    File[] f = file.listFiles();
    System.out.println("The Length of File is :"+f.length);
    Boolean flag=false;
    StringBuffer contents = new StringBuffer();
    BufferedReader reader = null;

    for(int i=0;i<f.length;i++)
    {
        try{
            reader = new BufferedReader(new FileReader(f[i]));
            String text = null;

            while ((text = reader.readLine()) != null)
            {
                if(text.contains(opcode))
                {
                    System.out.println("Found");
                    System.out.println("The File Containing the Search text is :"+f[i]);
                    f[i].renameTo(new File("D://CFF1156/changed.txt"));
                    System.out.println("renamed :"+(f[i].renameTo(new File("D://CFF1156/changed.txt"))));
                    if(f[i].renameTo(new File("D://CFF1156/changed.txt")))
                    {
                        System.out.println("Successfully renamed");
                    }
                    else
                    {
                        System.out.println("Error");
                    }
                }
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            try
            {
                if (reader != null)
                {
                    reader.close();
                }
            } 
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

The above code is searching the particular file. But I'm not able to rename it.

What would be a working Solution to this problem?

+2  A: 

You are renaming with the same name, in a loop. Fix that thing first. Furthermore, take the returned boolean value in a variable by renameTo() method, and use that variable in your if.

Adeel Ansari
congrats for understanding that bowl of spaghetti.
pstanton
+1  A: 

I am having a hard time reading the code as given, but there is a renameTo method on File (see this javadoc). Note that it takes a File object representing the desired pathname, and returns a boolean.

Michael Easter
He is using the same, but renaming it with the same name in a loop. :)
Adeel Ansari
+1  A: 

From Javadoc of renameTo

..., it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists.

You check the returned boolean in the second renameTo command. Remove all renameTo commands, or store the boolean of the first command and print this boolean to the console.

Markus Lausberg
A: 

First thing, you sometimes use the \ and other times //, im on Mac, so not sure what you should use on Windows. Second, you are renaming all the files to the same name.

Fix:

boolean renamed = f[i].renameTo(new File("D://CFF1156/changed"+ i + ".txt")); System.out.println(renamed?"Succesfully renamed":"Error");

medopal
A: 

Can you be more descriptive on what actually happens when you run the program ? Do you encounter any exceptions on running the code ? Or, does the code silently complete execution without the file being renamed ?

divesh premdeep