代码

  1. import lombok.experimental.UtilityClass;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.io.*;
  4. import java.nio.channels.FileChannel;
  5. import java.nio.charset.Charset;
  6. import java.nio.file.Files;
  7. import java.nio.file.Path;
  8. import java.nio.file.Paths;
  9. import java.util.stream.Collectors;
  10. import java.util.zip.GZIPOutputStream;
  11. /**
  12. * IO Kit
  13. */
  14. @Slf4j
  15. @UtilityClass
  16. public class IOKit {
  17. public static void closeQuietly(Closeable closeable) {
  18. try {
  19. if (null == closeable) {
  20. return;
  21. }
  22. closeable.close();
  23. } catch (Exception e) {
  24. log.error("Close closeable error", e);
  25. }
  26. }
  27. public static String readToString(String file) throws IOException {
  28. return readToString(Paths.get(file));
  29. }
  30. public static String readToString(BufferedReader bufferedReader) {
  31. return bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
  32. }
  33. public static String readToString(Path path) throws IOException {
  34. BufferedReader bufferedReader = Files.newBufferedReader(path);
  35. return bufferedReader.lines().collect(Collectors.joining(System.lineSeparator()));
  36. }
  37. public static String readToString(InputStream input) throws IOException {
  38. try (BufferedReader buffer = new BufferedReader(new InputStreamReader(input, "UTF-8"))) {
  39. return buffer.lines().collect(Collectors.joining(System.lineSeparator()));
  40. }
  41. }
  42. public static void copyFile(File source, File dest) throws IOException {
  43. try (FileChannel in = new FileInputStream(source).getChannel(); FileChannel out = new FileOutputStream(dest).getChannel()) {
  44. out.transferFrom(in, 0, in.size());
  45. }
  46. }
  47. public static void compressGZIP(File input, File output) throws IOException {
  48. try (GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(output))) {
  49. try (FileInputStream in = new FileInputStream(input)) {
  50. byte[] buffer = new byte[1024];
  51. int len;
  52. while ((len = in.read(buffer)) != -1) {
  53. out.write(buffer, 0, len);
  54. }
  55. }
  56. }
  57. }
  58. public static byte[] compressGZIPAsString(String content, Charset charset) throws IOException {
  59. if (content == null || content.length() == 0) {
  60. return null;
  61. }
  62. GZIPOutputStream gzip;
  63. ByteArrayOutputStream out = new ByteArrayOutputStream();
  64. gzip = new GZIPOutputStream(out);
  65. gzip.write(content.getBytes(charset));
  66. gzip.close();
  67. return out.toByteArray();
  68. }
  69. }

测试


import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.nio.file.Paths;

public class IOKitTest {

    @Test
    public void testCloseQuietly() throws FileNotFoundException {
        InputStream ins = IOKitTest.class.getResourceAsStream("/application.properties");
        IOKit.closeQuietly(ins);

        IOKit.closeQuietly(null);
    }

    @Test
    public void testReadToString() throws IOException, URISyntaxException {
        String content = IOKit.readToString(IOKitTest.class.getResourceAsStream("/application.properties"));
        Assert.assertEquals(true, StringKit.isNotBlank(content));

        content = IOKit.readToString(Paths.get(IOKitTest.class.getResource("/application.properties").toURI()).toString());
        Assert.assertEquals(true, StringKit.isNotBlank(content));
    }

    @Test
    public void testCopyFile() throws IOException {
        IOKit.copyFile(new File(IOKitTest.class.getResource("/application.properties").getPath()), new File("./tmp.properties"));
        File tmp = new File("./tmp.properties");
        Assert.assertEquals(true, tmp.exists() && tmp.isFile());
        tmp.delete();
    }

}