android write file

	public static void writefile(String text, String fileName){
		File externalStorageDir = Environment.getExternalStorageDirectory();
        File myFile = new File(externalStorageDir , fileName);//fileName: 1.txt
        if(myFile.exists())
        {
           try
           {
	        FileOutputStream fostream = new FileOutputStream(myFile);
	        OutputStreamWriter oswriter = new OutputStreamWriter(fostream); 
	        BufferedWriter bwriter = new BufferedWriter(oswriter);   
	        bwriter.write(text);
	        bwriter.newLine();            
	        bwriter.close(); 
	        oswriter.close(); 
	        fostream.close();
           }
	        catch (IOException e)
	        {
	            e.printStackTrace();
	        }
        }
        else
        {
            try {
                myFile.createNewFile();
            }
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }

猜你喜欢

转载自dasheny.iteye.com/blog/2170815