Summary of the Development and packaging of a Java SWT desktop program (2)

Source: Internet
Author: User

Iii. File and stream problems

About the operation of files, an Elder wrote very full, I copy: http://www.cnblogs.com/lovebread/archive/2009/11/23/1609122.html

1) load all files under the specified directory

 1           //  Get the target file to be imported  2 File fromfile = New  File (frompath );  3           If (! Fromfile. isdirectory ()){  4               Throw  New Exception ("Please specify a folder! " );  5   }  6           //  Load files  7 File [] FS = Fromfile. listfiles ();  8 List <string> filenames = New Arraylist <string> (); //  File Name  9 List <File> files =New Arraylist <File> (); //  File  10           //  System. Out. println ("fs. Length -->" + FS. Length );  11           For  (File F: FS ){  12 String fn = F. getname ();  13               If ((! F. isdirectory () & (fn. endswith (". txt "))){ // End with .txt, not a folder  14                   Int I = fn. lastindexof ("." );  15                   If (I>-1) & I < FN. Length ())  16 Fn = fn. substring (0 , I );  17                   //  System. Out. println ("F. getname -->" + FN );  18   Filenames. Add (FN ); 19   Files. Add (f );  20   }  21 }

2) write files

 1           //  Copy the file to the specified directory, get the directory first, and then copy it. Note that you must first create a new file under the obtained directory, then you can write the content of the Java file object to this file.  2 String tofilepath = sysroot + "\ course \ User \\" ;  3           Int I = 0;  4 Bufferedinputstream inbuff = Null  ;  5 Bufferedoutputstream outbuff = Null  ;  6           For (I = 0; I <files. Size (); I ++ ){  7 File file = New File (tofilepath, filenames. Get (I) + ". Le" );  8              If (! File. exists ()){  9   File. createnewfile ();  10   }  11 Inbuff = New Bufferedinputstream ( New  Fileinputstream (files. Get (I )));  12 Outbuff = New Bufferedoutputstream ( New Fileoutputstream (File ));  13               Byte [] Buff = New   Byte [1024 ];  14               Int Len = 0 ;  15               While (LEN = inbuff. Read (buff ))! =-1 ){  16 Outbuff. Write (buff, 0 , Len );  17  }  18   Outbuff. Flush ();  19   }  20           If (Inbuff! = Null  )  21   Inbuff. Close ();  22           If (Outbuff! = Null  )  23 Outbuff. Close ();

3) wrap the content in the file:

String nextline = system. getproperty ("line. separator"); // It is used for line feed. This must be used together with the following, and the line feed will only be generated when it is written to a file.

In Windows, \ r \ n is a line break, so it can also be:String newxline = "\ r \ n ";

4) write files:

String tempstr = "This is the first line! ";
Tempstr + = nextline + "this is the second line! "
Bufferedoutputstream writer = new bufferedoutputstream (New fileoutputstream (CONF); writer. Write (tempstr. getbytes (), 0, tempstr. Length ());

Note: writer. Write (tempstr. getbytes (), 0, tempstr );

No: writer. Write (tempstr );

If it is the following, the file content will not wrap

 

----- Only used for learning records.

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.