Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Groovy character encoding in replacing text in file

Featured Replies

  • Newbies

Hi, I'm completely new to Groovy and am trying to replace the 3 versions of the double quotes with 2 single quotes in a large CSV file.  I've managed to get the script below working but the input file encoding is UTF8 and the output file comes out as "Western (Mac OS Roman)".

 

Any help would be greatly appreciated, Thanks

File myInFile = new File( myFilePathStart );

myOutFile = new File( myFilePathEnd );

myOutFile.withWriter { out ->

 myInFile.eachLine { line ->

  myOutFileText = line.replaceAll(""", "''");

  myOutFileText = myOutFileText.replaceAll("“", "''");

  myOutFileText = myOutFileText.replaceAll("”", "''");

  myOutFileText = myOutFileText + "n";

  out << myOutFileText;

 }

};

return true;

Solved by FrankPottner

Go to solution

myInFile = new File( myFilePathStart )
myOutFile = new File( myFilePathEnd )

try{
myOutFile.withWriter('UTF-8') { out ->
 myInFile.eachLine { line ->
  myOutFileText = line.replace(""", "''")
.replace("“", "''")
.replace("”", "''") + 'n'
  out << myOutFileText
 } //end eachLine
} //end writer
} catch (e){
 return e
}
return true

  • Author
  • Newbies
  • Solution

Thanks "enthusiast with a bit of geek"... that almost worked.  I just had to add the UTF8 encoding to the "myInFile.eachLine" command also.  The script now looks like the following and does exactly what I need;

 

 

myInFile = new File( myFilePathStart )
myOutFile = new File( myFilePathEnd )
 
try{
   myOutFile.withWriter('UTF-8') { out ->
      myInFile.eachLine('UTF-8') { line ->
         myOutFileText = line.replaceAll(""", "''")
         .replaceAll("“", "''")
         .replaceAll("”", "''") + "n"
        out << myOutFileText;
      } //end eachLine
   } //end writer
} catch (e) {
   return e
}
return true

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.