欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)字符串和輸入流的相互轉(zhuǎn)換

 更新時(shí)間:2022年08月19日 09:50:05   作者:dangnianmingyue_gg  
這篇文章主要介紹了Java實(shí)現(xiàn)字符串和輸入流的相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

字符串和輸入流的相互轉(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)證的示例

    這篇文章主要介紹了SpringBoot基于SpringSecurity表單登錄和權(quán)限驗(yàn)證的示例。文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • javaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳

    javaWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳

    這篇文章主要為大家詳細(xì)介紹了JAVAWeb實(shí)現(xiàn)簡(jiǎn)單文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • java利用時(shí)間格式生成唯一文件名的方法

    java利用時(shí)間格式生成唯一文件名的方法

    這篇文章主要介紹了java利用時(shí)間格式生成唯一文件名的方法,需要的朋友可以參考下
    2017-01-01
  • Netty網(wǎng)絡(luò)編程實(shí)戰(zhàn)之搭建Netty服務(wù)器

    Netty網(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-10
  • idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析

    idea不能自動(dòng)補(bǔ)全yml配置文件的原因分析

    這篇文章主要介紹了idea不能自動(dòng)補(bǔ)全yml配置文件的原因,通過(guò)添加yml文件為配置文件能夠很快的解決,具體解決步驟跟隨小編一起通過(guò)本文學(xué)習(xí)下吧
    2021-06-06
  • Eclipse 2020-06 漢化包安裝步驟詳解(附漢化包+安裝教程)

    Eclipse 2020-06 漢化包安裝步驟詳解(附漢化包+安裝教程)

    這篇文章主要介紹了Eclipse 2020-06 漢化包安裝步驟(附漢化包+安裝教程),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • 詳解Java中native關(guān)鍵字

    詳解Java中native關(guān)鍵字

    這篇文章主要為大家詳細(xì)介紹了Java中native關(guān)鍵字,什么是Native Method
    2016-02-02
  • java實(shí)現(xiàn)撲克牌牌面小程序

    java實(shí)現(xiàn)撲克牌牌面小程序

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)撲克牌牌面小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • java快速生成接口文檔的三種解決方案

    java快速生成接口文檔的三種解決方案

    我們知道在項(xiàng)目開發(fā)階段,接口文檔基本上是必備產(chǎn)物了,一般由后端開發(fā)人員提供,作為和前端人員進(jìn)行前后端接口聯(lián)調(diào)的橋梁,或者與別的項(xiàng)目模塊進(jìn)行交互提供指導(dǎo)等等,這篇文章主要給大家介紹了關(guān)于java快速生成接口文檔的三種解決方案,需要的朋友可以參考下
    2021-07-07
  • 詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié)

    詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié)

    本篇文章主要介紹了詳解Spring Cloud Eureka多網(wǎng)卡配置總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04

最新評(píng)論