java FileWriter Append File and File Rename Method

  • 2021-11-02 00:48:31
  • OfStack

FileWriter Append Files and Rename Files

I won't talk too much, let's just look at the code ~


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterUtil {
    /**
     *  Append files: Use FileWriter
     */
    public static void appendMethod(String fileName, String content) {
        try {
            // Open 1 Writer, the first in the constructor 2 Parameters true Indicates writing a file in appended form 
            FileWriter writer = new FileWriter(fileName, true);
            writer.write(content);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     *  Modify file name 
     * @param oldFilePath
     * @param newFileName
     */
    public static void reNameLogFile(String oldFilePath,String newFileName){
        File f=new File(oldFilePath);
        String c=f.getParent();
//        File mm=new File(c + File.pathSeparator + newFileName + "_" + CommonUtil.getCurrTimeForString());
        File mm=new File(c + "/" + newFileName + "_" + CommonUtil.getBeforeDateStr());
        if(f.renameTo(mm)){
            System.out.println(" The file name was modified successfully !");
        }else{
            System.out.println(" Failed to modify file name ");
        }
    }
    public static void main(String[] args) {
        String fileName = "/Users/qin/Downloads/callLog.txt";
        String content = "new append!";
        FileWriterUtil.appendMethod(fileName, content);
        FileWriterUtil.appendMethod(fileName, "append end. \n");
        FileWriterUtil.reNameLogFile("/Users/qin/Downloads/callLog.txt","rayda");
    }
}

Java PrintWriter & FileWriter Write Append to File

Write files with PrintWriter


import java.io.IOException;
import java.io.PrintWriter;
public class PrintWriteDemo
{
    public static void main(String[] args) throws IOException
    {
        PrintWriter out = new PrintWriter("01.txt");
        out.print("the quick brown fox");
        out.println(" jumps over the lazy dog.");
        out.write("work is like a capricious lover whose ");
        out.write("incessant demands are resented but who is missed terribly when she is not there\n");
        out.close(); // If you do not close the file, the file stays in the buffer zone,  Won't be written into "01.txt" Medium 
    }
}

FileWriter can only write to files, cannot append to files

Writing and appending files with FileWriter


import java.io.IOException;
import java.io.FileWriter;
public class FileWriterDemo
{
    public static void main(String[] args) throws IOException
    {
        FileWriter out = new FileWriter("02.txt");
        //constructor Add to true, I.e. FileWriter out = new FileWriter("02.txt", true) Is to 02.txt The file was appended to the 
        out.write("work is like a capricious lover whose ");
        out.write("incessant demands are resented but who is missed terribly when she is not there\n");
        out.write(98.7 + "\n");
        out.close(); // It's important, 1 Remember to close the file 
    }
}

Don't forget throws IOException


Related articles: