Java Operations File Encyclopedia

Source: Internet
Author: User

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.

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.