1. Print command line parameters
Code:
- Import java. Io .*;
- Public class test1 {
- Public static void main (string [] ARGs ){
- For (INT I = 0; I <args. length; I ++ ){
- System. Out. Print (ARGs [I]);
- }
- System. Out. println ();
- }
- }
2. output the content in a file to the console as is
Code:
- Import java. Io .*;
- Public class Test2 {
- Public static void main (string [] ARGs ){
- System. Out. println ("Please enter the file path :");
- Try {
- String filename = "";
- While (true ){
- Int readbyte = system. In. Read ();
- If (readbyte =-1 | readbyte = '/R '){
- Break;
- } Else {
- Filename + = (char) readbyte;
- }
- }
- Char [] buffer = new char [20];
- Filereader Fr = new filereader (filename );
- While (true ){
- Int length = Fr. Read (buffer );
- If (length <0 ){
- Break;
- } Else {
- String text = new string (buffer, 0, length );
- System. Out. Print (text );
- }
- }
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- }
- }
3. Write the content in one file to another file
Code:
- Import java. Io .*;
- Public class test3 {
- Public static void main (string [] ARGs ){
- Filerwtest FRW = new filerwtest ();
- }
- }
- Class filerwtest {
- Bufferedreader BR;
- Bufferedwriter bw;
- File in = new file ("input.txt ");
- File out = new file ("output.txt ");
- Char [] cbuf = new char [1024];
- Int off = 0;
- Public filerwtest (){
- Try {
- If (! In. exists ()){
- In. createnewfile ();
- }
- If (! Out. exists ()){
- Out. createnewfile ();
- }
- BR = new bufferedreader (New inputstreamreader (New fileinputstream (in )));
- BW = new bufferedwriter (New outputstreamwriter (New fileoutputstream (out )));
- While (Br. Readline ()! = NULL ){
- BR. Read (cbuf, off, off + 1024 );
- Bw. Write (cbuf, off, off + 1024 );
- Off + = 1024;
- }
- BR. Close ();
- Bw. Close ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- }
- }
4. output the daffodils to a file.
Code:
- Import java. Io .*;
- Public class test4 {
- Public static void main (string [] ARGs ){
- Try {
- Byte [];
- Fileoutputstream Fos = new fileoutputstream ("data.txt", true );
- For (INT I = 100; I <= 999; I ++ ){
- Int A = I/100;
- Int B = I %100/10;
- Int c = I % 10;
- If (I = (INT) (math. Pow (A, 3) + math. Pow (B, 3) + math. Pow (C, 3 ))){
- String S = new string (I + "=" + A + "*" + A + "*" + A +
- "+" + B + "*" + B + "*" + B +
- "+" + C + "*" + C + "*" + C + "/N ");
- By = S. getbytes ();
- FOS. Write (by, 0, by. Length );
- } Else {
- Continue;
- }
- }
- FOS. Close ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- }
- }
5. Read the content of a file, convert uppercase letters into lowercase letters, and output them to another file.
Code:
- Import java. Io .*;
- Public class test5 {
- Static bufferedreader BR;
- Static bufferedwriter bw;
- Static file in = new file ("test5.java ");
- Static file out = new file ("result.txt ");
- Static string str1;
- Static string str2;
- Public static void main (string [] ARGs ){
- Try {
- If (! In. exists ()){
- In. createnewfile ();
- }
- If (! Out. exists ()){
- Out. createnewfile ();
- }
- BR = new bufferedreader (New inputstreamreader (New fileinputstream (in )));
- BW = new bufferedwriter (New outputstreamwriter (New fileoutputstream (out )));
- While (str1 = Br. Readline ())! = NULL ){
- Str2 = str1.tolowercase ();
- Bw. Write (str2, 0, str2.length ());
- Bw. newline ();
- }
- BR. Close ();
- Bw. Close ();
- } Catch (ioexception e ){
- E. printstacktrace ();
- }
- }
- }