Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading text file, Replace string, Write to new file....

843785Dec 17 2008 — edited Dec 17 2008
I'm kind a lost of on this problem. Using the BufferedReader class and BufferedWriter class, develop an application that reads lines of text from a file. Replace any occurrence of the word *?the?* with *?JAVAJAVA?* and change all characters to upper case before writing each line to a different text file named outfile.txt. Finally, have your application use the appropriate method on the File class to return the absolute path of the outfile.txt file and output the path to the screen.
 /**
 * Created December 10, 2008
 * 
 * @author Fausto Rivera
 * Colorado Technical University - Online Campus
 * 
 * IT271-0804B-02 Intermediate Object Oriented Programming II
 * Phase 2 IP
 * Instructor: Cheryl B Frederick
 * 
 */

import java.io.File;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class FRiveraReader {
    
      public static void main(String[] args) {
      
      // instantiated a new application object to initialize the application
      FRiveraReader application = new FRiveraReader();}

//      application.getReadfr();  // call to openFile method
//      application.doWritefr(); // call to readRecords method
//      application.closeFile();  // call to closeFile method




public FRiveraReader(){}

  public void getReadfr (String friveraInfile, String fr []){
  
   int i;

   File fileName = makeAbsoluteFilename(friveraInfile);

   try{
    BufferedReader reader = new BufferedReader(new FileReader(fileName));
    
    String line = reader.readLine();
     i=0;
      while (line != null) {
       fr[i] =line;
       line = reader.readLine();
    i++;
  }
  reader.close();
 }


catch(IOException e) {
 System.out.println("Error with reading file:" + friveraInfile);
}

}//end of getOrder method
 



public void doWritefr(String friveraOutfile, String fw[]){//String name of file, String array to be written
int i;
File fileNameout = makeAbsoluteFilename(friveraOutfile);
try {
 BufferedWriter writer = new BufferedWriter(new FileWriter (fileNameout));
  i=0;
  while (fw[i] != null) {
   writer.write(fw[i] + "%n"); //need delimiters between data in file;also, reader reads a line 
   i++;
  }
  writer.close();
 }

catch(FileNotFoundException e) {
 System.out.println("File not found");
}

catch(IOException e) {
 System.out.println("Error with reading file:" + friveraOutfile);
}



}//end of getOrder method
//................
private File makeAbsoluteFilename(String friveraOutfile)//these 2 classes used to resolve file name
    {
        File file = new File(friveraOutfile);
        if(!file.isAbsolute()) {
            file = new File(friveraOutfile);
        }
        return file;
    }
//................

  
  
  }


 
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 14 2009
Added on Dec 17 2008
6 comments
2,415 views