Question

Create a Java program that analyzes a program and produces the output into another file. The...

Create a Java program that analyzes a program and produces the output into another file. The program must retrieve the path to the .java file. Once the path to the file is found the program must figure out the length of the longest line is in the program. For example, working with a program called MainFile.java. Once it figures out the longest line in the code it will put the output into MainFile.java.stats and it will be located in the same directory. It doesn't matter what the contents of the .java file is as long as the output is correct in the .java.stats file. You can use your own .java files and folders to figure this program out. You must use List<String> lines = Files.readAllLines(filePath, Charset.defaultCharset()); iterate through the list of strings. And you must use void writeToFile( Path location, List<String> toWrite ){ Files.write(location,toWrite,Charset.defaultCharset()); } to create the output text which will be MainFile.java.stats. Please use both of the bolded code exactly how it is to figure out this program. There is another solution on the website; however, it is not what I was looking for.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:---- Date:18/09/2018

import java.util.*;

import java.io.*;

import java.nio.charset.Charset;

import java.nio.file.*;

public class LongestFile {

public static void main(String[] args) throws IOException {

String dir = "/home/foo/";

List<String> longest = new ArrayList<>();

String fileName="";

File folder = new File(dir);

File files[] = folder.listFiles();

String longest_file_path="";

for(int i=0;i<files.length;i++) {

if(!files[i].isFile())continue;

String fpath = files[i].getAbsolutePath();

if(fpath.endsWith(".java")||fpath.endsWith(".txt")) {

Path filepath = Paths.get(fpath);

try {

List<String> lines = Files.readAllLines(filepath,Charset.defaultCharset());

if(lines.size()>longest.size()) {

longest=lines;

longest_file_path = fpath+".stats";

fileName = files[i].getName();

}

} catch (IOException e) {

System.out.println("error in reading file "+files[i].getName());

}

}

}

System.out.println("longest file found in "+dir+" is : "+fileName);

System.out.println("writting to file......");

writeToFile(Paths.get(longest_file_path), longest);

System.out.println("\n\nOutput written to "+longest_file_path);

}

public static void writeToFile(Path location, List<String> toWrite) {

try {

Files.write(location, toWrite, Charset.defaultCharset());

} catch (IOException e) {

System.out.println(e.getMessage());

}

}

}

COMPILE & EXECUTE PASTE SOURCE Successfully compiled /tmp/java-wVTQxh/LongestFile . java <-main method

Add a comment
Know the answer?
Add Answer to:
Create a Java program that analyzes a program and produces the output into another file. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following...

    Please write a java program with a input file "jabberwock.txt", and output the "output.txt" with following requirement. You and your partner will be writing a simple Java program that implements some basic file I/O operations. You can use this code as the basis for some of your next project. FIRST: Write code that prompts the user for the name of a text file, opens that file if it exists and reads the file one line at a time printing each...

  • Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A...

    Homework description::::: Write JAVA program with following description. Sample output with code will be helful... A compiler must examine tokens in a program and decide whether they are reserved words in the Java language, or identifiers defined by the user. Design a program that reads a Java program and makes a list of all the identifiers along with the number of occurrences of each identifier in the source code. To do this, you should make use of a dictionary. The...

  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

  • java question: Write a program that reads in data from a text file named in.txt. Compute...

    java question: Write a program that reads in data from a text file named in.txt. Compute the sum of all the valid integers in the input file. Likewise, compute the sum of all the valid doubles in the input file. Write the former to an output file called int_total.txt, and write the latter to an output file called double_total.txt. B) Write a program that converts the Java source code from the next-line brace style to the end-of-line brace style. For...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

  • Once your UML diagram is complete implement the class naming the Java file MyInteger.java. Create another...

    Once your UML diagram is complete implement the class naming the Java file MyInteger.java. Create another Java file, a program named MyIntegerTester.java that tests all methods in the class. One technique is to write both classes at the same time. As much as possible you implement and test one method at a time. This minimized the amount of new code you have to look at when debugging a method. Only the Version 4.0 “tester” file will have main() method in...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression...

    Regular Expression processor in Java Overview: Create a Java program that will accept a regular expression and a filename for a text file. The program will process the file, looking at every line to find matches for the regular expression and display them. Regular Expression Format The following operators are required to be accepted: + - one or more of the following character (no groups) * - zero or more of the following character (no groups) [] – no negation,...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • Write a java program to create a student file which includes first name, last name, and...

    Write a java program to create a student file which includes first name, last name, and GPA. Write another Java program to open the students file. Display the students list and calculate the average GPA of the class.  

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT