Monday, 3 November 2014

Write a programme to display contents of a file in reverese order




Name : .Shreyal Mandot

Title : Write a programme to display contents of a file in reverese order



import java.io.*;

class stack
{

char data[];
int top;

stack()
{

top=-1;
data=new char[100];

}

void push(char d)
{

top++;
data[top]=d;

}

char pop()
{

char d=data[top];
top--;
return (d);

}

}


class ass5a3
{

public static void main(String args[])throws IOException
{

File f1;
char ch;int i=0;
f1=new File(args[0]);

if(!f1.exists())
{

System.out.println(args[0]+" Does not Exist ! \n Programme Terminated......... ");
System.exit(0);

}

        FileInputStream f=new FileInputStream(args[0]);
     
stack s=new stack();
     
while(i != -1)
{
     
        i=f.read();
            ch=(char)i;
      s.push(ch);

  }

while(s.top != -1)
System.out.print(s.pop());

}

}

/*

Output-

[prady@localhost setA]$ javac ass5a3.java
[prady@localhost setA]$ java ass5_3 test.txt

0123456789
ZzZzZzZzZzZ
AAAAAAAAAAA
aaaaaaaaaaa
0987654321

[prady@localhost setA]$ java ass5_3 t.txt

t.txt Does not Exist !
Programme Terminated.........

*/

No comments:

Post a Comment