Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Write a File in Java

Using File Writer and Buffered Writer in java.io.File package, you can write a file. See the example below. 

 

package com.seleniummaster.examplefile;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {

    public static void main(String[] args)
    {
        StringBuilder stringBuilder=new StringBuilder();
        stringBuilder.append("Hello Test").append("\n");
        stringBuilder.append("Selenium Master").append("\n");
        try {
            WriteToFile.WriteToFile(stringBuilder.toString(), "test.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void WriteToFile(String fileContent, String fileName) throws IOException {
        String projectPath = System.getProperty("user.dir");
        String tempFile = projectPath + File.separator+fileName;
        File file = new File(tempFile);
        // if file does exists, then delete and create a new file
        if (file.exists()) {
            try {
                File newFileName = new File(projectPath + File.separator+ "backup_"+fileName);
                file.renameTo(newFileName);
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(fileContent);
        System.out.println(fileContent);
        bw.close();

    }
    }

Run the code and see the result below

Hello Test
Selenium Master


Process finished with exit code 0