-5

My contents of text file looks something like this

Synonyms of Fluster:

*panic

*perturb

*disconcert

*confuse ......

Now i wish to replace the * with numbers.Something like this.

output

Synonyms of Fluster:

1)panic

2)perturb

3)disconcert

4)confuse ......

Edit:

Integer count = 1;
     File input = new File("C:\\Sample.txt");
     File output = new File("C:\\output.txt");

     BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
     Writer writer =new FileWriter(output);
      while((line=reader.readLine())!=null)
      {
          if(line.contains("*"))
          {
              line.replace("*",count.toString() );
              writer.write(line);
              count++;
          }

          else
          {
              writer.write(line);
          }
      }

This is what i had tried before posting question here..But this doesn't seem to work. Now can somebody help me out..?

FarSh018
  • 845
  • 2
  • 10
  • 12

2 Answers2

2

You should write a JAVA program.

Here's something that may get you started (rough code):

BufferedReader br = new BufferedReader(new FileReader(file));

//start reading file line-by-line
while ((line = br.readLine()) != null) {

    //replace * with whatever you want
    // Use a counter to keep track of lines to give corresponding line number
    String val = line.replace("*",counterVar.toString());

}
br.close();

Write back to file using BufferedWriter again, wrap it with PrintWriter if you wish to,

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("outputfile")));
0

Try something like this (Untested code):

try{
       List<String> lines = Files.readAllLines(fileName, Charset.defaultCharset());
        for (int i = 0; i< lines.size(); i++) {
            lines.set(i, lines[i].replace("*", String.valueOf(i)));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
 ...//Then save the file
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130