Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.util.*;
- import javax.swing.*;
- import java.lang.Number;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- /**
- *
- * CSCU9T4 Java strings and files exercise.
- *
- */
- public class FilesInOut {
- public static void main(String[] args) throws IOException {
- // Replace this with statements to set the file name (input) and file name (output).
- // Initially it will be easier to hardcode suitable file names.
- // Set up a new Scanner to read the input file.
- // Processing line by line would be sensible here.
- // Initially, echo the text to System.out to check you are reading correctly.
- // Then add code to modify the text to the output format.
- // Set up a new PrintWriter to write the output file.
- // Add suitable code into the above processing (because you need to do this line by line also.
- // That is, read a line, write a line, loop.
- // Finally, add code to read the filenames as arguments from the command line.
- // String inFileName = "input.txt";
- // String outFileName = "output.txt";
- String cmdInFileName = args[0];
- String cmdOutFileName = args[1];
- boolean uppercase = false;
- for (int i = 0; i < args.length; i++) {
- if (args[i].equals("-u")) {
- uppercase = true;
- }
- }
- readWriteFile(cmdInFileName, cmdOutFileName, uppercase);
- } // main
- public static void readWriteFile(String inFileName, String outFileName, boolean uppercase) {
- try {
- File input = new File("C:\\Users\\marks\\eclipse-workspace\\T4Prac4\\" + inFileName); // Set input file
- File output = new File("C:\\Users\\marks\\eclipse-workspace\\T4Prac4\\"+ outFileName); // Set output file
- Scanner scan = new Scanner(input); // Declare Scanner
- PrintWriter printer = new PrintWriter(output); // Declare printWriter
- while(scan.hasNextLine()) { // Loop while text exists in file
- String s = scan.nextLine(); // Scan next line
- String[] splitStrings = s.split(" ", 4);
- String finalOutput = "";
- if (splitStrings.length < 4) {
- if (uppercase) {
- finalOutput = formattedOutputUpperCase3(splitStrings[0], splitStrings[1], splitStrings[2]);
- }
- else finalOutput = formattedOutput3(splitStrings[0], splitStrings[1], splitStrings[2]);
- }
- else {
- if (uppercase) {
- finalOutput = formattedOutputUpperCase(splitStrings[0], splitStrings[1], splitStrings[2], splitStrings[3]);
- }
- else finalOutput = formattedOutput(splitStrings[0], splitStrings[1], splitStrings[2], splitStrings[3]);
- }
- System.out.println(finalOutput); // Print out to console
- printer.write(finalOutput + "\n"); // Format
- }
- scan.close();
- printer.close();
- }
- catch (FileNotFoundException e) {
- System.err.println("File not found");
- }
- } // readFile
- public static String capitalise(String str) {
- if(str == null || str.isEmpty()) { // Check there is a string
- return str;
- }
- return str.substring(0, 1).toUpperCase() + str.substring(1); // Convert first letter of string to Uppercase
- }
- public static String formatDate(String date) {
- SimpleDateFormat inputDate = new SimpleDateFormat("ddmmyyyy"); // Inputed format
- SimpleDateFormat newFormat = new SimpleDateFormat("dd/mm/yyyy");// New format
- String reformattedStr = "";
- try {
- reformattedStr = newFormat.format(inputDate.parse(date)); // Format the String
- }
- catch (ParseException e) {
- e.printStackTrace();
- }
- return reformattedStr; // return
- }
- public static String formattedOutput(String firstWord, String middleName, String secondWord, String date) {
- String finalOutput = capitalise(firstWord) + " " + capitalise(middleName) + ". " + capitalise(secondWord) + "\t\t " + formatDate(date);
- return finalOutput;
- }
- public static String formattedOutputUpperCase(String firstWord, String middleName, String secondWord, String date) {
- String finalOutput = firstWord.toUpperCase() + " " + middleName.toUpperCase() + ". " + secondWord.toUpperCase() + "\t\t " + formatDate(date);
- return finalOutput;
- }
- public static String formattedOutput3(String firstWord, String secondWord, String date) {
- String finalOutput = capitalise(firstWord) + " " + capitalise(secondWord) + "\t\t " + formatDate(date);
- return finalOutput;
- }
- public static String formattedOutputUpperCase3(String firstWord, String secondWord, String date) {
- String finalOutput = firstWord.toUpperCase() + " " + secondWord.toUpperCase() + "\t\t " + formatDate(date);
- return finalOutput;
- }
- } // FilesInOut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement