Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦

Similar presentations


Presentation on theme: "Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦"— Presentation transcript:

1

2 Unix Environment Input Output 2

3  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦ mkdir[directory name]  Change Directory (cd) ◦ cd [directory name] ◦ Use [Tab] button for auto-completion  Remove Directory Entries (rm) ◦ rm [file/folder name]  Java Compile (javac) ◦ javac *.java (shortcut to compile all java files) ◦ Similar effect as make command 3

4  Edit File in VIM (vim) ◦ vim [file name]  Running Java Program (java) ◦ java [program name without extension] ◦ java [class name] [output file] (input/output redirection)  Checking for Difference (diff) ◦ diff [file name] [file name] ◦ Difference in a single space or an extra line will be noted  Manual (man) ◦ man [command name] ◦ When you are not sure what the command does, or what parameter it expects 4

5  http://vim.rtorr.com/  3 main modes ◦ Normal/Command mode (default)  For navigation and manipulation of text  Everything the user types is interpreted as commands  e.g. “h” to move left, “x” to remove a character ◦ Insert mode (i, I, a, A, o, O)  For inserting new text  Exit Insert mode to normal mode using Esc ◦ Visual mode  For highlighting texts (useful for copy and paste) 5

6  Cursor movement (Only in command mode!) ◦ h: move cursor left ◦ j: move cursor down ◦ k: move cursor up ◦ l: move cursor right ◦ 0: jump to start of line ◦ $ jump to end of line ◦ G: end of file ◦ 5G: go to line 5 6

7  Insert mode – insert/append text  From command mode ◦ i: insert before cursor ◦ I: insert at the beginning of the line ◦ a: append after cursor ◦ A: append at the end of line ◦ o: open a new line below the current line ◦ O: open a new line above the current line ◦ ea: append at end of word 7

8  Editing ◦ s: delete character and substitute text ◦ cc: replace(change) entire line ◦ cw: replace(change) to end of word ◦ c$: replace(change) to end of line ◦ x: cut character ◦ u: undo ◦ r: replace one character ◦ R: replace all character until ESC 8

9  Visual mode ◦ v: start visual mode, mark lines, then yank (y) ◦ V: linewise ◦ Ctrl-v: visual block mode  Copy and Pasting ◦ Copy/yank: y  Copy one line: yy, 2 lines: 2yy, to end of line: y$, one word: yw ◦ Cut/delete: d  Cut one line: dd, 2 lines: 2dd, to end of line: d$, one word: dw ◦ Paste: p 9

10  Search ◦ /[pattern] → Search for the pattern ◦ n → Search next in the same direction ◦ N → Search next in the opposite direction  Saving and quitting ◦ Save (write): :w ◦ Save and quit: :wq ◦ Quit without saving: :q!  Tip ◦ Open 2 windows, one to code, other to compile ◦ gg=G : indent all lines 10

11  Write a Java program that prints “Hello World” out to the console  In Vim  Compile and execute the program  vim [name of file].java 11

12 Unix Environment Input Output 12

13  Scanner Class ◦ Initialization: Scanner sc = new Scanner (System.in); ◦ hasNext() and variants  Check if there exists another input ◦ next() and variants  Get the next input ◦ Variants: Int, Line, Double example: hasNextInt() and nextLine() ◦ There is NO nextChar() and hasNextChar() variant 13

14  Finds and returns the next complete token from this scanner.  A complete token is preceded and followed by input that matches the delimiter pattern.  By default: delimiter pattern is whitespace 14

15  next() ◦ reads the next token as a string  nextInt() ◦ reads the next token as an integer. If it is not: error  nextLine() ◦ Advance scanner, read and return all in current line (until next newline character) 15

16  Input: 1 fish 2 fish 3 fish 4 fish /n end  next(): ◦ 1 (as a string)  nextInt(): ◦ error  next(): ◦ fish  nextInt(): ◦ 2 (as an integer)  nextLine(): ◦ fish 3 fish 4 fish 16

17 o Type 1: Number of Operations is specified o Type 2: Read until Terminating Special Character o Type 3: Read until End of File 17

18 public static void main(String[] args) { // … Code Section Omitted // … Other Initialization Scanner sc = new Scanner(System.in); int numOps = sc.nextInt(); for (int i=0; i<numOps; i++) { // Read Other Inputs } // … Code Section Omitted } 18

19  Steps: 1. Initialize the Scanner Class 2. Read the First Input 3. Loop until Terminating Special Character encountered 4. Read into tempInput again 19 Scanner sc = new Scanner(System.in); String tempInput = sc.next(); while (!tempInput.equals(TERMINATING_CHAR)) { // Read Other Input (if exist) // Step 4 Here } tempInput = sc.next();

20 20 public static void main(String[] args) { // … Code Section Omitted // … Other Initialization Scanner sc = new Scanner(System.in); String tempInput = sc.next(); while (!tempInput.equals(TERMINATING_CHAR)) { // Read Other Input tempInput = sc.next(); } // … Code Section Omitted }

21  Steps: 1. Initialize the Scanner Class 2. Loop until End of File 21 Scanner sc = new Scanner(System.in); while (sc.hasNext()) { // Read Other Input }

22 22 public static void main(String[] args) { // … Code Section Omitted // … Other Initialization Scanner sc = new Scanner(System.in); while (sc.hasNext()) { // Read Other Inputs } // … Code Section Omitted }

23  Combines two or more different input types into a single program ◦ e.g.: Type 3 Input on the Outside, Type 1 Input on the Inside. 23 public static void main(String[] args) { // … Code Section Omitted // … Other Initialization Scanner sc = new Scanner (System.in); while (sc.hasNext()) { int numOps = sc.nextInt(); for (int i=0; i<numOps; i++) { // Read Other Inputs } // … Code Section Omitted }

24 Unix Environment Input Output 24

25  With Extra Line at the End  Without Extra Line at the End 25 System.out.println("Output String Here"); System.out.print("Output String Here"); System.out.print("Output String Here\n");

26  One per Line, Terminate with End Line  One per Line, Terminate without End Line 26 for (int i=0; i<numOutput; i++) { System.out.println(outputString); } System.out.print(outputString); for (int i=1; i<numOutput; i++) { System.out.println(); System.out.print(outputString); }

27  Matrix Type, End Space  Matrix Type, End Not Space 27 for (int i=0; i<numLine; i++) { for (int j=0; j<numOut; j++) { System.out.print (output + " "); } System.out.println(); } for (int i=0; i<numLine; i++) { System.out.print (output); for (int j=1; j<numOut; j++) { System.out.print (" " + output); } System.out.println(); }

28 END OF FILE


Download ppt "Unix Environment Input Output 2  List Content (ls) ◦ ls (list current directory) ◦ ls –all (include hidden files/folders)  Make directory (mkdir) ◦"

Similar presentations


Ads by Google