Android creates folders and files and writes data

Source: Internet
Author: User

 

Package elwin. fei. mobileaudio;

 

Import java. io. BufferedWriter;

Import java. io. File;

Import java. io. FileWriter;

Import java. io. IOException;

Import java. text. SimpleDateFormat;

 

Public class CreateFiles {

 

String filenameTemp = Info. audioPath + "/hhaudio" + ". txt ";

// Create folders and files

Public void CreateText () throws IOException {

File file = new File (Info. audioPath );

If (! File. exists ()){

Try {

// Create a folder Based on the specified path

File. mkdirs ();

} Catch (Exception e ){

// TODO: handle exception

}

}

File dir = new File (filenameTemp );

If (! Dir. exists ()){

Try {

// Create a file in the specified folder

Dir. createNewFile ();

} Catch (Exception e ){

}

}

 

}

// Write data to the created File

Public void print (String str ){

FileWriter fw = null;

BufferedWriter bw = null;

String datetime = "";

Try {

SimpleDateFormat tempDate = new SimpleDateFormat ("yyyy-MM-dd" + ""

+ "Hh: mm: ss ");

Datetime = tempDate. format (new java. util. Date (). toString ();

Fw = new FileWriter (filenameTemp, true );//

// Create a FileWriter object to write data to the volume stream.

Bw = new BufferedWriter (fw); // buffer the output of the file

String myreadline = datetime + "[]" + str;

Bw. write (myreadline + "\ n"); // write the file

Bw. newLine ();

Bw. flush (); // refresh the buffer of the stream

Bw. close ();

Fw. close ();

} Catch (IOException e ){

// TODO Auto-generated catch block

E. printStackTrace ();

Try {

Bw. close ();

Fw. close ();

} Catch (IOException e1 ){

// TODO Auto-generated catch block

}

}

}

}

 

From Feige's column

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.