-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEasyLib2.java
More file actions
280 lines (269 loc) · 7.64 KB
/
EasyLib2.java
File metadata and controls
280 lines (269 loc) · 7.64 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package test;
import java.awt.MouseInfo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Random;
/**<h1>EasyLib2</h1>
* <p> Instantiating this class allows the user to easily do tasks such as get the computers screen width and height, write to log files, print to the console and many more
* unique features!
*
* @since 22/4/2016
*/
public class EasyLib2 {
private static Random r = new Random();
String fileName;
String fileLocation;
/**
* <h1>Hello, World!</h1>
* The HelloWorld program implements an application that
* simply displays "Hello World!" to the standard output.
* <p>
* Basic hello world function to start with(if you are a new programmer!)
*
* @param none
*/
public void HelloWorld(){
System.out.println("Hello World!");
}
/** <p>
* Simple print method that works for (virtually)every thing you'd need to print.
*
* @param none
*/
public void print(Object o){
System.out.print(o);
}
/** <p>
* Simple println method that works for (virtually)every thing you'd need to print.
*
* @param none
*/
public void println(Object o){
System.out.println(o);
}
/** <p>
* Creates a log file in a given location.
*
* @param logName The name of the log file you want to create.
* @param location The location in which you would like the log file to be created.
* @param hasBranding Allows the programmer to give me credit or not when writing a log file.
*/
public void CreateLog(String logName, String location, boolean hasBranding) throws IOException{
File file = new File(location.concat(logName));
PrintWriter writer = new PrintWriter(location.concat(logName), "UTF-8");
fileName = logName;
fileLocation = location;
if(!file.exists()){
file.createNewFile();
}if(hasBranding){
writer.println("A text file generated by EasyLib2");
}
writer.close();
}
/** <p>
* Writes to a specified log file, if none exists this function will create one.
*
* @param logName The name of the log file.
* @param location The location in which you would like the log file to be written to.
* @param phraseToWrite The phrase or sentence the user wants to write into the log file.
*/
public void WriteToLog(String logName, String location, boolean onNewLine, String phraseToWrite) throws IOException{
File file = new File(location.concat(logName));
PrintWriter writer = new PrintWriter(new FileWriter(location.concat(logName), true));
if(!file.exists()){
CreateLog(logName, location, false);
}
if(onNewLine) writer.println(phraseToWrite);
else
writer.print(phraseToWrite);
writer.close();
}
/**<p>
* Returns the contents of the specified line.
* @param logName the name of the log file
* @param location the location of the log file
* @param specificLine a specific line in a log file.
*/
public String ReadLog(String logName, String location, int specificLine) throws IOException{
FileReader reader = new FileReader(location.concat(logName));
@SuppressWarnings("resource")
BufferedReader bReader = new BufferedReader(reader);
for(int i = 0; i < specificLine; i++){
bReader.readLine();
}
String line = bReader.readLine();
return line;
}
/**<p>
* Returns the username as an actual username, instead of recieveing /Users/USERNAME, you will get USERNAME.
*/
public String GetComputerName(){
String computerName = new String(System.getProperty("user.home"));
String last = "";
for(int i = 1; i < computerName.length(); i++){
if(computerName.charAt(i) == '/'){
for(int x = i + 1; x < computerName.length(); x++){
last += computerName.charAt(x);
}
}
}
return last;
}
/**<p>
* Gets the directory of the previously given log file.
*/
public String GetDIR(String logName){
File file = new File(fileLocation.concat(logName));
if(!file.exists()) return "This file does not has not been previously created by EasyLib2.";
return file.getAbsolutePath();
}
/** <p>
* Deletes the specified log files contents.
*
* @param logName The name of the log file you want to clear.
* @param location The location in which the log file is to be cleared.
*/
public void ClearLog(String logName, String location) throws IOException{
PrintWriter writer = new PrintWriter(location.concat(logName));
writer.flush();
writer.close();
}
/** <p>
* Deletes the specified file.
*
* @param logName The name of the log file you want to clear.
* @param location The location in which the log file is to be cleared.
*/
public void DeleteLog(String logName, String location){
File file = new File(location.concat(logName));
if(file.delete()){
return;
}else{
println(file.getName() + " was not deleted.");
}
}
public boolean doesFileExist(String logName, String location){
File file = new File(location.concat(logName));
return file.exists();
}
/** <p>
* Generates a random string.
*
* @param numOfLetters The amount of letters the user would like to generate.
*
*/
public String RandomString(int numOfLetters){ //Generates a random string
char[] chars = new char[numOfLetters];
for(int i = 0; i < numOfLetters;){
int rand = r.nextInt(90);
if(rand > 65){
chars[i] = (char)rand;
i++;
}
}
String str = new String(chars);
return str;
}
/** <p>
* Generates a random number.
*
* @param timesToLoop The amount of times the function will loop.
*
*/
public int RandomNumber(int timesToLoop){
int rand = 0;
for(int i = 0; i < timesToLoop; i++){
rand += r.nextInt(99);
}
return rand;
}
/** <p>
* Encrypts a given string, can be decrypted.
*
* @param stringToEncrypt The string you would like to encrypt.
*/
public String EncryptString(String stringToEncrypt){ //Encrypts a given string, can be decrypted with a custom decrypter below.
char[] strChar = stringToEncrypt.toCharArray();
for(int i = 0; i < strChar.length; i++) {
if(i == 0){
strChar[i] = (char)((int)strChar[i] - (i + 1));
continue;
}
strChar[i] = (char)((int)strChar[i] + i);
}
String string = new String(strChar);
return string;
}
/** <p>
* Decrypts a given string, most first be encrypyed by using the encrypter above.
*
* @param stringToDecrypt The string to decrypt.
*/
public String DecryptString(String stringToDecrypt){ //TODO: Reverse the loop, subtracting every value in the array, ending up w/ the original numbers, then translatied to a char
char[] strChar = stringToDecrypt.toCharArray();
for(int i = 0; i < strChar.length; i++){
if(i == 0){
strChar[i] = (char)((int)strChar[i] - i + 1);
continue;
}
strChar[i] = (char)((int)strChar[i] - i);
}
String string = new String(strChar);
return string;
}
/** <p>
* Returns the current OS.
*
*
* @param none
*/
public String GetOS(){
return System.getProperty("os.name");
}
/** <p>
* Returns if the give number is even.
*
* @param n The given number.
*/
public boolean IsEven(int n){
return n % 2 == 0;
}
/** <p>
* Returns if the give number is odd.
*
* @param n The given number.
*/
public boolean IsOdd(int n){
if(n % 2 != 0){
return true;
}
return false;
}
/** <p>
* Returns the current time, can cause depreciation unfortunately..
*
* @param none
**/
@SuppressWarnings("deprecation")
public String GetCurrentTime(){
Date date = new Date();
return date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}
/**<p>
* Returns mouse X position.
*/
public int GetMouseX(){
return (int)MouseInfo.getPointerInfo().getLocation().getX();
}
/**<p>
* Returns mouse X position.
*/
public int GetMouseY(){
return (int)MouseInfo.getPointerInfo().getLocation().getY();
}
}