Wednesday, July 3, 2019

3- Write a program that reads every line in a text file, removes the first word from each line, and then writes the resulting lines to a new text file.

3- Write a program that reads every line in a text file, removes the first word from each line, and then writes the resulting lines to a new text file.
/**
 * @(#)buffer.java
 *
 * buffer application
 *
 * @author
 * @version 1.00 2019/7/3
 */
 import java.io.*;
 import java.util.Scanner;
 import javax.swing.JOptionPane;

public class buffer {
   
    public static void main(String[] args) {
   
    Scanner input = new Scanner(System.in);
   
    System.out.println("ENTER THE FILE TO READ");
    String infileName = input.nextLine().trim();
    // trim will remove spaces in the beginning and in the end
   
    System.out.println("Enter the file to write to");
    String outfileName = input.nextLine().trim();
   
    Scanner inputFile = null;
try{
inputFile = new Scanner (new File(infileName));
}catch (FileNotFoundException e){
System.out.println("Error the reading the file " + infileName);
System.exit(0);
}
   
    PrintWriter outputFile = null;
    try{
    outputFile = new PrintWriter(new File(outfileName));
    }catch (FileNotFoundException e){
    System.out.println("Error writing to the file " + outfileName);
    System.exit(0);
    }
    while(inputFile.hasNextLine()){
    String line = inputFile.nextLine();
    Scanner lineScanner = new Scanner(line);
   
    if(lineScanner.hasNext()){
    String word = lineScanner.next();
    int wordLocation = line.indexOf(word);
    String rest = line.substring(wordLocation+word.length());
    outputFile.println(rest);
    }
    else {
    outputFile.println(line);
    }
    }
    inputFile.close();
    outputFile.close();
   
    JOptionPane.showMessageDialog(null,"file processing completed! ", "JAVA to", JOptionPane.INFORMATION_MESSAGE);
    }
   
   
    }

0 comments:

Post a Comment