1. Obtain information entered by the console user
Public String getinputmessage () throws ioexception ...{
System. Out. println ("Enter your command ");
Byte buffer [] = new byte [1024];
Int COUNT = system. In. Read (buffer );
Char [] CH = new char [count-2]; // the last two digits are the Terminator, delete not
For (INT I = 0; I <count-2; I ++)
Ch [I] = (char) buffer [I];
String STR = new string (CH );
Return STR;
}
The user input information can be returned. The disadvantage is that Chinese input is not supported and further improvement is needed.
Ii. Copy an object
1. copy a file as a file stream
Public void copyfile (string SRC, string DEST) throwsioexception ...{
Fileinputstream in = new fileinputstream (SRC );
File file = new file (DEST );
If (! File. exists ())
File. createnewfile ();
Fileoutputstream out = new fileoutputstream (File );
Int C;
Byte buffer [] = new byte [1024];
While (C = in. Read (buffer ))! =-1 )...{
For (INT I = 0; I <C; I ++)
Out. Write (buffer [I]);
}
In. Close ();
Out. Close ();
}
This method is tested and supports Chinese processing and can be copied to multiple formats, such as txt, XML, JPG, and Doc.
3. Write files
1. Use printstream to write files
Public void printstreamdemo ()...{
Try ...{
Fileoutputstream out = new fileoutputstream ("D:/test.txt ");
Printstream P = new printstream (out );
For (INT I = 0; I <10; I ++)
P. println ("this is" + I + "line ");
} Catch (filenotfoundexception e )...{
E. printstacktrace ();
}
}
2. Use stringbuffer to write files
Public void stringbufferdemo () throws ioexception ......{
File file = new file ("/root/SMS. log ");
If (! File. exists ())
File. createnewfile ();
Fileoutputstream out = newfileoutputstream (file, true );
For (INT I = 0; I <10000; I ++ )......{
Stringbuffer sb = new stringbuffer ();
SB. append ("This is the first line" + I + ": the methods described above are not used. Why is it always a strange question ");
Out. Write (sb. tostring (). getbytes ("UTF-8 "));
}
Out. Close ();
}
This method can be used to set the encoding to effectively solve Chinese problems.
Iv. File rename
Public void renamefile (string path, string oldname, stringnewname )...{
If (! Oldname. Equals (newname)... {// It is necessary to rename a new file name that is different from the previous one.