ChenJiaHe
2021-05-06 8d87cf8d33815bdd6e7d0c968d105a09c5f5eac5
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileNotFoundException;
8 import java.io.FileOutputStream;
9 import java.io.IOException;
10 import java.io.InputStream;
11 import java.io.UnsupportedEncodingException;
12
13 public class StreamUtils {
14
15     final static int BUFFER_SIZE = 4096;
16     /**
17      * ��InputStreamת����String
18      * 
19      * @param in
20      *            InputStream
21      * @return String
22      * @throws Exception
23      * 
24      */
25     public static String InputStreamTOString(InputStream in) {
26
27         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
28         byte[] data = new byte[BUFFER_SIZE];
29         String string = null;
30         int count = 0;
31         try {
32             while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
33                 outStream.write(data, 0, count);
34         } catch (IOException e) {
35             e.printStackTrace();
36         }
37
38         data = null;
39         try {
40             string = new String(outStream.toByteArray(), "UTF-8");
41         } catch (UnsupportedEncodingException e) {
42             e.printStackTrace();
43         }
44         return string;
45     }
46
47     /**
48      * ��InputStreamת����ij���ַ�����String
49      * 
50      * @param in
51      * @param encoding
52      * @return
53      * @throws Exception
54      */
55     public static String InputStreamTOString(InputStream in, String encoding) {
56         String string = null;
57         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
58         byte[] data = new byte[BUFFER_SIZE];
59         int count = -1;
60         try {
61             while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
62                 outStream.write(data, 0, count);
63         } catch (IOException e) {
64             e.printStackTrace();
65         }
66
67         data = null;
68         try {
69             string = new String(outStream.toByteArray(), encoding);
70         } catch (UnsupportedEncodingException e) {
71             e.printStackTrace();
72         }
73         return string;
74     }
75
76     /**
77      * ��Stringת����InputStream
78      * 
79      * @param in
80      * @return
81      * @throws Exception
82      */
83     public static InputStream StringTOInputStream(String in) throws Exception {
84
85         ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("UTF-8"));
86         return is;
87     }
88
89     /**
90      * ��Stringת����InputStream
91      * 
92      * @param in
93      * @return
94      * @throws Exception
95      */
96     public static byte[] StringTObyte(String in) {
97         byte[] bytes = null;
98         try {
99             bytes = InputStreamTOByte(StringTOInputStream(in));
100         } catch (IOException e) {
101         } catch (Exception e) {
102             e.printStackTrace();
103         }
104         return bytes;
105     }
106
107     /**
108      * ��InputStreamת����byte����
109      * 
110      * @param in
111      *            InputStream
112      * @return byte[]
113      * @throws IOException
114      */
115     public static byte[] InputStreamTOByte(InputStream in) throws IOException {
116
117         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
118         byte[] data = new byte[BUFFER_SIZE];
119         int count = -1;
120         while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)
121             outStream.write(data, 0, count);
122
123         data = null;
124         return outStream.toByteArray();
125     }
126
127
128     /**
129      * ��byte����ת����InputStream
130      * 
131      * @param in
132      * @return
133      * @throws Exception
134      */
135     public static InputStream byteTOInputStream(byte[] in) throws Exception {
136
137         ByteArrayInputStream is = new ByteArrayInputStream(in);
138         return is;
139     }
140
141     /**
142      * ��byte����ת����String
143      * 
144      * @param in
145      * @return
146      * @throws Exception
147      */
148     public static String byteTOString(byte[] in) {
149
150         InputStream is = null;
151         try {
152             is = byteTOInputStream(in);
153         } catch (Exception e) {
154             e.printStackTrace();
155         }
156         return InputStreamTOString(is, "UTF-8");
157     }
158     /**
159      * ��byte����ת����String
160      * 
161      * @param in
162      * @return
163      * @throws Exception
164      */
165     public static String getString(String in) {
166
167         String is = null;
168         try {
169             is = byteTOString(StringTObyte(in));
170         } catch (Exception e) {
171             e.printStackTrace();
172         }
173         return is;
174     }
175
176     // InputStream ת����byte[]
177     public byte[] getBytes(InputStream is) throws IOException {
178
179         ByteArrayOutputStream baos = new ByteArrayOutputStream();
180         byte[] b = new byte[BUFFER_SIZE];
181         int len = 0;
182
183         while ((len = is.read(b, 0, BUFFER_SIZE)) != -1) {
184             baos.write(b, 0, len);
185         }
186
187         baos.flush();
188
189         byte[] bytes = baos.toByteArray();
190
191         System.out.println(new String(bytes));
192
193         return bytes;
194     }
195     /**
196      * ����ļ�·�������ļ�����������
197      * ���ֽ�Ϊ��λ���� unicode ��
198      * @return
199      */
200     public static FileInputStream getFileInputStream(String filepath) {
201         FileInputStream fileInputStream = null;
202         try {
203             fileInputStream = new FileInputStream(filepath);
204         } catch (FileNotFoundException e) {
205             System.out.print("������Ϣ:�ļ�������");
206             e.printStackTrace();
207         }
208         return fileInputStream;
209     }
210     /**
211      * ����ļ����󴴽��ļ�����������
212      * ���ֽ�Ϊ��λ���� unicode ��
213      * @return
214      */
215     public static FileInputStream getFileInputStream(File file) {
216         FileInputStream fileInputStream = null;
217         try {
218             fileInputStream = new FileInputStream(file);
219         } catch (FileNotFoundException e) {
220             System.out.print("������Ϣ:�ļ�������");
221             e.printStackTrace();
222         }
223         return fileInputStream;
224     }
225     /**
226       * ����ļ����󴴽��ļ����������
227      * ���ֽ�Ϊ��λ���� unicode ��
228      * @param file
229      * @param append true:�ļ���׷�ӷ�ʽ��,false:�򸲸�ԭ�ļ�������
230      * @return
231      */
232     public static FileOutputStream getFileOutputStream(File file,boolean append) {
233         FileOutputStream fileOutputStream = null;
234         try {
235             fileOutputStream = new FileOutputStream(file,append);
236         } catch (FileNotFoundException e) {
237             System.out.print("������Ϣ:�ļ�������");
238             e.printStackTrace();
239         }
240         return fileOutputStream;
241     }
242     /**
243      * ����ļ�·�������ļ����������
244      * ���ֽ�Ϊ��λ���� unicode ��
245      * @param append true:�ļ���׷�ӷ�ʽ��,false:�򸲸�ԭ�ļ�������
246      * @return
247      */
248     public static FileOutputStream getFileOutputStream(String filepath,boolean append) {
249         FileOutputStream fileOutputStream = null;
250         try {
251             fileOutputStream = new FileOutputStream(filepath,append);
252         } catch (FileNotFoundException e) {
253             System.out.print("������Ϣ:�ļ�������");
254             e.printStackTrace();
255         }
256         return fileOutputStream;
257     }
258
259     public static File getFile(String filepath) {
260         return new File(filepath);
261     }
262     public static ByteArrayOutputStream getByteArrayOutputStream() {
263         return new ByteArrayOutputStream();
264     }
265
266 }