-1

Possible Duplicate:
Replace all occurrences of substring in a string - which is more efficient in Java?

I'm trying to replace all occurrences of \t (tab) with four spaces in a file. I've been searching for a while but couldn't find anything useful.

I found out that I need to create a temporary file.

Community
  • 1
  • 1
Fuze
  • 423
  • 1
  • 6
  • 11

2 Answers2

5

there is a function for this called replace

String output = input.replace("\t","    ");

to do this with a file create a temp file and open a FileWriter for it

open a FileReader for the original file

the in a loop call readln(), check for null, replace("\t"," ") and write on the appropriate objects

then close the reader and writer and delete() the original file and rename() the temp file to the original file

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
1

You can use replaceAll method like

String str = "your string";
String a = "    "; // [4 spaces]
String result = str.replaceAll("\t", a);
Martin Tuskevicius
  • 2,590
  • 4
  • 29
  • 46
Arpssss
  • 3,850
  • 6
  • 36
  • 80