One: The problem
(1) Java If the folder path does not exist, create first;
(2) If the file name does not exist, first create the re-read and write, if there is a direct append, keyword True
(3)file MyPath = new file (Tofilepath);
if (!mypath.exists ()) {//If this directory does not exist, then create it//this thing can only resume level folder, level Two is not built .....
Mypath.mkdir ();
System.out.println ("Create Folder path:" + Tofilepath);
}
The code above can only create a first level directory, can not create a level two directory, that is, only the current level of the directory can be created, if the previous level of the directory does not exist, the creation fails; Of course, you can solve this problem by recursive creation.
File MyPath = new file (Tofilepath); Just pass the path to the private property of the File.java class, see the source code, and do not open the file, so do not throw FileNotFoundException
(4) //write file name processing
String filen = tofilepath + "\ \" + FileName;
bufferedwriter writer = new BufferedWriter (new FileWriter (New File (Filen)));
Compare with the following code
File File=new file (OriginalPath);
if (File.isfile () && file.exists ()) {//Determine if the file exists
InputStreamReader Read = new InputStreamReader (
New FileInputStream (file), encoding);//considering the encoding format
BufferedReader bufferedreader = new BufferedReader (read);
}
In fact, FileWriter is packaged (called) the FileOutputStream () function source code is as follows: So the two are similar, but the stream can filter the code.
Public FileWriter (file file) throws IOException {
Super (new FileOutputStream (file));
}
Two: Code implementation
[Java]View PlainCopyPrint?
- 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 ("\ \"); /u002e
- string[] tmp = Chars[3].split ("\\u007c");//\\u007c
- For (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, the created
- 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 + "\" + filename,true);//Filepar + "\ \" + filename ,true
- //FileWriter is a cow, if the file name does not exist, first create re-read and write;
- String originalline = "Aaaaaaaaaa" + "\ n"; //
- SYSTEM.OUT.PRINTLN ("* * *"+ originalline);
- Fw.write (Originalline);
- //Close Write file, write 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 block
- E.printstacktrace ();
- }
- }
- }
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 ("\ \"); /u002e string[] tmp = Chars[3].split ("\\u007c");//\\u007c for (String cha:chars) {SYSTEM.OUT.PR Intln (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); }//FolderThe path exists in the case of String filename = "test.txt";//File name try {FileWriter fw = new FileWriter (fil EPar + "\" + filename,true),//Filepar + "\ \" + filename,true//FileWriter is the cow, if the file name does not exist, first create and read and write; Append write, the keyword true means append String originalline = "aaaaaaaaaa" + "\ n";//System.out.println ("* * *" + ori Ginalline); Fw.write (Originalline); Closes the write file, writes 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 block E.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