Java實現(xiàn)字符串和輸入流的相互轉換
字符串和輸入流的相互轉換
在讀取網(wǎng)絡資源時經(jīng)常要用到字符串和輸入流之間的相互轉化,找到了些方法,記錄一下。
將字符串轉化為輸入流,代碼如下:
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;?
}將輸入流轉化會字符串,代碼如下:
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 {
?? ?/**
?? ? * 把輸入流的內容轉化成字符串
?? ? * @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抽象是所有字符輸入流的父類,用于讀取文件內容
字符輸入流結構:

為了讀取方便,Java提供了一種讀取字符文件的便捷類。
FileReader類
構造方法:
FileReader(File file);在給定從中讀取數(shù)據(jù)的 File 的情況下創(chuàng)建一個新 FileReader。FileReader(String fileName);在給定從中讀取數(shù)據(jù)的文件名的情況下創(chuàng)建一個新 FileReader。
常用讀取方法:
| 方法名 | 說明 |
|---|---|
| int read() | 讀入一個字符,都到結尾則返回-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() | 判斷是否準備讀取此流 |
| void close() | 關閉字符輸入流,并釋放所有系統(tǒng)資源 |
| long skip(long n) | 跳過讀取n個字符,并返回跳過字符的數(shù)量 |
| void mark(int readLimit) | 將此輸入流標記,當使用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); //強制轉換成字符
}
} 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ù)。
字符輸出流結構:

為了寫入Java提供了一種字符寫入的便捷類。
FileWriter類
構造方法:
FileWriter(File file)與FileWriter(String fileName);使用給定的file對象或者給定的文件路徑名構造一個FileWriter對象。FileWriter(File file, boolean append)與FileWriter(String fileName, boolean append);通過給定的file對象或者文件路徑名構造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() | 刷新當前輸出流,并強制寫入所有字符數(shù)據(jù) |
| abstract void close() | 關閉此輸出流 |
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();
}
}
}
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
將字符串數(shù)字格式化為樣式1,000,000,000的方法
這篇文章主要介紹了將字符串數(shù)字格式化為樣式1,000,000,000的方法,有需要的朋友可以參考一下2014-01-01
如何利用Map與函數(shù)式接口來實現(xiàn)去除if else
這篇文章主要介紹了如何利用Map與函數(shù)式接口來實現(xiàn)去除if else問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring Boot前后端分離開發(fā)模式中的跨域問題及解決方法
本文介紹了解決Spring Boot前端Vue跨域問題的實戰(zhàn)經(jīng)驗,并提供了后端和前端的配置示例,通過配置后端和前端,我們可以輕松解決跨域問題,實現(xiàn)正常的前后端交互,需要的朋友可以參考下2023-09-09
eclipse創(chuàng)建一個基于maven的web項目詳細步驟
開始學習maven,并用maven創(chuàng)建了第一個屬于自己的web項目,下面這篇文章主要給大家介紹了關于eclipse創(chuàng)建一個基于maven的web項目的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-12-12

