Example of Unix commands implemented in Java
2014-10-10 00:29
393 查看
I created some illustrative and simple implementations of common Unix commands. For those who are familiar with Unix-like systems them make easier to understand Java. For those who are familiar with Java them make easier to understand Unix-like systems. :-)
1. PWD
The first one is pwd that show the current working directory.
Running this at /home/silveira directory gives us as output:
$ java Jpwd
/home/silveira
1. CAT
The command cat is usually utilized for displaying files.
$ java Jcat /etc/timezone
America/Fortaleza
3. LS
The command ls is to list files. The File API (java.io.File) is very flexible and portable, but in this
example I want just list files and directories of the current directory.
Usage:
$ java Jpwd
/home/silveira/example
$ java Jls
directoryA
fileA
.somefile
4. CD
The cd command changes the current working directory.
Usage:
$ java Jpwd
/home/silveira
$ java Jcd /tmp
$ java Jpwd
/tmp
1. PWD
The first one is pwd that show the current working directory.
public class Jpwd { public static void main(String[] args) { String pwd = System.getProperty("user.dir"); System.out.println(pwd); } }
Running this at /home/silveira directory gives us as output:
$ java Jpwd
/home/silveira
1. CAT
The command cat is usually utilized for displaying files.
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class Jcat { public static void main(String[] args) { if(args.length==1){ try { FileReader fileReader = new FileReader(args[0]); BufferedReader in = new BufferedReader(fileReader); String line; while((line = in.readLine())!= null){ System.out.println(line); } } catch (FileNotFoundException ex) { System.out.println(args[0]+", file not found."); } catch (IOException ex) { System.out.println(args[0]+", input/output error."); } } } } |
America/Fortaleza
3. LS
The command ls is to list files. The File API (java.io.File) is very flexible and portable, but in this
example I want just list files and directories of the current directory.
import java.io.File; public class Jls { public static void main(String[] args) { File dir = new File(System.getProperty("user.dir")); String childs[] = dir.list(); for(String child: childs){ System.out.println(child); } } }
Usage:
$ java Jpwd
/home/silveira/example
$ java Jls
directoryA
fileA
.somefile
4. CD
The cd command changes the current working directory.
import java.io.File; public class Jcd { public static void main(String[] args) { if(args.length==1){ File dir = new File(args[0]); if(dir.isDirectory()==true) { System.setProperty("user.dir", dir.getAbsolutePath()); } else { System.out.println(args[0] + "is not a directory."); } } } } |
$ java Jpwd
/home/silveira
$ java Jcd /tmp
$ java Jpwd
/tmp
相关文章推荐
- How Volatile in Java works? Example of volatile keyword in Java
- Class JavaLaunchHelper is implemented in both … libinstrument.dylib. One of the two will be used. Wh
- 10 points about instanceof operator in Java with example
- Class JavaLaunchHelper is implemented in both. One of the two will be used. Which one is undefined
- what is time complexity of concatenating two int in java example
- Class JavaLaunchHelper is implemented in both … libinstrument.dylib. One of the two will be used. Wh
- java.lang.NoSuchFieldError: No static field ib_back of type I in class Lcom/example/demo/R$id; or
- 10 Example of Hashtable in Java – Java Hashtable Tutorial
- Example of ConcurrentHashMap in Java--转
- How Synchronization works in Java ? Example of synchronized Block and Method
- List of Open Source Softwares in Java
- 数组排序的Java实现(Sorting of arrays implemented with Java)
- nested exception is java.sql.SQLException: Unknown type '246 in column 1 of 3 in binary-encoded result set. 问题
- Fix a Man-made Mistake in Java Project of Eclipse
- A simple Example of using Video Textures in Managed DirectX (C#)
- Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- java.sql.SQLException: Unknown type '246 in column 0 of 1 in binary-encoded result set的解决办法
- example of using xml parser in php: expat
- A example code of multithread communication in Python
- Some Samples Of Oracle Function In Order To Relax Java Program.