Here you can find the source of saveFile(String dir, String fileName, String data)
public static boolean saveFile(String dir, String fileName, String data)
//package com.java2s; /*// w w w. j ava2 s . c o m * Copyright (C) 2017, Megatron King * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static boolean saveFile(String dir, String fileName, String data) { return saveFile(dir, fileName, data, false, false); } private static boolean saveFile(String dir, String fileName, String data, boolean mkdir, boolean append) { File dirFile = new File(dir); if (!dirFile.exists()) { if (!mkdir) { return false; } createFolders(dirFile); } File dest = new File(dir, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(dest, append); fos.write(data.getBytes()); fos.flush(); return true; } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return false; } public static boolean createFolders(File folder) { return folder.mkdirs(); } }