文件写入

/**
	 * 保存文件
	 * @param filePath
	 * @param reader
	 * @throws DefaultException
	 */
	public static void saveFile(String filePath,InputStream reader) throws DefaultException{
		OutputStream writer = null;
		File file = new File(filePath);
		File parent = file.getParentFile();
		if (!parent.exists())
			parent.mkdirs();
		try {
			writer = new FileOutputStream(file);
			byte[] b = new byte[1024];
			int bytesRead = 0;
			while((bytesRead=reader.read(b, 0, 1024)) != -1){
				writer.write(b,0,bytesRead);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new DefaultException(e);
		} catch (IOException ioe) {
			ioe.printStackTrace();
			throw new DefaultException(ioe);
		} finally {
			try{
				reader.close();
				writer.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}

你可能感兴趣的:(文件写入)