Click here to Skip to main content
16,012,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want my java program to replace some content and write it to new file.

Here is what I have tried but it only reads from one file and writes to another but, I want to replace some text also.
Java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class FIO
{
  public static void main(String[] args) {

  File fileR = new File("D:/java/json.js");

  try(FileInputStream fis = new FileInputStream(fileR)) {

    int content;
    content = fis.read();

    System.out.print(content);

    //String content = "This is some text";

    File fileW = new File("D:/java/json1.js");

    // if file doesnt exists, then create it
    if (!fileW.exists()) {
      fileW.createNewFile();
    }

    FileWriter fw = new FileWriter(fileW.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();

    System.out.println("Done");
  }catch (IOException e) {
    e.printStackTrace();
  }
 }
}

i hav tried something like this.. it only reads from one file and writes to another.. but i want to replace some text
Posted
Updated 28-Jun-15 23:31pm
v3
Comments
Suvendu Shekhar Giri 29-Jun-15 4:56am    
Anything you have tried so far or you want us to write the complete program?
Suvendu Shekhar Giri 29-Jun-15 5:31am    
added your code to the question, please verify that it's correct.
Suvendu Shekhar Giri 29-Jun-15 5:41am    
fis.read(); will return (an integer value) the total number of bytes read into the buffer. Isn't it?
Then what you are expecting bw.write(content); would do?
kiran gowda8 29-Jun-15 6:00am    
yes it returns total number of bytes.. and its not possible to modify the text using it. that was my problem

1 solution

try like this below code:

Java
// Reading File
		String fileName = "temp.txt";
		String NewfileName =  "New_temp.txt";
		String FileContent = "";
        String line = null;
        try 
		{
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
			
		  
			while((line = bufferedReader.readLine()) != null) 
			{
				FileContent = line;
            } 
			
            bufferedReader.close();            
        }
        catch(IOException ex) 
		{
            System.out.println("Error reading file '" + fileName + "'" + ex.printStackTrace());                   
        }

// Changing Content of File
				// Do changes with  'FileContent' 
		
		
// Writeing in New  File
		try
		{
			FileWriter fileWriter =new FileWriter(NewfileName);
            BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
          
			bufferedWriter.write(FileContent);
			
			bufferedWriter.close();			
		}
        catch(IOException ex) 
		{
            System.out.println("Error Writing file '" + NewfileName + "'" + ex.printStackTrace());                   
        }
 
Share this answer
 
v3
Comments
kiran gowda8 29-Jun-15 6:05am    
thank you so much Member 8124090.
kiran gowda8 29-Jun-15 6:24am    
"font":"eng",
"heading":"Some text"
this is the content of txt file and i want to change the value of heading and write to another file. how to achieve this ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900