MainClass.java Source code

Java tutorial

Introduction

Here is the source code for MainClass.java

Source

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class MainClass {
    public static void main(String[] Args) {
        String filepath = "C:/myFile.txt";
        File aFile = new File(filepath);
        FileOutputStream outputFile = null;
        if (aFile.isFile()) {
            File newFile = aFile;
            do {
                String name = newFile.getName();
                int period = name.indexOf('.');
                newFile = new File(newFile.getParent(),
                        name.substring(0, period) + "_old" + name.substring(period));
            } while (newFile.exists());
            aFile.renameTo(newFile);
        }
        try {
            outputFile = new FileOutputStream(aFile);
            System.out.println("myFile.txt output stream created");
        } catch (FileNotFoundException e) {
            e.printStackTrace(System.err);
        }
    }
}