LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-06-2004, 12:47 PM   #1
deveraux83
Member
 
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Rep: Reputation: 15
Interesting Java question


A friend of mine who's a compsci recently presented to me a past question on Java which I thought was really interesting. I don't do java but I do C++ and since the two are closely related I tried to have a bash at it but failed. Neways, thought u guys might like to give it a shot (only if u're interested) and if u do pls post ur thoughts as I would be very interested to hear them.

Question: Write a Java code that when compiled into a program and executed will display the same output as running cat %source file%
 
Old 07-06-2004, 01:39 PM   #2
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Code:
import java.io.*;

class myReader{
  public static void main(String args[]){
     if(args.length < 1){
        System.out.println("Usage: java myReader <filename>");
        System.exit(0);
     }
     try{
     BufferedReader br = new BufferedReader(
        new InputStreamReader(new FileInputStream(args[0])));
     String tmp = br.readLine();
     while(tmp!=null){
          System.out.println(tmp);
          tmp = br.readLine();
     }
     }catch(FileNotFoundException fnfe){

     }catch(IOException ioe){

     }
  }
}
Is that what you want?

Save it as myReader.java, compile and then run with:
java myReader <filename>
which prints out the file as does:
cat <filename>
 
Old 07-06-2004, 01:45 PM   #3
deveraux83
Member
 
Registered: Jul 2003
Location: Malaysia
Distribution: Red Hat, Slackware 9.1
Posts: 76

Original Poster
Rep: Reputation: 15
Ok errmm, sorry, I forgot to mention, you can't use File I/O ..sorry, silly me.
 
Old 07-06-2004, 01:48 PM   #4
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Ahhhh yes, well that changes things slightly!
 
Old 07-07-2004, 02:10 AM   #5
moeminhtun
Member
 
Registered: Dec 2002
Location: Singapore
Distribution: Fedora Core 6
Posts: 647

Rep: Reputation: 30
Man! seems like you have to write your own Java decompiler.
 
Old 07-07-2004, 11:28 PM   #6
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
Well LMAO at least it doesn't use IO eh? :P

Code:
class Cat
{
    public static void main(String args[])
    {
       if(args.length < 1)
       {
           System.out.println("Usage: java Cat <filename>");
           System.exit(0);
        }
        Runtime.getRuntime().exec("cat " + args[0])
    }
}
 
Old 07-08-2004, 01:06 PM   #7
csfalcon
Member
 
Registered: Jun 2004
Location: MD
Distribution: Fedora Core
Posts: 269

Rep: Reputation: 31
that's the classic "self reproducing" program, I have seen the code of such a program in an old language. The most common application of this kind of programs is virus that reproduces itself.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting C Question ar1 Programming 2 05-07-2005 10:24 AM
Interesting (My)SQL question rjlee Programming 9 03-07-2005 08:23 AM
One interesting question palanisaravanan Linux - Networking 2 04-21-2004 09:39 AM
An Interesting question about a fresh install gamehack Debian 5 01-27-2004 02:04 PM
Interesting Network/Internet question ChaosX2 Linux - Networking 9 10-03-2001 10:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration