user2551736
user2551736

Reputation: 47

Java: Overwrite one character while copying a textfile

My current program reads a file and copies it to another directory, but I want it to change one single character to x, which is given by two ints for the number of the line and the number of the character in the line. For example if int line = 5 and int char = 4, the fourth character in line five is changed to an x, the rest remains. How can I add this to my program?

import java.io.*;
import java.util.Scanner;

public class copytest {

    public static void main(String[] args) throws Exception {       
        readFile();
    }

    public static void readFile() throws Exception {

        // Location of file to read
        File file = new File("Old.txt");

        Scanner scanner = new Scanner(file);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            //System.out.println(line);
            writeFile(line);
        }
        scanner.close();
        System.out.println("File Copied"); 
    }

    public static void writeFile(String copyText) throws Exception {

        String newLine = System.getProperty("line.separator");

        // Location of file to output
        Writer output = null;
        File file = new File("New.txt");
        output = new BufferedWriter(new FileWriter(file, true));
        output.write(copyText);
        output.write(newLine);
        output.close();     
    }
 }

Upvotes: 0

Views: 225

Answers (2)

Stephen C
Stephen C

Reputation: 719229

Ran Eldan has answered your Question, but I want to point out a couple of other major problems with your code:

  1. You are violating Java's identifier style rules. Java class names should be "camel case" and the first character should be an uppercase letter; i.e.

    public class copytest {
    

    should be

    public class CopyTest {
    

    This is not just a random nit-pick. If you ignore these style rules, you are liable to get yourself into problem when one of your class names collides with a member or package name defined by your ... or someone else's code. The errors can be very hard to spot.

    And of course, if you flout the style rules, you will get continual flak from other programmers when they need to read your code.

  2. Your writeFile method is horribly inefficient. Each time you call it, you open the file, write a line and close it again. This results in at least 3 system calls for each line written. Syscall overheads are significant.

    And in addition to being inefficient, you have the problem of dealing with existing output files when the program is run multiple times.

    What you should do is open the file once at the start of the run, and use the same BufferedWriter throughout.

Upvotes: 0

Ran Eldan
Ran Eldan

Reputation: 1350

Change your loop to:

 int i=0;
    while (scanner.hasNextLine()) {
        String line = scanner.nextLine();
        if (i == lineNumber) {
            if (line.length() >= charNumber) {
                line = line.substring(0,charNumber) + wantedChar +
                       line.substring(charNumber);
            }
        }
        writeFile(line);
        i++;
    }

Note that it will replace the char only if the line long enogth.

Upvotes: 2

Related Questions