The program needs to read in a text file and display the contents of the file to
ID: 3535744 • Letter: T
Question
The program needs to read in a text file and display the contents of the file to the screen output. The file will
contain words separated by spaces, lines terminated by the newline character(s), and
paragraphs separated by a blank line (i.e., a line with zero (0) characters in it).
Your task is to write the program Format which will take a single command line argument to
specify the width of the output line. The name of the file to be formatted will be entered as
redirected standard input. The output will be displayed to the standard output (i.e., the screen).
To read from a disk file you will need to import the classes java.util.Scanner.
To create a Scanner class object instance for the redirected standard input (the file to be
formatted) include the following lines:
Scanner input = new Scanner(System.in);
Your program will read from the scanner input a line at a time. For each line read, if the line
has a zero (0) length then just display a blank line. Otherwise, the line will contain words.
Create a new Scanner object instance whose constructor takes the name of the String that
contains the line you just read in. You can then pick up each word using the next() method.
You will need a StringBuilder object instance to hold the output line that you are building up.
You then append words to the end of the output StringBuilder. Remember to add a single
blank space between each word (but not before the first word in the output StringBuilder).
Before adding a word you should check that the current length of the string plus the length of
the new word and the blank space does not exceed the output line width that the user specified
at the beginning of the execution of the program. If the new word will cause the output string to
become too long then the unaugmented line will be displayed, the output line StringBuilder
should be cleared (reset to have 0 length) and the new word added to the empty string. Be
aware that when the file is completely read there might be some words left in the output
StringBuilder. These should also be displayed to the screen. Close all scanners when they
have completed their tasks.
You should include some statistics gathering and display to your program. Keep track of the
number of lines and words read in as well as the number of lines and words displayed.
I am including two text files with this project. The first is the text of the Declaration of
Independence (DoI.txt). This file will serve as the input file to the formatting program. The
second is the result of running the program (DoI.fmt). You should use these files to guide you
in developing the program.
Explanation / Answer
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class ReadWriteTextFileJDK7 {
public static void main(String... aArgs) throws IOException{
ReadWriteTextFileJDK7 text = new ReadWriteTextFileJDK7();
//treat as a small file
List<String> lines = text.readSmallTextFile(FILE_NAME);
log(lines);
lines.add("This is a line added in code.");
text.writeSmallTextFile(lines, FILE_NAME);
//treat as a large file - use some buffering
text.readLargerTextFile(FILE_NAME);
lines = Arrays.asList("Down to the Waterline", "Water of Love");
text.writeLargerTextFile(OUTPUT_FILE_NAME, lines);
}
final static String FILE_NAME = "C:\Temp\input.txt";
final static String OUTPUT_FILE_NAME = "C:\Temp\output.txt";
final static Charset ENCODING = StandardCharsets.UTF_8;
//For smaller files
List<String> readSmallTextFile(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
return Files.readAllLines(path, ENCODING);
}
void writeSmallTextFile(List<String> aLines, String aFileName) throws IOException {
Path path = Paths.get(aFileName);
Files.write(path, aLines, ENCODING);
}
//For larger files
void readLargerTextFile(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
try (Scanner scanner = new Scanner(path, ENCODING.name())){
while (scanner.hasNextLine()){
//process each line in some way
log(scanner.nextLine());
}
}
}
void readLargerTextFileAlternate(String aFileName) throws IOException {
Path path = Paths.get(aFileName);
try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)){
String line = null;
while ((line = reader.readLine()) != null) {
//process each line in some way
log(line);
}
}
}
void writeLargerTextFile(String aFileName, List<String> aLines) throws IOException {
Path path = Paths.get(aFileName);
try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)){
for(String line : aLines){
writer.write(line);
writer.newLine();
}
}
}
private static void log(Object aMsg){
System.out.println(String.valueOf(aMsg));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.