forked from saemundur-haraldsson/CSCUT4Practical2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FilesInOut.java
194 lines (175 loc) · 5.3 KB
/
FilesInOut.java
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
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import javax.swing.*;
import java.lang.Number;
import java.io.File;
/**
*
* CSCU9T4 Java strings and files exercise.
*
*/
public class FilesInOut {
//scenario tells us to output in all caps or not
public static boolean scenario = false;
public static void main(String[] args)
{
//file locations
String fileIn = "";
String fileOut = "";
Scanner sc = new Scanner(System.in);
//checking through args for flags
int argI = 0;
if (args[0].equals("-u"))
{
argI = 1;
scenario = true;
}
//checking if file locations are valid
if (args[argI].equals(null))
{
System.out.println("No output file location input");
}
else if(checkFile(args[argI]))
{
fileIn = args[argI];
System.out.println("File successfully located");
}
else
{
System.out.println("Error: please makes sure you input the right input file name");
}
if (args[argI+1].equals(null))
{
System.out.println("No output file location input");
}
else if(checkFile(args[argI+1]))
{
fileOut = args[argI+1];
System.out.println("File successfully located");
}
else
{
System.out.println("Error: please makes sure you input the right output file name");
}
//update file
updateFile(fileIn, fileOut);
}
// main
public static Boolean checkFile(String inputFileName)
{
try
{
File inputFile = new File(inputFileName);
Scanner inFile = new Scanner (inputFile);
}
catch (IOException e)
{
System.err.println("IOException: " + e.getMessage() + "not found");
return false;
}
return true;
}
//checkInputFile
//scanner for inputfile
public static void updateFile(String inputLoc, String outputLoc)
{
File inputFile = new File(inputLoc);
try (Scanner sc = new Scanner(inputFile, StandardCharsets.UTF_8.name()))
{
while(sc.hasNextLine())
{
updateFiledWrite(outputLoc,sc.nextLine());
}
}
catch(IOException e)
{
e.printStackTrace();;
}
}
//updateFile
//writer for outputFile
public static void updateFiledWrite(String outputloc, String input)
{
File outputFile = new File(outputloc);
try (FileWriter fw = new FileWriter(outputFile, true))
{
PrintWriter pw = new PrintWriter(fw);
pw.write(updateString(input) + "\r\n");
pw.close();
}
catch(IOException e)
{
e.printStackTrace();;
}
}
//UpdateFileWrite
//Method to change line from input file
public static String updateString(String input)
{
String update = "";
StringBuffer change = new StringBuffer(input);
boolean m = false;
for(int i = 0; i < input.length(); i ++)
{
//To format date
if (tryParseInt(change.substring(i)))
{
if (m)
{
update = input.substring(i-1, i+1) + "/" + input.substring(i+2 , i+4) + "/" + input.substring(i+3, input.length());
change.replace(i, input.length()+1, update);
break;
}
else
{
update = input.substring(i, i + 2) + "/" + input.substring(i + 3, i + 5) + "/" + input.substring(i + 4);
change.replace(i, input.length(), update);
break;
}
}
//to upper case
else
{
if( i == 0 || Character.isWhitespace(change.charAt((i-1))))
{
if(Character.isWhitespace(change.charAt((i+1))))
{
update = Character.toUpperCase(input.charAt(i)) + ".";
change.replace(i, i+1, update);
m = true;
}
else
{
char temp = Character.toUpperCase(change.charAt(i));
change.replace(i, i + 1, Character.toString(temp));
}
}
}
}
//checking for flags
if (scenario)
{
return change.toString().toUpperCase();
}
else
return change.toString();
}
//updateString
//method to check if string is number or not
public static boolean tryParseInt(String intChar)
{
try
{
Integer.parseInt(intChar);
return true;
}
catch (NumberFormatException e)
{
return false;
}
}
//tryParseInt
} // FilesInOut