Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java write a program that reads the content of a file, modifies that content,

ID: 644166 • Letter: I

Question

In Java write a program that reads the content of a file, modifies that content, and then writes the result to a new file. First, you need to decide exactly how you want to modify the content of the file, ultimately the details are up to you. There are a few restrictions. First, you need to make at least two modifications. And second, one of those modifications must be a replacement kind of modification (I recommend making use of the string method .replace()). So for example, you could replace every occurence of the letter "O" with the digit "0" and every occurence of the letter "I" with the digit "1". But it has to be something different than that example.

Then write a program that uses this trasformation to alter the contents of a text file. You should include a static method that takes a string and returns the transformed version of the string. The main() method should ask the user for a source filename and a destination filename. Then it should read the content from the source file, modify it using your transformation method, and write the results to the destination file. If any IOExceptions are thrown, it should display an error message and ask the user to enter a new file.

Explanation / Answer

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import javax.swing.JFileChooser; import javax.swing.JFrame; public class reader2 { public reader2() { } public static void main(String[] args) { reader(args); } public static void reader(String[] args) { JFileChooser chooser = new JFileChooser(); chooser.setCurrentDirectory(new File(".")); chooser.setFileFilter(new javax.swing.filechooser.FileFilter() { public boolean accept(File f) { return f.getName().toLowerCase().endsWith(".txt") || f.isDirectory(); } public String getDescription() { return "Text Documents (.txt)"; } }); int r = chooser.showOpenDialog(new JFrame()); if (r == JFileChooser.APPROVE_OPTION) { String name = chooser.getSelectedFile().getName(); String pathToFIle = chooser.getSelectedFile().getPath(); System.out.println(name); try{ BufferedReader reader = new BufferedReader( new FileReader( pathToFIle ) ); //Setup the reader while (reader.ready()) { //While there are content left to read String line = reader.readLine(); //Read the next line from the file String[] tokens = line.split( "url = " ); //Split the string at every @ character. Place the results in an array. for (String token : tokens){ //Iterate through all of the found results //System.out.println(token); System.out.println(token); } } reader.close(); //Stop using the resource }catch (Exception e){//Catch exception if any System.err.println("Error: " + e.getMessage()); } } } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote