Create folder path and new file for Java file operations

Source: Internet
Author: User

One: The problem

Java if the folder path does not exist, first create, if the file name does not exist, the first to create re-read and write, if there is a direct append, the keyword true indicates append

Two: Code implementation

Package Edu.tju.cs;import Java.io.file;import Java.io.filenotfoundexception;import java.io.filewriter;import Java.io.ioexception;import Java.io.randomaccessfile;public class AllTest {/*public static void main (string[] args) { System.out.println ("****\\\\"); String str = "A\\B\\B\\C.CSV|AAAAA"; string[] chars = Str.split ("\\\\"); string[] FileName = chars[3].split ("\ \"); /u002estring[] tmp = Chars[3].split ("\\u007c");//\\u007cfor (String cha:chars) {System.out.println (cha);} System.out.println (Filename[0]); System.out.println (tmp[1]);}    */public static void Main (String args[]) {stringbuffer filebuf=new stringbuffer ();        String Filepar = "d:\\test";//folder path File MyPath = new file (Filepar);            if (!mypath.exists ()) {//If this directory does not exist, then create the Mypath.mkdir ();        System.out.println ("Create Folder path:" + Filepar); }//folder path exists in case String filename = "test.txt";//File name try {FileWriter fw = new FileWriter (filepar + "\ \" + fil Ename,true);//Filepar + "\ \" + filename,true        FileWriter is a cow, if the file name does not exist, first create re-read and write, if there is a direct append write, the keyword true means append string originalline = "aaaaaaaaaa" + "\ n";//Sy        STEM.OUT.PRINTLN ("* * *" + originalline);        Fw.write (Originalline); Closes the write file, writing only one line of data at a time, because a read file is only a single od fw.close ();}  catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} }}

AnywayFileWriter is a cow, if the file name does not exist, first create re-read and write, if there is a direct append write, the keyword true means append but he can only one line of read and write, rather than the generation of buffer read and write efficiency high

Java a variety of ways to read files, append file content, to file the various operations see: Java a variety of ways to read files, append file content, various operations on the file

Create folder path and new file for Java file operations

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.