GFlam
GFlam

Reputation: 1109

Java find and replace text

Hi i'm working on an android app and this is my problem

I have a text file that is maybe 100 lines long it differs from phone to phone but lets say a section is like this

line 1 = 34
line 2 = 94
line 3 = 65
line 4 = 82
line 5 = 29
etc

each line will be equal to some number however that number will be different from phone since my application will be changing this number and it may already be different before my app is installed. So here's my problem i want to search the text file for say "line 3 = " then delete that entire line and replace it with "line 3 = some number"

My main goal is to change that number at the end of line 3 and keep line 3 that is the text exactly the same i only want to edit the number however the problem is that number will always be different

How can i go about doing this? thanks for any help

Upvotes: 0

Views: 3088

Answers (4)

GFlam
GFlam

Reputation: 1109

Thanks guys for the replies but what i ended up doing was using the sed command in bash and the wild card command * to replace the line and then just ran the script through java which went a little like this

Script

busybox sed -i 's/line 3 = .*/line 3 = 70/g' /path/to/file

Java

Command

execCommand("/path/to/script");

Method

public Boolean execCommand(String command) 
{
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
        os.writeBytes(command + "\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    return true;
}

Upvotes: 2

Kyle
Kyle

Reputation: 4238

A couple thoughts. An easier way to do this (if possible) would be to store these lines in a collection (like an ArrayList) and do all of your manipulation within your collection.

Another solution can be found here. If you need to replace the contents within a text file, you could call a method periodically to do this:

try {
    BufferedReader in = new BufferedReader(new FileReader("in.txt"));
    PrintWriter out = new PrintWriter(new File("out.txt"));

    String line; //a line in the file
    String params[]; //holds the line number and value

    while ((line = in.readLine()) != null) {
            params = line.split("="); //split the line
            if (params[0].equalsIgnoreCase("line 3") && Integer.parseInt(params[1]) == 65) { //find the line we want to replace
                    out.println(params[0] + " = " + "3"); //output the new line
            } else {
                    out.println(line); //if it's not the line, just output it as-is
            }
    }

    in.close();
    out.flush();
    out.close();

}catch(Exception e) { e.printStackTrace(); }

Upvotes: 0

aioobe
aioobe

Reputation: 421040

You can't "insert" or "remove" characters in the middle of a file. I.e., you can't replace 123 with 1234 or 12 in the middle of a file.

So either you "pad" each number so they all have equal width, i.e., you represent 43 as for instance 000043, or you'll probably have to regenerate the whole file.

To regenerate the whole file, I suggest you read the original file line by line, process the lines as appropriate, and write them out to a new, temporary file along the way. Then, when you're through, you replace the old file with the new one.

To process the line I suggest you do something like

String line = "line 3 = 65";

Pattern p = Pattern.compile("line (\\d+) = (\\d+)");
Matcher m = p.matcher(line);

int key, val;
if (m.matches()) {
    key = Integer.parseInt(m.group(1));
    val = Integer.parseInt(m.group(2));

    // Update value if relevant key has been found.
    if (key == 3)
        val = 123456;

    line = String.format("line %d = %d", key, val);
}

// write out line to file...

Upvotes: 2

dacwe
dacwe

Reputation: 43504

The simplest solution is to read the whole file into memory and then replace the line want to change and then write it back to the file.

For exmple:

String input = "line 1 = 34\nline 2 = 94\nline 3 = 65\nline 4 = 82\nline 5 = 29\n";
String out = input.replaceAll("line 3 = (\\d+)", "line 3 = some number");

...outputs:

line 1 = 34
line 2 = 94
line 3 = some number
line 4 = 82
line 5 = 29

Upvotes: 0

Related Questions