Here you can find the source of saveFile(File f, String fileName, File desDir)
Parameter | Description |
---|---|
f | f |
fileName | fileName |
desDir | desDir |
Parameter | Description |
---|---|
IOException | IOException |
public static void saveFile(File f, String fileName, File desDir) throws IOException
//package com.java2s; /*//from w ww . jav a 2 s. c om * Copyright (C) 2010 Viettel Telecom. All rights reserved. * VIETTEL PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * . */ public static final int BUFFER_SIZE = 1024 * 8; /** * saveFile * * @param f f * @param fileName fileName * @param desDir desDir * @throws IOException IOException */ public static void saveFile(File f, String fileName, File desDir) throws IOException { InputStream stream = new FileInputStream(f); OutputStream out = new FileOutputStream(desDir.getAbsolutePath() + File.separatorChar + fileName); int bytesRead; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) { out.write(buffer, 0, bytesRead); } stream.close(); out.close(); } /** * Save file * * @param f f * @param fileName fileName * @param desDir desDir * @param override override * @throws IOException IOException */ public static void saveFile(File f, String fileName, File desDir, boolean override) throws IOException { File file = new File(desDir.getAbsolutePath() + File.separatorChar + fileName); if (override) { if (file.exists()) { file.delete(); } } InputStream stream = new FileInputStream(f); OutputStream out = new FileOutputStream(file); int bytesRead; byte[] buffer = new byte[BUFFER_SIZE]; while ((bytesRead = stream.read(buffer, 0, BUFFER_SIZE)) != -1) { out.write(buffer, 0, bytesRead); } stream.close(); out.close(); } /** * Deletes a file or a directory. * * @param file the file or directory which should be deleted. * @return true when the file could be deleted */ public static boolean delete(File file) { if (file.isDirectory()) { String[] children = file.list(); for (int i = 0; i < children.length; i++) { boolean success = delete(new File(file, children[i])); if (!success) { return false; } } } // The directory is now empty so delete it return file.delete(); } }