1 / 80

Strings, I/O, Formatting, and Parsing

Strings, I/O, Formatting, and Parsing. String, StringBuilder, and StringBuffer. The String Class. String are immutable objects Create an instance of String with the keyword "new“ Example: String s = new String(); String s = new String("abcdef"); String s = "abcdef";. The String Class.

aggie
Download Presentation

Strings, I/O, Formatting, and Parsing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings, I/O,Formatting,and Parsing

  2. String, StringBuilder, and StringBuffer

  3. The String Class • String are immutable objects • Create an instance of String with the keyword "new“ • Example: • String s = new String(); • String s = new String("abcdef"); • String s = "abcdef";

  4. The String Class

  5. The String Class

  6. The String Class

  7. The String Class • Another example:

  8. The String Class • Output:

  9. The String Class • And now?

  10. The String Class • Example:

  11. The String Class • Que imprime?:

  12. The String Class • Imprime "spring winter spring summer".

  13. Important facts about String and memory • JVM reserve a special area called "Sreing constant pool” • When the compiler encounters a string literal, it checks the pool to see if it exists. If found, the literal reference to the new addresses the existing chain (the existing chain has an additional reference.) [Immutable]

  14. new String

  15. String Class Methods

  16. String Methods • public char charAt(int index) String x = "airplane"; System.out.println( x.charAt(2) ); // output ‘r’

  17. String Methods • public String concat(String s) String x = "taxi"; System.out.println( x.concat(" cab") ); // output is "taxi cab"

  18. String Methods • public boolean equalsIgnoreCase(String s) String x = "Exit"; System.out.println(x.equalsIgnoreCase("EXIT"); // is "true“ System.out.println(x.equalsIgnoreCase("tixe")); // is "false"

  19. String Methods • public int length() String x = "01234567"; System.out.println( x.length() ); // returns "8"

  20. String Methods • public String replace(char old, char new) String x = "oxoxoxox"; System.out.println( x.replace('x', 'X') ); // output is "oXoXoXoX"

  21. String Methods • public String substring(int begin) • public String substring(int begin, int end) String x = "0123456789"; System.out.println( x.substring(5) ); // output is "56789" System.out.println( x.substring(5, 8)); // output is "567"

  22. String Methods • public String toLowerCase() String x = "A New Moon"; System.out.println( x.toLowerCase() ); // output is "a new moon"

  23. String Methods • public String toUpperCase() String x = "A New Moon"; System.out.println( x. toUpperCase() ); // output is “A NEW MOON"

  24. String Methods • public String trim() String x = " hi "; System.out.println( x + "x" ); // result is " hi x" System.out.println( x.trim() + "x"); // result is "hix"

  25. The StringBuffer and StringBuilder Classes • Java.lang.StringBuilder and java.lang.StringBuffer classes should be used when making several modifications to the strings. • Unlike String, objects of type StringBuffer and StringBuilder can be modified again and again without leaving behind discarded objects • A common use of StringBuffer and StringBuilder is on file I / S

  26. StringBuffer vs. StringBuilder • The StringBuilder class was added in Java 5. • StringBuilder is Not thread safe. In other words, their methods are not synchronized (Chapter 9) • Sun recommends that StringBuilder instead of StringBuffer whenever possible because StringBuilder will run faster.

  27. Using StringBuffer and StringBuilder • StringBuffer • StringBuilder

  28. Key methods of StringBuffer and StringBuilder • public synchronized StringBuffer append(String s) StringBuffer sb = new StringBuffer("set "); sb.append("point"); System.out.println(sb); // output is "set point“ StringBuffer sb2 = new StringBuffer("pi = "); sb2.append(3.14159f); System.out.println(sb2); // output is "pi = 3.14159"

  29. Key methods of StringBuffer and StringBuilder • public StringBuilder delete(int start, int end) StringBuilder sb = new StringBuilder("0123456789"); System.out.println(sb.delete(4,6)); // output is "01236789"

  30. Key methods of StringBuffer and StringBuilder • public StringBuilder insert(int offset, String s) StringBuilder sb = new StringBuilder("01234567"); sb.insert(4, "---"); System.out.println( sb ); // output is "0123---4567"

  31. Key methods of StringBuffer and StringBuilder • public synchronized StringBuffer reverse() StringBuffer s = new StringBuffer("A man a plan a canal Panama"); sb.reverse(); System.out.println(sb); // output: "amanaP lanac a nalp a nam A"

  32. Key methods of StringBuffer and StringBuilder • public String toString() StringBuffer sb = new StringBuffer("test string"); System.out.println( sb.toString() ); // output is "test string"

  33. File Navigation and I/O • File .- The API says that the kind of file is "an abstract representation n of a file and directory path. " The File class is not used for reading or writing data, is used to work at high level, creating new files, search files, delete files, create directories, and working with paths (path).

  34. File Navigation and I/O • FileReader .- This class is used to read character files. The method read() is very low, allowing you to read the entire flow (stream) character. FileReaders often surrounded by objects of more s high level, BufferedReaders, which improves performance and provides the means most convenient to work s with the data.

  35. File Navigation and I/O • BufferedReader.- In comparison with FileReaders n, BufferedReaders read into relatively large data file at a time, and maintains these data in a buffer. When prompted for the next character or line of data is retrieved from memory (minimizes the time) and then perform read operations. In addition s, BufferedReader provides methods more suitable as readLine(), giving the next line of arhiva

  36. File Navigation and I/O • FileWriter.- This class is used for writing character files. His method write() to write character (s) of strings or a file. FileWriter are usually surrounded by writers such as high-level objects or PrintWriters BufferedWriters, which provide better performance and higher, with more method flexible to write the data.

  37. File Navigation and I/O • BufferedWriter.- Compared with FileWriters, writes BufferedWriters relatively large pieces of data from one file at a time, minimizes the number of times of write operations to a file. In addition, the BufferedWriter class provides the method NewLine () that facilitates the creation of line separators automatically.

  38. File Navigation and I/O • PrintWriter.- This class has been improved significantly in Java 5. Because it has new methods and constructors (you can build a PrintWriter from a File or String). New methods like format (), printf () and append () PrintWriter make it very flexible and powerful.

  39. Creating files using the File class • File type objects are used to represent the current files (but not data files) or directories that exist on a physical computer disk.

  40. Creating files using the File class File • Solo se a creado el objeto File, no el archivo físico.

  41. Creating files using the File class File • The above code prints false, true, true • If you run a second time, print: true, false, true • boolean exists() .- This method returns true if it finds the specified file. • boolean createNewFile() .- This method creates a file if it is not exists. • boolean isDirectory() .- This method returns true if it finds the specified directory

  42. Using FileWriter & FileReader

  43. Using FileWriter & FileReader • Output: 12 howdy folks

  44. Writer & Reader Hierarchy

  45. Combining I/O • Example 1 :

  46. Combining I/O • Example 2 :

  47. Combining I/O • Example 3 :

More Related