JAVA input/output stream experiment

Source: Internet
Author: User

1. Print command line parameters

Code:
  1. Import java. Io .*;
  2. Public class test1 {
  3. Public static void main (string [] ARGs ){
  4. For (INT I = 0; I <args. length; I ++ ){
  5. System. Out. Print (ARGs [I]);
  6. }
  7. System. Out. println ();
  8. }
  9. }

2. output the content in a file to the console as is

Code:
  1. Import java. Io .*;
  2. Public class Test2 {
  3. Public static void main (string [] ARGs ){
  4. System. Out. println ("Please enter the file path :");
  5. Try {
  6. String filename = "";
  7. While (true ){
  8. Int readbyte = system. In. Read ();
  9. If (readbyte =-1 | readbyte = '/R '){
  10. Break;
  11. } Else {
  12. Filename + = (char) readbyte;
  13. }
  14. }
  15. Char [] buffer = new char [20];
  16. Filereader Fr = new filereader (filename );
  17. While (true ){
  18. Int length = Fr. Read (buffer );
  19. If (length <0 ){
  20. Break;
  21. } Else {
  22. String text = new string (buffer, 0, length );
  23. System. Out. Print (text );
  24. }
  25. }
  26. } Catch (ioexception e ){
  27. E. printstacktrace ();
  28. }
  29. }
  30. }

3. Write the content in one file to another file

Code:
  1. Import java. Io .*;
  2. Public class test3 {
  3. Public static void main (string [] ARGs ){
  4. Filerwtest FRW = new filerwtest ();
  5. }
  6. }
  7. Class filerwtest {
  8. Bufferedreader BR;
  9. Bufferedwriter bw;
  10. File in = new file ("input.txt ");
  11. File out = new file ("output.txt ");
  12. Char [] cbuf = new char [1024];
  13. Int off = 0;
  14. Public filerwtest (){
  15. Try {
  16. If (! In. exists ()){
  17. In. createnewfile ();
  18. }
  19. If (! Out. exists ()){
  20. Out. createnewfile ();
  21. }
  22. BR = new bufferedreader (New inputstreamreader (New fileinputstream (in )));
  23. BW = new bufferedwriter (New outputstreamwriter (New fileoutputstream (out )));
  24. While (Br. Readline ()! = NULL ){
  25. BR. Read (cbuf, off, off + 1024 );
  26. Bw. Write (cbuf, off, off + 1024 );
  27. Off + = 1024;
  28. }
  29. BR. Close ();
  30. Bw. Close ();
  31. } Catch (ioexception e ){
  32. E. printstacktrace ();
  33. }
  34. }
  35. }

4. output the daffodils to a file.

Code:
  1. Import java. Io .*;
  2. Public class test4 {
  3. Public static void main (string [] ARGs ){
  4. Try {
  5. Byte [];
  6. Fileoutputstream Fos = new fileoutputstream ("data.txt", true );
  7. For (INT I = 100; I <= 999; I ++ ){
  8. Int A = I/100;
  9. Int B = I %100/10;
  10. Int c = I % 10;
  11. If (I = (INT) (math. Pow (A, 3) + math. Pow (B, 3) + math. Pow (C, 3 ))){
  12. String S = new string (I + "=" + A + "*" + A + "*" + A +
  13. "+" + B + "*" + B + "*" + B +
  14. "+" + C + "*" + C + "*" + C + "/N ");
  15. By = S. getbytes ();
  16. FOS. Write (by, 0, by. Length );
  17. } Else {
  18. Continue;
  19. }
  20. }
  21. FOS. Close ();
  22. } Catch (ioexception e ){
  23. E. printstacktrace ();
  24. }
  25. }
  26. }

5. Read the content of a file, convert uppercase letters into lowercase letters, and output them to another file.

Code:
  1. Import java. Io .*;
  2. Public class test5 {
  3. Static bufferedreader BR;
  4. Static bufferedwriter bw;
  5. Static file in = new file ("test5.java ");
  6. Static file out = new file ("result.txt ");
  7. Static string str1;
  8. Static string str2;
  9. Public static void main (string [] ARGs ){
  10. Try {
  11. If (! In. exists ()){
  12. In. createnewfile ();
  13. }
  14. If (! Out. exists ()){
  15. Out. createnewfile ();
  16. }
  17. BR = new bufferedreader (New inputstreamreader (New fileinputstream (in )));
  18. BW = new bufferedwriter (New outputstreamwriter (New fileoutputstream (out )));
  19. While (str1 = Br. Readline ())! = NULL ){
  20. Str2 = str1.tolowercase ();
  21. Bw. Write (str2, 0, str2.length ());
  22. Bw. newline ();
  23. }
  24. BR. Close ();
  25. Bw. Close ();
  26. } Catch (ioexception e ){
  27. E. printstacktrace ();
  28. }
  29. }
  30. }

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.