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:

Related Articles

  • Reading text file, Replace string, Write to new file....October 11

    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

  • How to replace a string in a text file?November 30

    Hi All, i read one text file and based on that i replaced the old character with the new string but it doesnt works.do any one of you have idea on this? My Code: String newPassword=(String) getInputText1().getValue(); String password=(String)getSimNo

  • Replace a string within text file to another stringNovember 30

    hi everybody i wich to replace some word writed in text file to another word,,,,for example i want to replace all word when the word is "SOLARIS" to "solaris" . anyone have an idea to resolve this prob via command or script,plz respond

  • Find and replace a line in a text  fileOctober 11

    Hi All, I wanted to read a text file , find a line and replace that line with out affecting any other lines and with out creating a new file. If any one of you know any API to do this work ,please throw some lightThis seems to be a question on optima

  • Replacing text in a text file with special charactersOctober 11

    Hi all, in my test text file i found an interesting issue, where i can't replace text that looks something like this "New Text Document - Copy (2).txt" (gc $logeps)|foreach{$_ -replace "$($file.name)",""}|Out-File $logeps the

  • How Can I replace line in text fileOctober 11

    I have a text file like following format 1 bvhhk g1 2 bvgjvh g1 3 mmm,mvb g2 I want to replace 2 nd line to " prasad" and after replacing it should be following format 1 prasad g1 2 bvgjvh g1 3 mmm,mvb g2 I try above change using following code

  • Find and replace string in fileOctober 11

    hi, i need to find and replace text (string) data in about 100+ files, i've decided to write a small java program to do it but i'm stuck on how to replace. here is what i'm heading towards: class ReplaceString {   BufferedReader in;   BufferedReader

  • Replace string in ms word fileOctober 11

    Salue, I need to add a functionality to my Java app by allowing the replacement of strings in ms word files. Basically I need to use an original MS Word file as template and then populate it with data, replacing some keywords. And I need to do this w

  • Finding strings in a text file?November 30

    Hi! i'm learning to read text files. what i want to do with this program is to find word 1-3, 2-4, 3-5... and write them in a system.out.println. i found a program that opens files and count words, lines and characters and tried to adjust it do find

  • Replace/cut part of words from a text file.November 30

    Hello Hello everyone, I have a quick question. I have my text file that contains also words like ... let's say abc1, abc2 and so on, and I need to cut the c from the word, I need ab1, ab2. Here is what I have started: import java.util.*; import java.

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