Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Lab 09 – OOP I

Input/Output Page 1

In this lab, your will be developing a command line application to filter a text file by
deleting the words specified by the user via the command line. The lab. is composed of
several phases;

1. Passing parameters: It allows the user to specify via the command line the following
parameters:
 The path of the file to be processed.
 The words to deleted.

For example, to filter the content of the file my_file.txt by deleting all the pronouns,
the user must write the following command;

filter my_file.txt he she it this

2. Read File Content: in Java there are several methods that can be used to read the file
content. We list some of them below:
a. Using the methods in Paths, Files and StandardCharsets classes in the
package nio JDK 7.
Path p = Paths.get(file_path);
List<String> lines = Files.readAllLines(p, StandardCharsets.UTF_8);

b. Using the methods in class Scanner


File f = new File(file_path);
Scanner sc = new Scanner(f, StandardCharsets.UTF_8.name());
while (sc.hasNextLine())
System.out.println(sc.nextLine());

c. Using FileReader and BufferedReader


FileReader fReader = new FileReader(file_path);
BufferedReader reader= new BufferedReader(fReader);
String line;
while ((line=reader.readLine())!=null)
System.out.println(line);

d. Using the Files.lines() in JDK8, which returns a Stream<String>


Path p = Paths.get(file_path);
Stream<String> lines = Files.lines(p);
Lab 09 – OOP I
Input/Output Page 2

e. Using the Files.newBufferedReader () in JDK8, which returns a


Stream<String>

Path p = Paths.get(file_path);
BufferedReader reader = Files.newBufferedReader(p);
Stream<String> lines = reader.lines();

3. Filter file content: using the method replaceAll in class String or the method
filter in the Stream.
4. Write content to a file: in Java there are several methods that allow you to write data
into a file;
a. Using the methods in the Paths, Files and StandardCharsets classes
in JDK 7
b. Using the methods in the BufferedWriter class
Path p = Paths.get(file_path);
BufferedWriter writer = Files.newBufferedWriter(p)
writer.write(line)

No Luck Just Skill!

You might also like