I. Obtaining information entered by the console user
/** gets the information entered by the console user
* @return
* @throws IOException
*/
Public String Getinputmessage () throws IOException ... {
System.out.println ("Please enter your command:");
byte buffer[]=new byte[1024];
int count=system.in.read (buffer);
Char[] ch=new char[count-2];//last two digits as Terminator, delete no
for (int i=0;i<count-2;i++)
ch[i]= (char) buffer[i];
String Str=new string (ch);
return str;
}
Can return the user input information, the disadvantage is not support Chinese input, need further improvement.
Two. copy files
1. Copying files in a file flow manner
* * Copy files as file stream
* @param src File source directory
* @param dest File destination directory
* @throws IOException
*/
public void CopyFile (String src,string dest) throws IOException ... {
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 has been tested to support Chinese processing, and can be replicated in a variety of types, such as Txt,xml,jpg,doc and other formats three. Write a file
1. Use PrintStream to write files
/**
* Sample file output
*/
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=new FileOutputStream (file,true);
for (int i=0;i<10000;i++) ... {
StringBuffer sb=new StringBuffer ();
Sb.append ("This is the first" +i+ "line: The various methods described earlier are not closed, why is always a strange problem");
Out.write (Sb.tostring (). GetBytes ("Utf-8"));
}
Out.close ();
}
This method can be used to set the code to effectively solve the Chinese problem.