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
  • Implement the File Input/Output example presented in page 7. You need to create your own test.txt...

    Implement the File Input/Output example presented in page 7. You need to create your own test.txt file using any text editor. The input file should have some integer values (e.g., 10 values) separated by whitespaces. Normally this input file should be placed in the same directory as the program code resides. But depending on the IDE you use, it may be a different directory in the project. Figure out which directory your IDE reads an input file from and writes...

  • 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* You will write a program to do the following: 1. Read an input file with...

    *Java* You will write a program to do the following: 1. Read an input file with a single line of text. Create a method named load_data. 2. Determine the frequency distribution of every symbol in the line of text. Create a method named 3. Construct the Huffman tree. 4. Create a mapping between every symbol and its corresponding Huffman code. 5. Encode the line of text using the corresponding code for every symbol. 6. Display the results on the screen....

  • Creating a Shell Interface Using Java This project consists of modifying a Java program so that...

    Creating a Shell Interface Using Java This project consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the user’s next command: cat Prog.java. This command displays the file Prog.java on...

  • 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...

  • Reading and Writing Files in a Directory Create a program that provides a listing of all...

    Reading and Writing Files in a Directory Create a program that provides a listing of all the files in a directory. The program should be a Java application that accepts a single argument into the args array of the main() method. This argument identifies the name of a directory. The application should make sure that this filename really does identify a directory and then list all of the files in the directory. For each file, list whether the file is...

  • 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.  

  • Create a java program that reads an input file named Friends.txt containing a list 4 names...

    Create a java program that reads an input file named Friends.txt containing a list 4 names (create this file using Notepad) and builds an ArrayList with them. Use sout<tab> to display a menu asking the user what they want to do:   1. Add a new name   2. Change a name 3. Delete a name 4. Print the list   or 5. Quit    When the user selects Quit your app creates a new friends.txt output file. Use methods: main( ) shouldn’t do...

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
Active Questions
ADVERTISEMENT