Home > Default > Reading text file, Replace string, Write to new file....

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

October 11Hits:0
Advertisement
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;

Answers

I have modified my code as far as being able to create a text file, write to string, and change to upper case. Now, how can I connected input to the output stream and then replace all the instances of the string "the" for the string "JAVAJAVA"? When the file is created, it goes into a loop writing the following:
[email protected]%[email protected]%[email protected]%[email protected]%[email protected]%[email protected]%
Here is my modified code
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 FRiveraReader() {}
private static final String inFile = "C:\\Documents and Settings\\Fausto Rivera\\My Documents\\NetBeansProjects\\Rivera_F_IT271_0804B_02_PH2_IP\\friverainfile.txt";//the path name of your file
  public void myReadfr (){
   try {
    BufferedReader reader = new BufferedReader(new FileReader(inFile));
    //String "JAVAJAVA" = String "the";
    System.out.println(inFile);
    String line = reader.readLine();
      while (reader != null) {
       line = reader.readLine();
  reader.close();
catch(IOException e) {
System.out.println("Error with reading file:" + inFile);
}//end of myReadfr method
public void myWritefr(){//String name of file, String array to be written
File outFile = new File ("C:\\Documents and Settings\\Fausto Rivera\\My Documents\\NetBeansProjects\\Rivera_F_IT271_0804B_02_PH2_IP\\myoutFile.txt");
try {
BufferedWriter writer = new BufferedWriter(new FileWriter (outFile));
String newline = writer.toString() .toUpperCase();
  while (writer != null) {
   writer.write(newline + "%n"); //need delimiters between data in file;also, reader reads a line
  writer.close();
catch(FileNotFoundException e) {
System.out.println("File not found");
catch(IOException e) {
System.out.println("Error writing file:" + outFile);
}//end of getOrder method
private File makeAbsoluteFilename(String myoutFile)//these 2 classes used to resolve file name
        File file = new File(myoutFile);
        if(!file.isAbsolute()) {
            file = new File(myoutFile);
        return file;
  }Edited by: FRiveraJr on Dec 17, 2008 12:10 PM

Read other 7 answers

Tags:

Copyright (C) 2017 wisumpire.com, All Rights Reserved. webmaster#wisumpire.com 12 q. 0.146 s.