Java Programing laungage

java.util Projects

java.util Project 1

Limit the Size of Log File

In This programming tutorials, we are going to illustrates you how to control the size of a log file in Java application. Log files have not any boundation at the file creation time but Java is facilated to the developer to control the size of a log file with help of FileHandler. If the log records are too smaller then limiting log file size otherwise the spaces are worst but it is larger than some information's to be lost or not mentioning in the log file.

Previous Home Next
adplus-dvertising

program Description

In this Example take a file name which should have '.log' extension. If file exists then the capacity of log file is limited or controlled or bounded and shows a message "Operation successfully" otherwise it shows message "File not found!" and no any action executed.

code Descriptions


FileHandler(String file_name, int limit, int no_file)

It is a constructor of Logging class which is used for declaring a file handler for writing to the set of files. It takes following arguments.

String file_name: File name which has to be written.

int limit: The maximum size of file.

int no_file: Number of file that has been used by user.

Example


package r4r;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
public class limitsizetest {
public static void main(String[] args) {
try{
Logger logg = Logger.getLogger("R4RTechSoft Solution"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter file name with '.log' extension"); String str = br.readLine(); File file = new File(str + ".log"); if(file.exists()) { int limits = 10; FileHandler fhand = new FileHandler(str + ".log", limits,1); System.out.println("Operation is successfully"); logg.addHandler(fhand); } else{ System.out.println("File not found!"); } } catch (IOException e){} } }
Previous Home Next