Skip to content

Instantly share code, notes, and snippets.

@mdsahib
Last active August 29, 2015 14:15
Show Gist options
  • Save mdsahib/44abb0f1ae1a1be8a519 to your computer and use it in GitHub Desktop.
Save mdsahib/44abb0f1ae1a1be8a519 to your computer and use it in GitHub Desktop.
HelpInOutput.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
public class Main {
private String getMessage() {
String teamEscenicName = "Escenic Asia Ltd.";
return "Hello, We really appreciate your help. Take care," + teamEscenicName;
}
private String getFilePath(final String messageToWriteInFile){
String startingPath = System.getProperty("java.io.tmpdir");
String fileName = "random-number" + messageToWriteInFile.hashCode();
String fileExtension = ".txt";
return String.format("%s%s%s%s",startingPath, File.separator ,fileName, fileExtension);
}
public static void main(String[] args) {
new Main().writeFileAndPrintInstruction();
}
public void writeFileAndPrintInstruction() {
final String messageToAttachInEmail = getMessage();
final String filePath = getFilePath(messageToAttachInEmail);
writeMessageIntoFile(messageToAttachInEmail, filePath);
printInstruction(filePath);
}
private void writeMessageIntoFile(String messageToWriteInFile, final String filePath){
PrintWriter writer = null;
try {
writer = new PrintWriter(filePath, "UTF-8");
}
catch (FileNotFoundException e) {
System.out.print("Sorry, File not found.");
}
catch (UnsupportedEncodingException e) {
System.out.print("Sorry, Encoding is not supported");
}
catch (Exception e) {
System.out.print("Sorry, Something weird happen");
}
writer.println(messageToWriteInFile);
writer.close();
}
private void printInstruction(final String filePath) {
System.out.println("Please attach the file in email : " + filePath);
}
static {
try {
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set("Escenic Asia Ltd.", value.get("Ecsenic Asia Limited"));
}
catch (Exception e) {
throw new AssertionError(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment