String 与inputString 互转方法

1、将String字符串转换成字符流
String str = "String与InputStream相互转换";
InputStream in_nocode = new ByteArrayInputStream(str.getBytes());
InputStream in_withcode = new ByteArrayInputStream(str.getBytes("UTF-8"));
2.InputStream to String这里提供几个方法。
方法1:
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
方法2:

public String inputStream2String (InputStream in) throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
方法3:
public static String inputStream2String(InputStream is) throws IOException{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,string与wstring类型换一般有以下几种方法方法一:使用标准库std::wstring_convert和std::codecvt_utf8_utf16(或std::codecvt_utf8<wchar_t>)进行换。这个方法在C++11和C++14标准下是可用的,但在C++17标准下已经被弃用。示例代码如下: ```cpp #include <string> #include <locale> #include <codecvt> // convert string to wstring std::wstring to_wide_string(const std::string& input) { std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; return converter.from_bytes(input); } // convert wstring to string std::string to_byte_string(const std::wstring& input) { std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; return converter.to_bytes(input); } ``` 这种方法适用于C++11和C++14标准,但不适用于C++17标准。 方法二:使用内联函数和std::wstring_convert进行换。这个方法也使用了std::codecvt_utf8<wchar_t>进行编码换。示例代码如下: ```cpp #include <string> #include <locale> #include <codecvt> // convert string to wstring inline std::wstring to_wide_string(const std::string& input) { std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; return converter.from_bytes(input); } // convert wstring to string inline std::string to_byte_string(const std::wstring& input) { std::wstring_convert<std::codecvt_utf8<wchar_t>> converter; return converter.to_bytes(input); } ``` 这种方法方法一类似,只是使用了内联函数来进行换。 方法三:使用Windows系统特有的宽字符串定义L"wstring"来进行换。这个方法适用于Windows系统,在C开发中经常用到。示例代码如下: ```cpp #include <string> #include <windows.h> // convert string to wstring std::wstring to_wide_string(const std::string& input) { int size = MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, nullptr, 0); std::wstring output(size, L'\0'); MultiByteToWideChar(CP_UTF8, 0, input.c_str(), -1, &output<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [C++string与wstring类型换](https://blog.csdn.net/qq_30386941/article/details/126814596)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C++11:string和wstring之间互转换](https://blog.csdn.net/qq_43331089/article/details/123438581)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值