您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Java如何實現(xiàn)字符串和輸入流的相互轉(zhuǎn)換”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
在讀取網(wǎng)絡(luò)資源時經(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)化會字符串,代碼如下:
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)建一個新 FileReader。
FileReader(String fileName);
在給定從中讀取數(shù)據(jù)的文件名的情況下創(chuàng)建一個新 FileReader。
常用讀取方法:
方法名 | 說明 |
---|---|
int read() | 讀入一個字符,都到結(jié)尾則返回-1 |
int read(char[] cbuf) | 將讀取的cbuf.length個字符讀取到char數(shù)組中 |
int read(char[] cbuf, int off, int len) | 從此字符輸入流中偏移量off到len個字符讀取到char數(shù)組中 |
void reset() | 重置該流 |
boolean ready() | 判斷是否準(zhǔn)備讀取此流 |
void close() | 關(guān)閉字符輸入流,并釋放所有系統(tǒng)資源 |
long skip(long n) | 跳過讀取n個字符,并返回跳過字符的數(shù)量 |
void mark(int readLimit) | 將此輸入流標(biāo)記,當(dāng)使用reset方法時就返回到該位置,從此位置開始讀入字符 |
1.單個讀取,如果文件太大不建議使用。
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對象 FileReader fr=null; try { fr=new FileReader(file); int c; while((c=fr.read())!=-1) { System.out.print((char)c); //強制轉(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.讀取多個字符輸出。
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抽象類是所有字符輸出流的父類,用于對文件寫入數(shù)據(jù)。
字符輸出流結(jié)構(gòu):
為了寫入Java提供了一種字符寫入的便捷類。
FileWriter類
構(gòu)造方法:
FileWriter(File file)
與 FileWriter(String fileName);
使用給定的file對象或者給定的文件路徑名構(gòu)造一個FileWriter對象。
FileWriter(File file, boolean append)
與 FileWriter(String fileName, boolean append);
通過給定的file對象或者文件路徑名構(gòu)造FileWriter對象,以及是否追加還是覆蓋。
常用讀取方法
方法名 | 說明 |
---|---|
void write(char[] cbuf) | 將cbuf指定的所有字符數(shù)組寫入到字符輸出流中 |
void write(int c) | 向字符輸出流中寫入一個字符 |
void write(char[] cbuf,int off,int len) | 將cbuf數(shù)組中的字符從偏移量off到長度為len個字符寫入到此輸出流中。 |
void write(String str ) | 向字符輸流中寫入一個字符串 |
void write(String str , int off ,int len) | 將str字符串從偏移量off,長度為len個字符串寫入到此輸出流中。 |
Abstract void flush() | 刷新當(dāng)前輸出流,并強制寫入所有字符數(shù)據(jù) |
abstract void close() | 關(guān)閉此輸出流 |
1.writer(int c);寫入一個字符
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對象 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); 寫入一個字符串
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對象 FileWriter fw=null; try { fw=new FileWriter(file); String str="你好,java"; fw.write(str); //寫入一個字符串,等價于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(); } } } } }
“Java如何實現(xiàn)字符串和輸入流的相互轉(zhuǎn)換”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。