Create folder path and new file for Java file operations

Source: Internet
Author: User

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?
  1. Package  Edu.tju.cs;
  2. Import  Java.io.File;
  3. Import  java.io.FileNotFoundException;
  4. Import  Java.io.FileWriter;
  5. Import  java.io.IOException;
  6. Import  Java.io.RandomAccessFile;
  7. Public class alltest {
  8. /*public static void Main (string[] args) {
  9. System.out.println ("****\\\\");
  10. String str = "A\\B\\B\\C.CSV|AAAAA";
  11. string[] chars = Str.split ("\\\\");
  12. string[] FileName = chars[3].split ("\ \"); /u002e
  13. string[] tmp = Chars[3].split ("\\u007c");//\\u007c
  14. For (String cha:chars) {
  15. System.out.println (cha);
  16.         }
  17. System.out.println (filename[0]);
  18. System.out.println (tmp[1]);
  19.     }*/  
  20. Public static void main (String args[])
  21. {
  22. StringBuffer filebuf=New stringbuffer ();
  23. String Filepar = "D:\\test"; //Folder path   
  24. File MyPath = new file (filepar);
  25. if (!mypath.exists ()) {//If this directory does not exist, the created
  26. Mypath.mkdir ();
  27. System.out.println ("Create folder path:"+ filepar);
  28. }
  29. //folder path exists in case   
  30. String filename = "test.txt"; //File name   
  31. Try  {  
  32. FileWriter FW = new FileWriter (filepar + "\" + filename,true);//Filepar + "\ \" + filename ,true
  33. //FileWriter is a cow, if the file name does not exist, first create re-read and write;   
  34. String originalline = "Aaaaaaaaaa" + "\ n"; //    
  35. SYSTEM.OUT.PRINTLN ("* * *"+ originalline);
  36. Fw.write (Originalline);
  37. //Close Write file, write only one line of data at a time, because a read file is only a single OD   
  38. Fw.close ();
  39. } catch (IOException e) {
  40. //TODO auto-generated catch block   
  41. E.printstacktrace ();
  42. }
  43. }
  44. }
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

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.