Create an application that reads a file of data, changes the Strings to all uppe
ID: 3701303 • Letter: C
Question
Create an application that reads a file of data, changes the Strings to all uppercase, and prints the data to a new file. For an optional advanced challenge, instead of changing the Strings to uppercase, sort the data by inserting it into a TreeMap. Make sure all checked exceptions are caught and the stacktrace printed on an exception. Use the refactor tool in Eclipse to break the logic into small meaningful groups for clarity.
Create a new project.
Save the sample data attached to this assignment: data.dat.
Process the data from the file using a FileReader
Open the file
Read the contents of the file, line by line.
Change each line as it is read by converting the Strings to uppercase.
Insert each line as an entry into an array or collection.
There are no more than 25 items in the data file.
Optionally, use a collection that sorts the data automatically.
Write the contents of the file to a new file called processed.dat.
Use a FileWriter to create the file.
Wrap the FileWriter in a BufferedWriter for performance.
Write each data from the collection to the file, looping through all of the entries.
As required for all assignments from now on
validate the proper functionality using the example answers below and review the Concepts and Ideas checklist format the code
add a comment at the top of the file that reads @ author and your name
build the javadocs
Verify that the jar file contains source and class files and the MANIFEST.MF file contains a Main-Class. See the Resources section for Configuring Eclipse to view Jar files.
Run the application from the jar file, either from the command line using java -jar jarfilename or from Eclipse. See the Resources section for Configuring Eclipse to run Jar files.
submit the jar file in the Assignment section of BlackBoard under View/Complete Assignment for this assignment.
Example:
This is the content of the data.dat file before processing:
Alpha
Beta
Gamma
Delta
Epsilon
Zeta
Eta
Theta
Iota
Kappa
Lambda
Mu
Nu
Xi
Omikron
Pi
Rho
Sigma
Tau
Upsilon
Phi
Chi
Psi
Omega
This is the content of the processed.dat file after processing:
ALPHA
BETA
GAMMA
DELTA
EPSILON
ZETA
ETA
THETA
IOTA
KAPPA
LAMBDA
MU
NU
XI
OMIKRON
PI
RHO
SIGMA
TAU
UPSILON
PHI
CHI
PSI
OMEGA
This is the content of the processed.dat file after processing with optional sorting:
ALPHA
BETA
CHI
DELTA
EPSILON
ETA
GAMMA
IOTA
KAPPA
LAMBDA
MU
NU
OMEGA
OMIKRON
PHI
PI
PSI
RHO
SIGMA
TAU
THETA
UPSILON
XI
ZETA
Explanation / Answer
I have create the project and also unit tested , please see class file which have the funationlity
package chegg.processdatfile;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
*
* @author :""
*/
public class clsDatFileReader {
//String inputFiles = "E:\data\data.dat";
//String OutPutFile = "E:\data\processed.dat";
public List<String> readFile(String inputFile) {
List<String> records = new ArrayList<String>();
try {
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
String line;
while ((line = reader.readLine()) != null) {
records.add(line.toUpperCase());
}
reader.close();
return records;
} catch (Exception e) {
System.err.format("Exception occurred trying to read '%s'.", inputFile);
e.printStackTrace();
return null;
}
}
public void writeFile(List<String> lstString, String strOutPutFile) {
BufferedWriter bw = null;
FileWriter fw = null;
// sort in acending order
Collections.sort(lstString);
try {
fw = new FileWriter(strOutPutFile);
bw = new BufferedWriter(fw);
String content = "";
for (String strVal : lstString) {
content = strVal ;
bw.write(content);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
To test the project use below code :
package chegg.processdatfile;
import java.util.List;
public class CheggProcessDatfile {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String inputFile = "E:\data\data.dat";
String OutPutFile = "E:\data\processed.dat";
clsDatFileReader obj = new clsDatFileReader();
List<String> lstArray= obj.readFile(inputFile);
obj.writeFile(lstArray,OutPutFile);
}
}
You can create java doc of your own ..like below or google you will easly get it. I can not attach java doc because , there is no option to attach
Select Project –> Generate JavaDoc
Best of luck !!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.