In: Computer Science
Part 1 readFile(String filename)
In this method you are passed a String with the name of a file. This method will read the file in line by line and store each line in a String array. This String array is then returned. An example is shown below.
File Contents: Purple Rain by Prince I never meant to cause you any sorrow I never meant to cause you any pain I only wanted one time to see you laughing I only want to see you laughing in the purple rain String Array Contents: [0]: Purple Rain by Prince [1]: I never meant to cause you any sorrow [2]: I never meant to cause you any pain [3]: I only wanted one time to see you laughing [4]: I only want to see you laughing in the purple rain
In order to do this, you will need:
Assume that the String array holds a max of 1000 elements. Notice that there are three lines of code written at the bottom. DO NOT MODIFY THESE LINES.This code is copying your String array into a new array with the same amount of elements as there are lines in the file. Since this code is written for you, it assumes that your String array is named lines and that the int that keeps track of the amount of lines is named lineCounter. You will loop through and parse this file with the use of the Scanner and File Object and store each line in the String array while also counting each line.
Hints
Part 2 reverseFile(String[] parsedFile, String filename)
This method takes a String array that has lines of a file in it and a filename. This method will write the contents of the String array in reverse order to the file passed. The writing will be done with a PrintWriter. Here is an example.
String Array Content: [0]: Purple Rain by Prince [1]: I never meant to cause you any sorrow [2]: I never meant to cause you any pain [3]: I only wanted one time to see you laughing [4]: I only want to see you laughing in the purple rain Files Contents: I only want to see you laughing in the purple rain I only wanted one time to see you laughing I never meant to cause you any pain I never meant to cause you any sorrow Purple Rain by Prince
Hint
Part 3 logFile(String[] parsedFile, String filename)
This method takes a String array that has lines of a file in it and a filename. This method will write contents of the String array to a file. However, It will only write the lines that contain “LOG”. Other lines will be skipped over. Here is an example.
String Array Content: [0]: LOG username: happy_cat31 [1]: password: MrMuffins1234 [2]: LOG username: sad_panda [3]: password: BambooLover83 Files Contents: LOG username: happy_cat31 LOG username: sad_panda
Hints
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileIO {
//Part 1
public static String[] readFile(String filename) {
//Student Code Here
//3 lines below given to students. DO NOT MODIFY
String[] rtn = new String[lineCounter];
System.arraycopy(lines, 0, rtn, 0, lineCounter);
return rtn;
}
//Part 2
public static void reverseFile(String[] parsedFile, String
filename) {
//Student Code Here
}
//Part 3
public static void logFile(String[] parsedFile, String filename)
{
//Student Code Here
}
public static void main(String[] args) {
}
}
Short Summary:
Implemented the program as per requirement
Attached source code and sample output
**************Please do upvote to appreciate our time. Thank you!******************
Source Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class FileIO {
//Part 1
public static String[] readFile(String filename) {
//Student Code Here
int lineCounter=0;
//Create a initial string
array
String[] lines = new
String[1000];
Scanner input;
try {
//Read file
using scanner
input = new
Scanner(new File(filename));
//Iterate
until file has a line
while(input.hasNext())
{
lines[lineCounter]=input.nextLine();
lineCounter++;
}
//3 lines below
given to students. DO NOT MODIFY
String[] rtn =
new String[lineCounter];
System.arraycopy(lines, 0, rtn, 0, lineCounter);
input.close();
return
rtn;
} catch (FileNotFoundException e)
{
// TODO
Auto-generated catch block
e.printStackTrace();
}
return null;
}
//Part 2
public static void reverseFile(String[] parsedFile,
String filename) {
//Student Code Here
try {
//Create a
Writer object
PrintWriter
writer=new PrintWriter(new File(filename));
for(int
i=parsedFile.length-1;i>=0;i--)
{
writer.write(parsedFile[i]+"\n");
}
writer.close();
} catch (FileNotFoundException
e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
//Part 3
public static void logFile(String[] parsedFile, String
filename) {
//Student Code Here
try {
PrintWriter
writer=new PrintWriter(new File(filename));
for(int
i=0;i<parsedFile.length;i++)
{
if(parsedFile[i].contains("LOG"))
writer.write(parsedFile[i]+"\n");
}
writer.close();
} catch (FileNotFoundException
e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
//Provide file path accordingly
String []
array=readFile("C:\\Users\\asus\\InputText.txt");
String[] parsedArray= {"LOG
username: happy_cat31","password: MrMuffins1234","LOG username:
sad_panda","password: BambooLover83"};
//Provide file path accordingly
reverseFile(array,"C:\\Users\\asus\\OutputText.txt");
//Provide file path accordingly
logFile(parsedArray,
"C:\\Users\\asus\\OutputLog.txt");
}
}
Output:
InputFile:
Reverse Output:
LogOutput:
**************Please do upvote to appreciate our time. Thank you!******************