Java I/O operations, reading and writing files

Source: Internet
Author: User

Package up.down.file;

Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.io.OutputStream;
Import Java.io.OutputStreamWriter;
Import java.io.UnsupportedEncodingException;
Import Java.nio.Buffer;
Import Java.util.Date;

Import Org.apache.axis.message.InputStreamBody;
Import Org.apache.commons.fileupload.FileItemFactory;
Import Org.apache.commons.fileupload.FileUpload;
Import Org.apache.commons.fileupload.ProgressListener;
Import Org.apache.commons.fileupload.disk.DiskFileItemFactory;
Import Org.apache.commons.fileupload.servlet.ServletFileUpload;
Import Org.apache.struts.upload.DiskFile;

public class Test {
public static void Main (string[] args) {

File File = new file ("./build/period not survival into gold registered account. xml");
File WriteFile = new File ("./build/writefile.txt");
String upload= "f:/period is not survival into the gold registered account. Xml.bak";
if (file.exists ()) {
SYSTEM.OUT.PRINTLN ("---> Document exists");
try {
ReadFile (file); Read
Writfile (writefile,readfile (file)); Creating file Writes
UploadFile (upload); Uploading files
catch (Exception e) {
E.printstacktrace ();
}
}else{
System.out.println ("--> file does not exist exists");
}
}
Reading files
public static StringBuffer ReadFile (file file) throws ioexception{
System.out.println ("Read start time:" +new Date ());
BufferedReader br = new BufferedReader (new FileReader (file));
SYSTEM.OUT.PRINTLN ("File Size:" +file.length ()/1000+ "KB");
StringBuffer line = new StringBuffer ();
String str = NULL;
while ((Str=br.readline ())!=null) {
Line.append (str);
Line.append ("\ n");
}
SYSTEM.OUT.PRINTLN ("--->" +line);
SYSTEM.OUT.PRINTLN ("read end time =" +new Date ());
Br.close ();
return line;
}
Write the file to save the contents of the read file in the engineering directory
public static void Writfile (File file,stringbuffer sb) throws exception{
FileWriter FW = new FileWriter (file);
Fw.getencoding (). GetBytes ("Utf-8");
Fw.write (Sb.tostring ());
System.out.println ("-------> Write succeeded. ");
System.out.println ("Write a file of size" +file.length ()/1000+ "KB");
Fw.close ();
}

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.