Basic operations for writing files in Java

Source: Internet
Author: User

Import Java.io.bufferedwriter;import java.io.file;import Java.io.filewriter;import Java.io.ioexception;import Java.io.printwriter;public class FileIO {/** * @param args * @throws ioexception */public static void Main (string[] args) Throws IOException {//1. Create a folder mkdir//You can create a folder by this method if the path exists ... Mkdirs path does not exist or can be created by this method, first create the path and then create the folder file Myfolderpath = new file ("D:\\myeclipse2013work\\test\\test"); try {if (! Myfolderpath.exists ()) {Myfolderpath.mkdir ();}} catch (Exception e) {System.out.println ("New directory Operation error"); E.printstacktrace ();} 2. The difference between creating files FileWrite and Printwrite requires attention to file Myfilepathfile = new file ("Test.txt"), and try {if (!myfilepathfile.exists ()) { Myfilepathfile.createnewfile ();} FileWriter resultfile = new FileWriter (myfilepathfile); PrintWriter myFile = new PrintWriter (resultfile); Myfile.println ("New file Operation"); Myfile.println ("New file Operation"); Myfile.println ( "New file Operation"), Myfile.println ("New file Operation"), Myfile.write ("SSS", 1, 1), Myfile.println ("New file Operation"); Resultfile.write (" Sssssssssssss "); Resultfile.close ();} catch (ExceptiOn e) {//Todo:handle exceptionSystem.out.println ("New file Operation Error"); E.printstacktrace ();} 3. FileWrite Combine bufferedwriter print text line testbufferedwriter ();//4. Get the current project path file directory = new file ("");//parameter is empty string Coursefile = Directory.getcanonicalpath (); System.out.println (coursefile);} public static void Testbufferedwriter () {try {String FilePath = "Test1.txt"; File MyFile = new file (FilePath), if (!myfile.exists ()) {myfile.createnewfile ();} FileWriter resultfile = new FileWriter (myFile); BufferedWriter buffer = new BufferedWriter (resultfile); String filecontent = "This is my name./n, we are isoftsotne.com/n Chinesebillboy @ here. /n "; String contentstring = ""; int flag = 0;boolean Isgo = true;while (flag = Filecontent.indexof ("/n"))! =-1 && isgo {contentstring = filecontent.substring (0, flag); Buffer.write (contentstring); Buffer.newline (); Buffer.flush (); if ( Flag + 2 >= filecontent.length ()) {Isgo = false;} else {filecontent = filecontent.substring (flag + 2);}} Buffer.flush (); Resultfile.flush(); Buffer.close (); Resultfile.close ();} catch (IOException IoE) {ioe.printstacktrace ();} finally {}}}

  

Basic operations for writing files in Java

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.