Java Queries Here..

chandru.in

In the zone
If such basic file operations are what you want to perform, have a look at the methods of the File class.

ren, dir etc are internal commands and hence Runtime.exec() will not work for them. For other external commands, Runtime.exec() will work fine.
 

ravidawar

Broken In
actually i have a folder in which i have around 100 files with name file.001, file.002 and so on.. so i need to write a java program to convert the file names to file001.rar,file002.rar and so on..thats why i need to run the commands..any other option to do that??
one thing i can do is to use a fileinputreader to read the file and then make a copy of that with a different name (all running under a for loop) but that would be a layman way of doing this thing.
 

Bandu

Journeyman
^Is it only a one time rename that you have to do? If thats the case, you can do it using the ren command like option, or create a batch file to automate things. I don't see a need to write a program for such a trivial activity - unless you are looking for something that is platform independent, etc.

But, as Chandru suggested above, I don't think you will be able to do it.

If you are looking for a command based solution, then do try the following:

Code:
ren file.??? file.???.rar
That does not exactly give you the desired result, but is something you can use and enhance further to get rid of that extra dot. Heres what I did:

*i37.tinypic.com/2cz7gw0.jpg

Edit: Looking at the set of .nnn files that you have and your intended purpose to rename them as rar, I am guessing that you would like to extract the contents from a multispan rar archive. If that is the case, you need not bother about renaming and stuff, just right click and say extract here, or rename a single file from .001 to .r01 and try to extract using this file. You may also try forcefully opening one of the files in winrar and then using the Extract option.
 
Last edited:

chandru.in

In the zone
actually i have a folder in which i have around 100 files with name file.001, file.002 and so on.. so i need to write a java program to convert the file names to file001.rar,file002.rar and so on..thats why i need to run the commands..any other option to do that??
one thing i can do is to use a fileinputreader to read the file and then make a copy of that with a different name (all running under a for loop) but that would be a layman way of doing this thing.

If Bandhu's reply doesn't apply to you, read ahead.

Copying file by file is not only lay man way, it is also highly inefficient. If that is exactly the pattern for re-naming, you can try the code below.

Code:
package demo;

import java.io.File;

public class Main {

    public static void main(String[] args) {
        File directory = new File("/home/chandru/temp");

        for (int i = 1; i <= 2; i++) {
            String numberString = String.format("%03d", i);

            File originalFile = new File(directory, "file." + numberString);
            File targetFile = new File(directory, "file" + numberString
                    + ".rar");

            originalFile.renameTo(targetFile);
        }

        System.out.println("Rename complete.");
    }
}

Change the paths, loop condition and filename patterns as needed.
 

ravidawar

Broken In
Awsome Chandru , thanx a lot.
@Bandu if i do it by the ren command then i will have to do it 100 times.is there any way to run loop in a bat file ?
 

Bandu

Journeyman
^Ravi. You don't need a loop. I've used wildcard thingy. If you notice, with one command, I renamed both my files and the same will work for any number of files.
 

Plasma_Snake

Indidiot
Well for a change lets have this too ;) :D
Q. What is the difference between an Abstract class and Interface?
A. Terms are different ... Nothing more

Q. What is JFC ?
A. Jilebi, Fanta & Coffee

Q. Explain 2 tier and 3 -tier Architecture ?
A. Two wheelers like scooters will have 2 tyres and autorickshaws will have 3 tyres.

Q. I want to store more than 10 objects in a remote server ? Which methodology will follow ?
A. Send it through courier.

Q. Can I modify an object in CORBA ?
A. As you wish , I do not have any objections.

Q. How to communicate 2 threads each other ?
A. Non living things can't communicate.

Q. What is meant by flickering ?
A. Closing and opening of eyes.

Q. Explain RMI Architecture?
A. I am a computer professional not an architect student.

Q. What is the use of Servlets ?
A. In hotels, they can replace servers.

Q. What is the difference between Process and Threads ?
A. Threads are small ropes. Make a rope from threads is an example for process.

Q. When is update method called ?
A. Who is update method?

Q. What is JAR file ?
A. File that can be kept inside a jar.

Q. What is JINI ?
A. A ghost which was Aladdin's friend.

Q. How will you call an Applet from a Java Script?
A. I will give invitation.

Q. How you can know about drivers and database information ?
A. I will go and inquire in the bus dep to.

Q. What is serialization ?
A. Arranging one after the other from left to right.

Q. What is bean ? Where it can be used ?
A. A kind of vegetable. In kitchens for cooking they can be used.

Q. Write down how will you create a binary Tree ?
A. When we sow a binary seed , a binary tree will grow.

Q. What is the exact difference between Unicast and Multicast object ?
A. If in a society, if there is only one caste, then it is Unicast, else it is multicast
 

Quiz_Master

* Teh Flirt King *
LOLOL Plasma Snake... Even after reading 1st few, I was thinking of REAL answers.
Though its a bit different of what is popular among us BCA students.

Q. What is serialization ?
A. Something Ekta Kapoor does.

Q. What is bean ? Where it can be used ?
A. Mr. Bean is a funnu show! It can be used to mock Sharma Sir . (Sharma sir in my college, he looks like Mr. Bean :p )

Q. When is update method called ?
A. Raat 12 baje baad. Local Calls free :p

There is more..like that related to VB, SQL and C++ :p


Its a "Forward as Email to all teachers" material. :p (and I already Fwd it :D )
 
Last edited:

nitish_mythology

OSS Enthusiast!
Here is my query.......

/* A sample class to clear basic concepts of inheritance....
@Author Nitish Upreti
*/

class parent
{
protected char var;
protected int var2;

public void setMe()
{
var='p';
var2=11;
}

}


class child extends parent
{

protected char var;
protected int vars;

public void setMe()
{
var='c';
vars=22;
}

}

public class god
{

private int x; //An inst var

public static void main(String args[])
{

//Trying to use the instant var in a static 'main' method

// x=12 GENERATES ERROR!! PROOVED!!


//parent p=new parent();
//child c=new child();


parent p2=new child();

//p.setMe();
//c.setMe();
p2.setMe();

//System.out.println(p.var);
//System.out.println(c.var);
System.out.println(p2.var);

}
}

The output is a blank line.... why?????????
It should either be 'p' or 'c' !!
 

chandru.in

In the zone
While inheriting and providing same name/signature for members in sub-class, methods are overriden, but variables are not.

Now when you invoke setMe() on p2, it invokes child object's setMe() (methods are overridden) thus setting child's var. When you print you are printing parents var (variables are not overridden) which is not yet set.
 

chandru.in

In the zone
Yes that is the default value. However, how it is displayed upon execution depends on the terminal and platform on which program executes.
 

nitish_mythology

OSS Enthusiast!
Hmm...
I am on Vector Linux! :)

Another Ques
what do we get from interfaces??? we could simply create an abstract class with all abstract methods n' all attributes public static and final...

That will be equivalent to creating an interface!!
 

nitish_mythology

OSS Enthusiast!
I am aware of this fact...
I read tht interfaces save u from Deadly diamond of death..hows that??
U could have used multiple inheritance(if it was allowed) n used purely abstract classes...
WHy did java designers go 4 interfaces??
 
Top Bottom