2018년 4월 11일 수요일

[Java] File, ByteArray, String, Stream Conversion Examples


1. ByteArray To File

1) JDK ByteArray To Path
1
2
3
static void byteArrayToFileName(byte[] data, String file) throws IOException {
        java.nio.file.Files.write(Paths.get(file), data);
    }

2) Commons(FileUtils) ByteArray To File
1
2
3
static void byteArrayToFile(byte[] data, File file ) throws IOException {
        org.apache.commons.io.FileUtils.writeByteArrayToFile(file, data);
    }

3) Guava ByteArray To File
1
2
3
static void guavaByteArrayToFile(byte[] data, File file ) throws IOException {
        com.google.common.io.Files.write(data, file);
    }


2. File To ByteArray

1) JDK Path to ByteArray
1
2
3
static byte[] fileNameToByteArray(String file ) throws IOException {
        return java.nio.file.Files.readAllBytes(Paths.get(file));
    }

2) Commons(FileUtils) File To ByteArray
1
2
3
static byte[] fileToByteArray( File file ) throws IOException {
        return org.apache.commons.io.FileUtils.readFileToByteArray(file);
    }



3. InputStream to ByteArray

1) Commons(IOUtils) InputStream To ByteArray
1
2
3
static byte[] inputStreamToByteArray( InputStream is ) throws IOException {
        return org.apache.commons.io.IOUtils.toByteArray(is);
    }

2) Guava InputStream To ByteArray
1
2
3
static byte[] guavaInputStreamToByteArray( InputStream is ) throws IOException {
        return ByteStreams.toByteArray(is);
    }

3) JDK InputStream To ByteArray using ByteArrayOutputStream
1
 2
 3
 4
 5
 6
 7
 8
 9
10
static byte[] inputStreamToByteStream( InputStream is ) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        byte[] data = new byte[16384];
        int len;
        while ((len = is.read(data)) >= 0 ) {
            buffer.write(data, 0, len);
        }
        buffer.flush();
        return buffer.toByteArray();        
    }



4. ByteArray to InputStream

1) JDK ByteArray To InputStream
1
2
3
static InputStream byteArrayToInputStream(byte[] data) {
         return new ByteArrayInputStream(data);
    }



5. File To LinesArray

1) File To LinesArray using Scanner
1
2
3
4
5
6
7
8
static String[] readFileLines(File file) throws FileNotFoundException {
        List<String> lines = new ArrayList<>();
        final Scanner s = new Scanner(file);
        while(s.hasNextLine()) {
            lines.add(s.nextLine());
        }      
        return lines.toArray(new String[lines.size()]);
    }

2) File To LinesArray using BufferedReader
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
static String[] fileToLinesArray(File file) throws FileNotFoundException, IOException {
        List<String> lines = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(file));
        while(true) {
            String line = br.readLine();
            if( line == null ) {
                break;
            }
            lines.add(line);
        }      
        br.close();
        return lines.toArray(new String[lines.size()]);
    }


6. List To Array


1
2
3
4
5
6
7
static void listToArray() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        Integer[] array = list.toArray(new Integer[list.size()]);
    }


7. Array To List


1
2
3
4
5
6
7
8
9
static void arrayToList() {
        Integer[] values = { 1, 2, 3 };
        List<Integer> list = new ArrayList<>(Arrays.asList(values));
        
        int[] values2 = new int[] { 1, 2, 3 };
        list = Arrays.stream(values2).boxed().collect(Collectors.toList());
        list = Arrays.stream(values2).boxed().collect(Collectors.toCollection(ArrayList::new));
        list = Arrays.stream(values2).boxed().collect(Collectors.toCollection(LinkedList::new));      
    }








댓글 없음:

댓글 쓰기