0

Say the following is my file content (hundreds of same pattern lines)

    1979,2013-08-07 19:03:35,abc,12345,310012
    1980,2013-08-07 19:05:03,fds,12345,310160
.
.

I want to read the file and scan the file line by line and replace the 4th column of the line (12345 which repeats on all the lines) to another value as well as adding a new value at the end of each line.

Finally I need to regenerate the file with the updated values as an output.

here is what I do so far:

URL path = ClassLoader.getSystemResource("test.txt");

        File file = new File(path.toURI());

        try
        {

            Scanner scanner = new Scanner(file);

            while (scanner.hasNextLine())
            {
                String line = scanner.nextLine();

                // Here I need to do the replacement codes
            }
            scanner.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }

Is it a good idea to split each line to an array and do the processes like that or is there any better solution for this?

I am also not sure how to create an output with the edited content. Any help would be appreciated.

ComeRun
  • 921
  • 1
  • 20
  • 38

4 Answers4

2

You could try something like this:

public void replace(final int column, final String replacement, final File file, final String... appends) throws IOException {
    assert column >= 0 : "column < 0";
    final List<String> lines = new LinkedList<String>();
    final Scanner reader = new Scanner(file, "UTF-8");
    while(reader.hasNextLine()){
        final String line = reader.nextLine().trim();
        if(line.isEmpty())
            continue;
        final String[] tokens = line.split(",");
        assert column < tokens.length-1 : "column > tokens.length-1";
        tokens[column] = replacement;
        final StringBuilder builder = new StringBuilder();
        for(final String token : tokens)
            builder.append(token + ",");
        for(final String append : appends)
            builder.append(append + ",");
        builder.deleteCharAt(builder.length()-1);
        lines.add(builder.toString());
    }
    reader.close();
    final BufferedWriter writer = new BufferedWriter(new FileWriter(file, false));
    for(final String line : lines){
        writer.write(line);
        writer.newLine();
    }
    writer.flush();
    writer.close();
}

Or if you are using Java 8, you could try something like this:

public void replace(final int column, final String replacement, final File file, final String... appends) throws IOException {
    assert column >= 0 : "column < 0";
    final List<String> lines = new LinkedList<>();
    final Scanner reader = new Scanner(file, "UTF-8");
    while(reader.hasNextLine()){
        final String line = reader.nextLine().trim();
        if(line.isEmpty())
            continue;
        final String[] tokens = line.split(",");
        assert column < tokens.length-1 : "column > tokens.length-1";
        tokens[column] = replacement;
        final List<String> temp = new LinkedList<>();
        temp.addAll(Arrays.asList(tokens));
        temp.addAll(Arrays.asList(appends));
        lines.add(temp.stream().collect(Collectors.joining(",")));
    }
    reader.close();
    final BufferedWriter writer = new BufferedWriter(new FileWriter(file, false));
    lines.forEach(
            l -> {
                try{
                    writer.write(l);
                    writer.newLine();
                }catch(IOException ex){
                    ex.printStackTrace();
                }
            }
    );
    writer.flush();
    writer.close();
}

To call this method, you could do something like this:

replace(3, "string to replace 12345", file, strings_you_want_to_append_to_the_end); 
Josh M
  • 11,611
  • 7
  • 39
  • 49
  • Thanks for the answer, can u let me know how to actually get the modified file out and use it? – ComeRun Aug 28 '13 at 04:02
  • Well, I was under the impression that you wanted to re-write the file with the necessary modifications? – Josh M Aug 28 '13 at 04:03
  • Yes; after it finishes reading the file (and making the necessary changes to each line) it will re-write the file with all of the modified lines. – Josh M Aug 28 '13 at 04:08
1

I'd do this by putting each line into an arraylist, using split(",") on each line of the array, and replacing the 4th index in each array. To rejoin the array back into a string, you'd have to write your own function (Java doesn't have one built-in). There is another stack overflow question that addresses this problem, though.

try
    {
        Scanner scanner = new Scanner(file);

        // initialize the arraylist, which will hold the split strings
        ArrayList<String[]> data = new ArrayList<String[]>();

        while (scanner.hasNextLine())
        {
            // filling the arraylist
            String line = scanner.nextLine();
            String[] split = line.split(",");
            data.add(split);
        }
        scanner.close();

        // replacing the values
        for (String[] split : data) {
            split[3] = "new value";
        }

        // sloppily glueing everything back together
        String output = "";
        for (String[] split : data) {
            for (int i = 0; i < 5; i++) {
                if (i < 4) {output += datum + ",";}
                else {output += datum;}
            }
            output += "\n";
        }
        return output;
    }
catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }

Probably really sloppy and inefficient, but it's pretty logical.

Community
  • 1
  • 1
ajiang
  • 1,702
  • 2
  • 12
  • 12
0

You can do that with a regular expression:

(?<=^[^,]+,[^,]+,[^,]+,)[^,]+

e.g.:

private static final Pattern REGEX_PATTERN = 
        Pattern.compile("(?<=^[^,]+,[^,]+,[^,]+,)[^,]+");

public static void main(String[] args) {

    // Open input file

    // Open output file                

    // Read input line

    String inputLine = "1979,2013-08-07 19:03:35,abc,12345,310012";
    String outputLine = REGEX_PATTERN.matcher(input)
                                     .replaceFirst("YOUR_REPLACEMENT");

    // Print (only for debug)
    System.out.println(
        outputLine 
    );  // prints "1979,2013-08-07 19:03:35,abc,YOUR_REPLACEMENT,310012"

    // Write output line

    // Close both files

}
Paul Vargas
  • 41,222
  • 15
  • 102
  • 148
0

Try the following

stringBuilder = new StringBuilder();
String[] resultArray = sCurrentval.split(",");

resultArray[3] = EDIT_VALVE; //your new value to replace

for (String s : resultArray)
   {
       stringBuilder.append(s);
       stringBuilder.append(",");
   }

sCurrentval = stringBuilder.append(LAST_VALUE).toString(); // add your last value
newuser
  • 8,338
  • 2
  • 25
  • 33