參考: Writer Class
方法:
Writer類用於寫入字符流,可以將字節數組作為參數傳遞。這樣就可以將字節數組轉換為Writer類。要從字符串中獲取字節數組,請使用getBytes()方法。
下麵是上述方法的實現:
程序:
// Java Program Convert
// Byte Array to Writer
import java.io.StringWriter;
import java.io.Writer;
public class GFG {
// Method which convert
// byte array into Writer Class
static String writeByte(byte[] byteString,
byte[] byteInt,
byte[] byteChar,
byte[] byteDouble)
{
// Declare the writer class
Writer writer = new StringWriter();
try {
// Call append() method
// to append byte array into
// writer class as append method
// takes input of only string object
writer
.append(new String(byteString)
+ new String(byteDouble)
+ new String(byteChar)
+ new String(byteInt));
writer.close();
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
// return the string
return writer.toString();
}
// Driver Code
public static void main(String args[])
{
String str = "Speed of light: ";
int num = 8;
char ch = 'e';
double dec = 3.0;
// Insert String value
byte[] byteString = str.getBytes();
// Insert int value
byte[] byteInt = Integer.toString(num).getBytes();
// Insert char value
byte[] byteChar = Character.toString(ch).getBytes();
// Insert double value
byte[] byteDouble = Double.toString(dec).getBytes();
// Call the method
System.out.println(writeByte(byteString, byteInt,
byteChar, byteDouble));
}
}
輸出:
Speed of light: 3.0e8
相關用法
- Java Byte Array轉Image用法及代碼示例
- Java Byte Array轉Object用法及代碼示例
- Java Byte Array轉Long用法及代碼示例
- Java Byte Array轉String用法及代碼示例
- Java Byte Array轉Hex String用法及代碼示例
- Java Byte Array轉JSON用法及代碼示例
- Java Byte compareUnsigned()用法及代碼示例
- Java Byte decode()用法及代碼示例
- Java Byte parseByte()用法及代碼示例
- Java Byte toUnsignedInt()用法及代碼示例
- Java Byte toUnsignedLong()用法及代碼示例
- Java Byte valueOf()用法及代碼示例
- Java Byte byteValue()用法及代碼示例
- Java Byte compare()用法及代碼示例
- Java Byte compareTo()用法及代碼示例
- Java Byte doubleValue()用法及代碼示例
- Java Byte equals()用法及代碼示例
- Java Byte floatValue()用法及代碼示例
- Java Byte hashCode()用法及代碼示例
- Java Byte intValue()用法及代碼示例
- Java Byte longValue()用法及代碼示例
- Java Byte shortValue()用法及代碼示例
- Java Byte toString()用法及代碼示例
- Java ByteArrayOutputStream close()用法及代碼示例
- Java ByteBuffer getDouble()用法及代碼示例
注:本文由純淨天空篩選整理自bilal-hungund大神的英文原創作品 Program to convert Byte Array to Writer in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。