15、關於String,File,InputStream之間的相互轉換
阿新 • • 發佈:2019-01-29
1、介紹了關於String,File,InputStream之間的相互轉換
1.1 String2InputStream
1.2 inputStream2String(InputStream is) ; String inputStream2String(InputStream is, String charset)/** * String2InputStream(String str)的工具方法 * * @param str * 需要轉換的字串str * @return 返回的是字串str轉換為inputstream的結果 */ public static InputStream String2InputStream(String str) { ByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); return stream; }
/** * inputStream2String(InputStream is) * * @param is * 輸入流InputStream is * @return 輸入流InputStream is轉換為的String */ public static String inputStream2String(InputStream is) { StringBuffer buffer = null; BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(is)); buffer = new StringBuffer(); String line = ""; while ((line = in.readLine()) != null) { buffer.append(line); } } catch (Exception e) { throw new RuntimeException( "在呼叫Utils.inputStream2String(InputStream is)發生異常!!!"); } return buffer.toString(); } /** * inputStream2String(InputStream is, String charset) * * @param is * 輸入流InputStream is * @param charset * 字符集String charset * @return 輸入流InputStream is,字符集String charset轉換為的String */ public static String inputStream2String(InputStream is, String charset) { ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); byte data[] = new byte[1024]; int len = -1; while ((len = is.read(data)) != -1) { baos.write(data, 0, len); } String a = baos.toString(charset); return baos.toString(charset); } catch (IOException e) { throw new RuntimeException( "在呼叫Utils.inputStream2String(InputStream is,String charset)發生異常!!!"); } finally { if (null != baos) { try { baos.close(); } catch (IOException e) { throw new RuntimeException( "在呼叫Utils.inputStream2String(InputStream is, String charset)發生異常!!!"); } baos = null; } } }
1.3 file2InputStream
/** * file2InputStream(File file) * * @param file * 檔案 File file * @return 檔案File轉換為的InputStream */ public static InputStream file2InputStream(File file) { InputStream in = null; try { in = new FileInputStream(file); return in; } catch (FileNotFoundException e) { throw new RuntimeException( "在呼叫Utils.file2InputStream(File file)發生異常!!!"); } } /** * file2InputStream(String filenPath) * * @param filenPath * 檔案 File的具體路徑 * @return 檔案File轉換為的InputStream */ public static InputStream file2InputStream(String filenPath) { InputStream in = null; File file = null; try { file = new File(filenPath); in = new FileInputStream(file); return in; } catch (FileNotFoundException e) { throw new RuntimeException( "在呼叫Utils.file2InputStream(String filenPath)發生異常!!!"); } }
1.4 inputstreamtofile
/**
* inputstreamtofile(InputStream ins, File file)
*
* @param ins
* 輸入流InputStream ins
* @param file
* 輸入流InputStream ins轉換的檔案
*/
public static void inputstreamtofile(InputStream ins, File file) {
OutputStream os = null;
try {
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = ins.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
throw new RuntimeException(
"在呼叫Utils.inputstreamtofile(InputStream ins, File file)發生異常!!!");
} finally {
try {
if (os != null)
os.close();
if (ins != null) {
ins.close();
}
} catch (IOException e) {
throw new RuntimeException(
"在呼叫Utils.inputstreamtofile(InputStream ins, File file)發生異常!!!");
}
}
}
public static void inputstreamtofile(InputStream ins, String filePath) {
OutputStream os = null;
File file = null;
try {
file = new File(filePath);
os = new FileOutputStream(file);
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = ins.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (Exception e) {
throw new RuntimeException(
"在呼叫Utils.inputstreamtofile(InputStream ins, File file)發生異常!!!");
} finally {
try {
if (os != null)
os.close();
if (ins != null) {
ins.close();
}
} catch (IOException e) {
throw new RuntimeException(
"在呼叫Utils.inputstreamtofile(InputStream ins, File file)發生異常!!!");
}
}
}