Java Create txt file and deposit content __java Learning

Source: Internet
Author: User
Tags stringbuffer
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;

Import Java.io.PrintWriter;
	public class Txtexport {private static String path = ' d:/';

	private static String filenametemp;
		public static void Main (string[] args) throws IOException {txtexport.creattxtfile ("Hello");
	Txtexport.writetxtfile ("Hello"); /** * Create File * * @throws IOException */public static Boolean Creattxtfile (String name) throws IOException
		{Boolean flag = false;
		filenametemp = path + name + ". txt";
		File filename = new file (filenametemp);
			if (!filename.exists ()) {filename.createnewfile ();
		Flag = true;
	return flag; /** * Write File * * @param newstr * NEW content * @throws IOException/public static Boolean Writetxtfile
		(String newstr) throws IOException {//Read the original file contents first, then write to Boolean flag = false;
	String Filein = newstr + "\ r \ n";	String temp = "";
		FileInputStream FIS = null;
		InputStreamReader ISR = null;

		BufferedReader br = null;
		FileOutputStream fos = null;
		PrintWriter pw = null;
			try {//filename path File File = new file (filenametemp);
			Reads the file into the input stream FIS = new FileInputStream (file);
			ISR = new InputStreamReader (FIS);
			br = new BufferedReader (ISR);

			StringBuffer buf = new StringBuffer (); Save the original contents of the file for (int j = 1; (temp = Br.readline ())!= null;
				J + +) {buf = Buf.append (temp); System.getproperty ("Line.separator")//the separator between line and line equals "\ n" buf = Buf.append (System.getproperty ("Line.separator")
			);

			} buf.append (Filein);
			FOS = new FileOutputStream (file);
			PW = new PrintWriter (FOS);
			Pw.write (Buf.tostring (). ToCharArray ());
			Pw.flush ();
		Flag = true;
		The catch (IOException E1) {//TODO automatically generates catch blocks throw E1;
			finally {if (PW!= null) {pw.close ();
			} if (fos!= null) {fos.close ();
			} if (br!= null) {br.close ();
	}		if (ISR!= null) {isr.close ();
			} if (FIS!= null) {fis.close ();
	} return flag;
 }

}

Related Article

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.