Description
Save binary contents of an URL into a file.
License
Open Source License
Parameter
Parameter | Description |
---|
url | This URL will be loaded. |
file | Write into this file. |
Exception
Parameter | Description |
---|
IOException | Reading or writing failed. |
Declaration
public static void saveFile(final URL url, final File file) throws IOException
Method Source Code
//package com.java2s;
/* This file is part of the project "Hilbert II" - http://www.qedeq.org
*
* Copyright 2000-2013, Michael Meyling <mime@qedeq.org>.
*
* "Hilbert II" is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*///from ww w . j a v a 2 s. c o m
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.URL;
public class Main {
/**
* Save binary contents of an URL into a file. Existing files are overwritten.
*
* @param url This URL will be loaded.
* @param file Write into this file.
* @throws IOException Reading or writing failed.
*/
public static void saveFile(final URL url, final File file) throws IOException {
saveFile(url.openStream(), file);
}
/**
* Save binary contents of an input stream into a file. The input stream is closed even
* if exceptions occur. Existing files are overwritten.
* @param in Read this stream.
* @param file Write into this file.
*
* @throws IOException Reading or writing failed.
*/
public static void saveFile(final InputStream in, final File file) throws IOException {
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
final byte[] data = new byte[8 * 1024];
int length;
while ((length = in.read(data)) != -1) {
out.write(data, 0, length);
}
} finally {
close(in);
close(out);
}
}
/**
* Saves a <code>String</code> into a file. Existing files are overwritten.
*
* @param filename Name of the file (could include path).
* @param text Data to save in the file.
* @throws IOException File exception occurred.
*
* @deprecated Use {@link #saveFile(File, String, String)} that has an encoding.
*/
public static void saveFile(final String filename, final String text) throws IOException {
saveFile(new File(filename), text);
}
/**
* Saves a <code>StringBuffer</code> in a file. Existing files are overwritten.
*
* @param filename Name of the file (could include path).
* @param text Data to save in the file.
* @throws IOException File exception occurred.
*
* @deprecated Use {@link #saveFile(File, StringBuffer, String)} that has an encoding.
*/
public static void saveFile(final String filename, final StringBuffer text) throws IOException {
saveFile(new File(filename), text.toString());
}
/**
* Saves a <code>StringBuffer</code> in a file. Existing files are overwritten.
*
* @param file File to save into.
* @param text Data to save in the file.
* @throws IOException File exception occurred.
*
* @deprecated Use {@link #saveFile(File, StringBuffer, String)} that has an encoding
* parameter.
*/
public static void saveFile(final File file, final StringBuffer text) throws IOException {
saveFile(file, text.toString());
}
/**
* Saves a <code>String</code> in a file. Uses default encoding. Existing files are
* overwritten.
*
* @param file File to save the data in.
* @param text Data to save in the file.
* @throws IOException File exception occurred.
*
* @deprecated Use {@link #saveFile(File, String, String)} that has an encoding parameter.
*/
public static void saveFile(final File file, final String text) throws IOException {
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(file));
out.write(text);
} finally {
close(out);
}
}
/**
* Saves a <code>String</code> in a file. Existing files are overwritten.
*
* @param file File to save the data in.
* @param text Data to save in the file.
* @param encoding Use this encoding.
* @throws IOException File exception occurred.
*/
public static void saveFile(final File file, final StringBuffer text, final String encoding)
throws IOException {
saveFile(file, text.toString(), encoding);
}
/**
* Saves a <code>String</code> in a file.
*
* @param file File to save the data in.
* @param text Data to save in the file.
* @param encoding Use this encoding.
* @throws IOException File exception occurred.
*/
public static void saveFile(final File file, final String text, final String encoding) throws IOException {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding));
try {
out.write(text);
} finally {
out.close();
}
}
/**
* Closes input stream without exception.
*
* @param in Input stream, maybe <code>null</code>.
*/
public static void close(final InputStream in) {
if (in != null) {
try {
in.close();
} catch (Exception e) {
// ignore
}
}
}
/**
* Closes writer without exception.
*
* @param writer Writer, maybe <code>null</code>.
*/
public static void close(final Writer writer) {
if (writer != null) {
try {
writer.close();
} catch (Exception e) {
// ignore
}
}
}
/**
* Closes out stream without exception.
*
* @param out Output stream, maybe <code>null</code>.
*/
public static void close(final OutputStream out) {
if (out != null) {
try {
out.close();
} catch (Exception e) {
// ignore
}
}
}
/**
* Closes input reader without exception.
*
* @param reader Reader, maybe <code>null</code>.
*/
public static void close(final Reader reader) {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
// ignore
}
}
}
}
Related
- readUrlText(String urlString)
- readURLText(URL url, StringBuffer errorText)
- readURLThrowException(final String url)
- readURLToByteArray(URL url)
- readURLToString(URL u, String encoding)
- saveFileFromURL(URL url, File destinationFile)
- saveImage(String imageUrl, String destinationFile)
- saveImageAsFile(String imageUrl, String destinationFile, boolean overwrite)
- saveUrl(String filename, String urlString)