Java實(shí)現(xiàn)字符串和輸入流的相互轉(zhuǎn)換
字符串和輸入流的相互轉(zhuǎn)換
在讀取網(wǎng)絡(luò)資源時(shí)經(jīng)常要用到字符串和輸入流之間的相互轉(zhuǎn)化,找到了些方法,記錄一下。
將字符串轉(zhuǎn)化為輸入流,代碼如下:
public static InputStream getStringStream(String sInputString){? if (sInputString != null && !sInputString.trim().equals("")){? ? ? try{? ? ? ? ? ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());? ? ? ? ? return tInputStringStream;? ? ? }catch (Exception ex){? ? ? ? ? ex.printStackTrace();? ? ? }? }? return null;? }
將輸入流轉(zhuǎn)化會(huì)字符串,代碼如下:
public static String getStreamString(InputStream tInputStream){? if (tInputStream != null){? ? ? ?try{? ? ? ? ? ? BufferedReader tBufferedReader = new BufferedReader(new InputStreamReader(tInputStream));? ? ? ? ? ? StringBuffer tStringBuffer = new StringBuffer();? ? ? ? ? ? String sTempOneLine = new String("");? ? ? ? ? ? while ((sTempOneLine = tBufferedReader.readLine()) != null){? ? ? ? ? ? ? ? tStringBuffer.append(sTempOneLine);? ? ? ? ? ? }? ? ? ? ? ?return tStringBuffer.toString();? ? ? }catch (Exception ex){? ? ? ? ? ?ex.printStackTrace();? ? ? }? }? return null;? }
或者是以下的方法,代碼如下:
public class StreamTool { ?? ?/** ?? ? * 把輸入流的內(nèi)容轉(zhuǎn)化成字符串 ?? ? * @param is ?? ? * @return ?? ? */ ?? ?public static String readInputStream(InputStream is){ ?? ??? ?try { ?? ??? ??? ?ByteArrayOutputStream baos=new ByteArrayOutputStream(); ?? ??? ??? ?int length=0; ?? ??? ??? ?byte[] buffer=new byte[1024]; ?? ??? ??? ?while((length=is.read(buffer))!=-1){ ?? ??? ??? ??? ?baos.write(buffer, 0, length); ?? ??? ??? ?} ?? ??? ??? ?is.close(); ?? ??? ??? ?baos.close(); ?? ??? ??? ?//或者用這種方法 ?? ??? ??? ?//byte[] result=baos.toByteArray(); ?? ??? ??? ?//return new String(result); ?? ??? ??? ?return baos.toString(); ?? ??? ?} catch (Exception e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ??? ?return "獲取失敗"; ?? ??? ?} ?? ?} }
字符輸入與輸出流
字符輸入流
java.io.Reader抽象是所有字符輸入流的父類,用于讀取文件內(nèi)容
字符輸入流結(jié)構(gòu):
為了讀取方便,Java提供了一種讀取字符文件的便捷類。
FileReader類
構(gòu)造方法:
FileReader(File file);
在給定從中讀取數(shù)據(jù)的 File 的情況下創(chuàng)建一個(gè)新 FileReader。FileReader(String fileName);
在給定從中讀取數(shù)據(jù)的文件名的情況下創(chuàng)建一個(gè)新 FileReader。
常用讀取方法:
方法名 | 說(shuō)明 |
---|---|
int read() | 讀入一個(gè)字符,都到結(jié)尾則返回-1 |
int read(char[] cbuf) | 將讀取的cbuf.length個(gè)字符讀取到char數(shù)組中 |
int read(char[] cbuf, int off, int len) | 從此字符輸入流中偏移量off到len個(gè)字符讀取到char數(shù)組中 |
void reset() | 重置該流 |
boolean ready() | 判斷是否準(zhǔn)備讀取此流 |
void close() | 關(guān)閉字符輸入流,并釋放所有系統(tǒng)資源 |
long skip(long n) | 跳過(guò)讀取n個(gè)字符,并返回跳過(guò)字符的數(shù)量 |
void mark(int readLimit) | 將此輸入流標(biāo)記,當(dāng)使用reset方法時(shí)就返回到該位置,從此位置開始讀入字符 |
1.單個(gè)讀取,如果文件太大不建議使用。
import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class dome2{ public static void main(String[] args){ File file=new File("D:/../...txt"); //創(chuàng)建file對(duì)象 FileReader fr=null; try { fr=new FileReader(file); int c; while((c=fr.read())!=-1) { System.out.print((char)c); //強(qiáng)制轉(zhuǎn)換成字符 } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fr!=null) { try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
2.讀取多個(gè)字符輸出。
import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class dome2{ public static void main(String[] args){ File file=new File("D:/../...txt"); FileReader fr=null; try { fr=new FileReader(file); char[] c=new char[100]; int length; while((length=fr.read(c))!=-1) { System.out.println(new String(c,0,length)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fr!=null) { try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
字符輸出流
java.io.Writer抽象類是所有字符輸出流的父類,用于對(duì)文件寫入數(shù)據(jù)。
字符輸出流結(jié)構(gòu):
為了寫入Java提供了一種字符寫入的便捷類。
FileWriter類
構(gòu)造方法:
FileWriter(File file)
與FileWriter(String fileName);
使用給定的file對(duì)象或者給定的文件路徑名構(gòu)造一個(gè)FileWriter對(duì)象。FileWriter(File file, boolean append)
與FileWriter(String fileName, boolean append);
通過(guò)給定的file對(duì)象或者文件路徑名構(gòu)造FileWriter對(duì)象,以及是否追加還是覆蓋。
常用讀取方法
方法名 | 說(shuō)明 |
---|---|
void write(char[] cbuf) | 將cbuf指定的所有字符數(shù)組寫入到字符輸出流中 |
void write(int c) | 向字符輸出流中寫入一個(gè)字符 |
void write(char[] cbuf,int off,int len) | 將cbuf數(shù)組中的字符從偏移量off到長(zhǎng)度為len個(gè)字符寫入到此輸出流中。 |
void write(String str ) | 向字符輸流中寫入一個(gè)字符串 |
void write(String str , int off ,int len) | 將str字符串從偏移量off,長(zhǎng)度為len個(gè)字符串寫入到此輸出流中。 |
Abstract void flush() | 刷新當(dāng)前輸出流,并強(qiáng)制寫入所有字符數(shù)據(jù) |
abstract void close() | 關(guān)閉此輸出流 |
1.writer(int c);寫入一個(gè)字符
import java.io.File; import java.io.FileWriter; import java.io.IOException; public class dome2{ public static void main(String[] args){ File file=new File("D:/../...txt"); //創(chuàng)建file對(duì)象 FileWriter fw=null; try { fw=new FileWriter(file); char c='你'; fw.write((int)c); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fw!=null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
2.writer(String str); 寫入一個(gè)字符串
import java.io.File; import java.io.FileWriter; import java.io.IOException; public class dome2{ public static void main(String[] args){ File file=new File("D:/../...txt"); //創(chuàng)建file對(duì)象 FileWriter fw=null; try { fw=new FileWriter(file); String str="你好,java"; fw.write(str); //寫入一個(gè)字符串,等價(jià)于write(str,0,str.length); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(fw!=null) { try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot基于SpringSecurity表單登錄和權(quán)限驗(yàn)證的示例
這篇文章主要介紹了SpringBoot基于SpringSecurity表單登錄和權(quán)限驗(yàn)證的示例。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09javaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳
這篇文章主要為大家詳細(xì)介紹了JAVAWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Netty網(wǎng)絡(luò)編程實(shí)戰(zhàn)之搭建Netty服務(wù)器
Netty是JBOSS開源的一款NIO網(wǎng)絡(luò)編程框架,可用于快速開發(fā)網(wǎng)絡(luò)的應(yīng)用。Netty是一個(gè)異步的、基于事件驅(qū)動(dòng)的網(wǎng)絡(luò)應(yīng)用框架,用于快速開發(fā)高性能的服務(wù)端和客戶端。本文將詳細(xì)說(shuō)說(shuō)如何搭建Netty服務(wù)器,需要的可以參考一下2022-10-10idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析
這篇文章主要介紹了idea不能自動(dòng)補(bǔ)全yml配置文件的原因,通過(guò)添加yml文件為配置文件能夠很快的解決,具體解決步驟跟隨小編一起通過(guò)本文學(xué)習(xí)下吧2021-06-06Eclipse 2020-06 漢化包安裝步驟詳解(附漢化包+安裝教程)
這篇文章主要介紹了Eclipse 2020-06 漢化包安裝步驟(附漢化包+安裝教程),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié)
本篇文章主要介紹了詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04