Wednesday, December 8, 2010

java random access file rotating

This code provides a file which is random access,
rotating by means firstly fixed file size
and after the reaching the limit
it'll delete first lines and append new content at the end of file.

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Calendar;

public class RotateRAF
{
public static int Size = 2048;
public RotateRAF(String message, String fileName) throws IOException
{
Calendar cal = Calendar.getInstance();
String time = cal.getTime().toString();
RandomAccessFile raf = new RandomAccessFile(fileName,"rw");
if(raf.length() <= Size)
WriteToRAF(fileName, time, message);
else{
deleteRAF(fileName);
WriteToRAF(fileName, time, message);
}
}
public static void WriteToRAF(String fileName,String time,String message)
throws IOException
{
try
{
RandomAccessFile raf = new RandomAccessFile(fileName,"rw");
raf.seek(raf.getFilePointer());
raf.seek(raf.length());
raf.writeBytes(time);
raf.writeBytes(" ");
raf.seek(raf.length());
raf.writeBytes(message);
raf.seek(raf.length());
raf.writeBytes("\n");
} catch (FileNotFoundException e)
{e.printStackTrace();}
}

public static void deleteRAF(String fileName) throws IOException
{
try
{
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
long pos = 0;
while ( raf.readLine() != null) {
//Create a byte[] to contain the not deleted contents of the file.
// and truncate the file to the pos. of deleted the contains.
byte[] notDeleted = new byte[(int) (raf.length() - raf.getFilePointer())];
raf.read(notDeleted);
raf.getChannel().truncate(pos);
raf.seek(pos);
raf.write(notDeleted);
pos += raf.getFilePointer();
return;
} catch (FileNotFoundException e)
{e.printStackTrace();}
}

public static void main(String args[]) throws IOException
{
for(int i=0;i<=10;i++){
RotateRAF("Computers are useless. They can only give you answers.Pablo Picasso",
"rotateRAF.txt");
RotateRAF("The Internet? Is that thing still around?.Homer Simpson",
"rotateRAF.txt");
}
}

}

Sunday, September 26, 2010

PL control flow structure categories

Control flow is executed or evaluated by a program which would be imperative or declarative via individual statements,instructions, or function calls. The control flow concept is one of the most important issue of programming language design. The control flow issue consist of eight categories. The categories are below:
1. sequencing: statements executed specified order in general in accordance with the code text.
2. selection: using condition constructs such as (also popular) if and case (switch) statements. It would depending on run-time condition.
3. iteration: it provides repeatly execution of a part of code. It can be work absolute number or till a case that the condition is true in run-time. Terms used in example for, do-while, repeat constructs.
4. procedural abstraction: a subroutine which as an example control construct but it is not simple collection of the construct.
5. recursion: recursion on the other hand, self-referential subroutines that the approach to save the information of evaluated partially instances.So,it needs a stack structure. Due to the this feature of the computational model the expression is defined in terms of itself.
6. concurrency: simple means paralellism,more than one part of program are to be executed at the same time.
7. exception handling and speculation: exception handling is when the condition is false, an execution handling works either remainder protected program fragment or the whole protected fragment. The second is speculation.
8. nondeterminacy: The ordering or any choosing of statements is not specified.


and also available details.PLP


---------------------------------------------------------------------------
sirin