-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSaveTextToFile.groovy
More file actions
123 lines (97 loc) · 3.38 KB
/
SaveTextToFile.groovy
File metadata and controls
123 lines (97 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Purpose: Writes the provided text to a file with the requested encoding and EOL.
Returns: Text
Name: SaveTextToFile ( filePath ; theText ; theEncoding ; eolFormat )
Parameters: ( filePath ) filepath
( theText ) text
( theEncoding ) enum = (UTF-8, US-ASCII, LATIN-1...LATIN-9)
( eolFormat ) enum = (MAC (< Mac OS X), WIN, UNX (includes Mac OS X)
Dependencies: NONE
2011-08-31 JPS, Created.
NOTES:
Supported Encodings
8859_1 (ISO-8859-1/LATIN-1)
8859_2 (ISO-8859-2/LATIN-2)
8859_3 (ISO-8859-3/LATIN-3)
8859_4 (ISO-8859-4/LATIN-4)
8859_5 (ISO-8859-5/LATIN-5)
8859_6 (ISO-8859-6/LATIN-6)
8859_7 (ISO-8859-7/LATIN-7)
8859_8 (ISO-8859-8/LATIN-8)
8859_9 (ISO-8859-9/LATIN-9)
ASCII (7-bit ASCII)
UTF8 (UCS Transformation Format-8)
Supported EOL:
Mac, Windows, Linux/UNIX
*/
theEncoding = theEncoding.toUpperCase();
//Determine if the provided encoding is valid, otherwise return an error.
switch ( theEncoding ) {
case ["UTF8", "UTF-8"]:
theEncoding = "UTF-8";
break;
case ["US-ASCII", "ASCII"]:
theEncoding = "US-ASCII";
break;
case ["ISO-8859-1/LATIN-1", "ISO-8859-1", "8859-1", "8859_1", "LATIN 1", "LATIN-1"]:
theEncoding = "8859_1";
break;
case ["ISO-8859-2/LATIN-2", "ISO-8859-2","8859-2", "8859_2", "LATIN 2", "LATIN-2"]:
theEncoding = "8859_2";
break;
case ["ISO-8859-3/LATIN-3", "ISO-8859-3","8859-3", "8859_3", "LATIN 3", "LATIN-3"]:
theEncoding = "8859_3";
break;
case ["ISO-8859-4/LATIN-4", "ISO-8859-4","8859-4", "8859_4", "LATIN 4", "LATIN-4"]:
theEncoding = "8859_4";
break;
case ["ISO-8859-5/LATIN-5", "ISO-8859-5","8859-5", "8859_5", "LATIN 5", "LATIN-5"]:
theEncoding = "8859_5";
break;
case ["ISO-8859-6/LATIN-6", "ISO-8859-6","8859-6", "8859_6", "LATIN 6", "LATIN-6"]:
theEncoding = "8859_6";
break;
case ["ISO-8859-7/LATIN-7", "ISO-8859-7","8859-7", "8859_7", "LATIN 7", "LATIN-7"]:
theEncoding = "8859_7";
break;
case ["ISO-8859-5/LATIN-8", "ISO-8859-8","8859-8", "8859_8", "LATIN 8", "LATIN-8"]:
theEncoding = "8859_8";
break;
case ["ISO-8859-5/LATIN-9", "ISO-8859-9","8859-9", "8859_9", "LATIN 9", "LATIN-9"]:
theEncoding = "8859_9";
break;
default:
//Invalid Type provided
return false;
}
eolFormat = eolFormat.toUpperCase();
//Determine if the provided eolForm is valid, otherwise return false.
switch ( eolFormat ) {
case ["WIN", "WINDOWS", "CR+LF", "CRLF", "\r\n"]:
eolFormat = "\r\n";
break;
case ["MAC", "CR", "\r"]:
eolFormat = "\r";
break;
case ["UNIX", "UNX", "LINUX", "LNX", "MAC OS X", "\n"]:
eolFormat = "\n";
break;
default:
//Invalid Type provided
return false;
}
//The BufferedReader reads lines without the EOL, just append the selected EOL
BufferedReader br = new BufferedReader( new StringReader ( theText ));
StringBuilder sb = new StringBuilder();
for (;;) {
String line = br.readLine();
if (line == null)
break;
sb.append(line + eolFormat);
}
theText = sb.toString();
//This more advanced version allows you to specify the character encoding
OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream( filePath ), theEncoding );
writer.write( theText );
writer.close();
return true;