0

i have a File with some character and i have some entities like & è à now i want to add instead of & & amp; and so on..how i can do that? If i have Oggi è lunedi & domani è mercoledi i want that è is replaced by (& grave;) and & is replaced by (& amp;) I have this code but works only on one identity at a time...

 public void genereateFile(String char_speciale, String char_sostituto, String url_scheletro_file, String url_nuovo_file){
            try{
                BufferedReader rdr = new BufferedReader(new InputStreamReader(new FileInputStream(url_scheletro_file)));
                BufferedWriter wrt = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url_nuovo_file)));
                String line=rdr.readLine();
                while(line!= null){
                    line=line.replaceAll(char_speciale, char_sostituto);
                    wrt.append(line);
                    wrt.newLine();
                    line = rdr.readLine();
                }
                wrt.close();
            }catch(Exception e){
                e.printStackTrace();
            }
  • 2
    please add an example of what your sample input would look like and the output you want to generate. – Yogesh_D Apr 05 '16 at 09:37
  • using regular expression like below : to replace all '&' and 'à ' with a '_' String new_s = s.toLowerCase().replaceAll("[&à]", "_"); replaceAll is using regular expressions, and using . inside a character class [ ] just recognises a . rather than any character. – Ahmed Gamal Apr 05 '16 at 09:49
  • Your question is not clear. If you are trying to escape/unescape HTML character, maybe this helps http://stackoverflow.com/questions/14998726/replace-html-codes-with-equivalent-characters-in-java Solution provided use https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html – RubioRic Apr 05 '16 at 09:52

3 Answers3

0

Your question is not clear but if you want to replace all 'è' and 'à' with '&'

Use regular expression to do that so call your method as

genereateFile("[èà]", "&", url_scheletro_file, url_nuovo_file);
Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
0

using regular expression like below : to replace all '&' and 'à ' with a '' String

 String new_s = line.toLowerCase().replaceAll("[èà]", "&");

replaceAll is using regular expressions, and using . inside a character class [ ] just recognises a . rather than any character.

EDIT:

 public void genereateFile( String char_sostituto, String url_scheletro_file, String url_nuovo_file){
            try{
                BufferedReader rdr = new BufferedReader(new InputStreamReader(new FileInputStream(url_scheletro_file)));
                BufferedWriter wrt = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(url_nuovo_file)));
                String line=rdr.readLine();
                while(line!= null){
                    line=line.toLowerCase().replaceAll("[èà]", char_sostituto);

                    wrt.append(line);
                    wrt.newLine();
                    line = rdr.readLine();
                }
                wrt.close();
            }catch(Exception e){
                e.printStackTrace();
            }
Ahmed Gamal
  • 1,666
  • 1
  • 17
  • 25
0

You can use replaceAll(regex, replacement); the first argument is the regular expression and if it matches it replace it with the replacement provided.

Check Docs

johndoe
  • 4,387
  • 2
  • 25
  • 40