Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.sql.Timestamp;
- import java.util.Scanner;
- public class sender {
- int user = 0;
- String userString = "";
- String[] errors = new String[1];
- int i = 0;
- File textDataFile = new File("TextData.txt");
- File userSettingsFile = new File("Settings.txt");
- String message = "";
- String subject = "";
- // Define the time and date
- java.util.Date date = new java.util.Date();
- // Used to prompt for user input
- InputStreamReader istream = new InputStreamReader(System.in);
- BufferedReader bufRead = new BufferedReader(istream);
- public boolean checkForFile() {
- if (!textDataFile.exists()) {
- errors[0] = ("We were not able to find the user data file in the current directory.");
- return false;
- }
- return true;
- }
- public void createTextData() {
- // If there are errors, print them out
- if (errors[0] != null) {
- if (errors[0] != null) {
- System.out.println(errors[i]);
- }
- System.out
- .println("\nWould you like to attempt to resolve this issue? (Strike the \"f\" key to fix): ");
- Scanner sc = new Scanner(System.in);
- String a = sc.next();
- if (a.equals("f")) {
- System.out.println("Please wait...");
- // progressBar.setValue(50);
- try {
- // Create file
- FileWriter fstream = new FileWriter("TextData.txt");
- BufferedWriter out = new BufferedWriter(fstream);
- // Close the output stream
- out.close();
- } catch (Exception e) {// Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- if (textDataFile.exists()) {
- System.out.println("The file was created!");
- } else {
- System.out
- .println("The file does not exist. There were errors. Please contact us.");
- }
- } else {
- System.out.println("Program terminated by user.");
- System.exit(0);
- }
- }
- }
- public void sendMessage() {
- try {
- // Subject
- System.out.println("Subject: ");
- subject = bufRead.readLine();
- while (subject.isEmpty()) {
- System.out
- .println("Please fill in this field before continuing!");
- System.out.println("Subject: ");
- subject = bufRead.readLine();
- }
- // Message
- System.out.println("Message: ");
- message = bufRead.readLine();
- while (message.isEmpty()) {
- System.out
- .println("Please fill in this field before continuing!");
- System.out.println("Message: ");
- message = bufRead.readLine();
- }
- // Confirmation of text sent
- System.out
- .println("\n------------------------\n\nSummary of message sent:\nSubject: "
- + subject + "\nMessage: " + message);
- } catch (IOException err) {
- System.out.println("Error reading line(s).");
- }
- }
- public void writeToFile() {
- try {
- BufferedWriter out = new BufferedWriter(new FileWriter(
- "TextData.txt", true));
- // Get the current time/date
- Object timenDate = new Timestamp(date.getTime());
- // Convert the date/time object to a string
- String dateNTime = timenDate.toString();
- out.write("\r\n" + user + "Ø" + dateNTime + "Ø");
- out.write(subject + "Ø" + message);
- out.close();
- } catch (Exception e) {
- System.err.println("\nError: " + e.getMessage());
- }
- }
- public int getUser() {
- // Get the user #
- if (userSettingsFile.exists()) {
- try {
- Scanner scanner = new Scanner(userSettingsFile);
- while (scanner.hasNextLine()) {
- userString = scanner.nextLine();
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- // Set the user #
- if (userString.equals("1")) {
- user = 1;
- } else if (userString.equals("2")) {
- user = 2;
- }
- return user;
- }
- public static void main(String[] args0) {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement