Chapter 9 input/output stream 1-4

Source: Internet
Author: User
Code:
  1. Package ch09;
  2. Import java. Io. bufferedreader;
  3. Import java. Io. bufferedwriter;
  4. Import java. Io. chararrayreader;
  5. Import java. Io. chararraywriter;
  6. Import java. Io. file;
  7. Import java. Io. fileinputstream;
  8. Import java. Io. filenotfoundexception;
  9. Import java. Io. filereader;
  10. Import java. Io. filewriter;
  11. Import java. Io. ioexception;
  12. Import java. util. collections;
  13. Class homeworkch09 {
  14. Public homeworkch09 (){
  15. System. Out. println ("Chapter 9 after-school exercise questions. ");
  16. }
  17. // 1. Write an application to read the content of a text file.
  18. Public void key01 (){
  19. File file = new file ("E:/workspaced/javahomework", "hello.txt"); // same directory as the current application
  20. // System. Out. println (file. getpath ());
  21. Byte B [] = "DD". getbytes ();
  22. Try {
  23. Fileinputstream FCM = new fileinputstream (File );
  24. Int n = 0;
  25. // Test garbled characters
  26. String STR = NULL;
  27. While (n = FCM. Read (B, 0, 2 ))! =-1) {// read two bytes to prevent garbled characters
  28. STR = new string (B, 0, N );
  29. System. Out. Print (STR );
  30. }
  31. } Catch (filenotfoundexception e) {// an exception cannot be found in the file.
  32. E. printstacktrace ();
  33. } Catch (ioexception e ){
  34. E. printstacktrace ();
  35. }
  36. }
  37. // 2. Write an application that saves 10 lines of text entered by the user from the keyboard to the file.
  38. Public void key02 (){
  39. Filewriter file = NULL;
  40. Try {
  41. File = new filewriter ("mydoc.txt ");
  42. Bufferedwriter BW = new bufferedwriter (File );
  43. For (INT I = 0; I <10; I ++ ){
  44. Internal reader = new external (system. In );
  45. Bw. Write (reader. nextline ());
  46. Bw. Flush ();
  47. }
  48. File. Close ();
  49. Bw. Close ();
  50. } Catch (ioexception e ){
  51. E. printstacktrace ();
  52. }
  53. }
  54. // 3. Use the array audio stream to write Russian letters into the memory, and then read them from the memory.
  55. /** The answer output contains 32 Russian letters and 33 letters. Although the answer and answer are two different letters, they are only different in the spoken language, when writing or printing, you can simply turn it into plain. */
  56. Public void key03 (){
  57. Int startposition = (INT) 'hangzhou'; // first letter in Russian
  58. Int endposition = (INT) 'hangzhou'; // last Russian letter
  59. System. Out. println ("Russian letters in total:" + (startposition-endposition ));
  60. // For (INT I = 1040; I <1072; I ++ ){
  61. // System. Out. Print (char) I );
  62. //}
  63. Int n =-1;
  64. Chararraywriter out = new chararraywriter ();
  65. For (INT I = startposition; I <= endposition; I ++ ){
  66. Out. Write (I );
  67. }
  68. Chararrayreader in = new chararrayreader (Out. tochararray ());
  69. Try {
  70. While (n = in. Read ())! =-1 ){
  71. If (N % 2 = 0 ){
  72. System. Out. println ("/N ");
  73. }
  74. System. Out. printf ("/t position % d, character/'% C/'", N, (char) N );
  75. }
  76. } Catch (ioexception e ){
  77. E. printstacktrace ();
  78. }
  79. }
  80. // 4. Write an application, store the 10 integers entered by the user on the keyboard into the file, and read them in sequence.
  81. Public void key04 () {// ==================== use a buffer stream ==============================
  82. File file = new file ("key04.txt ");
  83. Try {
  84. Filewriter out = new filewriter (File );
  85. Bufferedwriter out2 = new bufferedwriter (out );
  86. Int I = 0;
  87. While (I <2 ){
  88. S = new partition (system. In );
  89. String STR = S. nextline ();
  90. Out2.write (STR );
  91. Out2.newline (); // write a response
  92. Out2.flush (); // refresh the buffer
  93. I ++;
  94. }
  95. Filereader in = new filereader (File );
  96. Bufferedreader in2 = new bufferedreader (in );
  97. String integers = NULL;
  98. While (integers = in2.readline ())! = NULL ){
  99. System. Out. println (integers );
  100. }
  101. } Catch (filenotfoundexception e ){
  102. E. printstacktrace ();
  103. } Catch (ioexception e ){
  104. E. printstacktrace ();
  105. }
  106. }
  107. }
  108. Public class ch09_1 {
  109. Public static void main (string [] ARGs ){
  110. Homeworkch09 HK = new homeworkch09 ();
  111. // Test location
  112. }
  113. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.