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