Mercurial > hg > monetdb-java
comparison src/main/java/org/monetdb/util/Extract.java @ 391:f523727db392
Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.*
This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author | Martin van Dinther <martin.van.dinther@monetdbsolutions.com> |
---|---|
date | Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12) |
parents | src/main/java/nl/cwi/monetdb/util/Extract.java@54137aeb1f92 |
children | bf9f6b6ecf40 |
comparison
equal
deleted
inserted
replaced
390:6199e0be3c6e | 391:f523727db392 |
---|---|
1 /* | |
2 * This Source Code Form is subject to the terms of the Mozilla Public | |
3 * License, v. 2.0. If a copy of the MPL was not distributed with this | |
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. | |
5 * | |
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V. | |
7 */ | |
8 | |
9 package org.monetdb.util; | |
10 | |
11 import java.io.BufferedReader; | |
12 import java.io.FileNotFoundException; | |
13 import java.io.FileWriter; | |
14 import java.io.IOException; | |
15 | |
16 /** | |
17 * This file contains a function to extract files from its including Jar | |
18 * package. | |
19 * | |
20 * @author Ying Zhang "Y.Zhang@cwi.nl" | |
21 * @version 0.1 | |
22 */ | |
23 public class Extract { | |
24 private static final int DEFAULT_BUFSIZE = 16386; | |
25 | |
26 public Extract() {} | |
27 | |
28 /** | |
29 * Extracts a file from the Jar package which includes this class to | |
30 * the given destination | |
31 * @param fromFile The file to extract, including it absolute path | |
32 * inside its including Jar package. | |
33 * @param toFile Destination for the extracted file | |
34 * @throws FileNotFoundException If the file to extract can not be | |
35 * found in its including Jar package. | |
36 * @throws IOException If any error happens during | |
37 * creating/reading/writing files. | |
38 */ | |
39 public static void extractFile(final String fromFile, final String toFile) | |
40 throws FileNotFoundException, IOException | |
41 { | |
42 final java.io.InputStream is = new Extract().getClass().getResourceAsStream(fromFile); | |
43 if (is == null) { | |
44 throw new FileNotFoundException("File " + fromFile + | |
45 " does not exist in the JAR package."); | |
46 } | |
47 | |
48 final BufferedReader reader = new BufferedReader(new java.io.InputStreamReader(is)); | |
49 final FileWriter writer = new FileWriter(toFile, false); | |
50 final char[] cbuf = new char[DEFAULT_BUFSIZE]; | |
51 int ret = reader.read(cbuf, 0, DEFAULT_BUFSIZE); | |
52 while (ret > 0) { | |
53 writer.write(cbuf, 0, ret); | |
54 ret = reader.read(cbuf, 0, DEFAULT_BUFSIZE); | |
55 } | |
56 reader.close(); | |
57 writer.close(); | |
58 } | |
59 } |