|
| 1 | +/* |
| 2 | + * Copyright 2020 Philipp Salvisberg <philipp.salvisberg@trivadis.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.utplsql.sqldev.model; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.nio.charset.Charset; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | + |
| 23 | +import org.utplsql.sqldev.exception.GenericRuntimeException; |
| 24 | + |
| 25 | +public class FileTools { |
| 26 | + // do not instantiate this class |
| 27 | + private FileTools() { |
| 28 | + super(); |
| 29 | + } |
| 30 | + |
| 31 | + public static byte[] readFile(Path path) { |
| 32 | + try { |
| 33 | + return Files.readAllBytes(path); |
| 34 | + } catch (IOException e) { |
| 35 | + final String msg = "Cannot read file " + path.toString() + "."; |
| 36 | + throw new GenericRuntimeException(msg, e); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static void writeFile(Path path, byte[] bytes) { |
| 41 | + try { |
| 42 | + Files.write(path, bytes); |
| 43 | + } catch (IOException e) { |
| 44 | + final String msg = "Cannot write file " + path.toString() + "."; |
| 45 | + throw new GenericRuntimeException(msg, e); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public static void writeFile(Path path, Iterable<? extends CharSequence> lines, Charset cs) { |
| 50 | + try { |
| 51 | + Files.write(path, lines, cs); |
| 52 | + } catch (IOException e) { |
| 53 | + final String msg = "Cannot write file " + path.toString() + "."; |
| 54 | + throw new GenericRuntimeException(msg, e); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments