byte <--> String
package com.example;
//import java.nio.ByteBuffer; //導入依賴的package包/類
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.io.RandomAccessFile;
/**
* Hello world!
*
*/
public class byteRead
{
public static void main( String[] args ) throws IOException
{
System.out.println( "Hello World!" );
String fileName="C:\\Work\\Java\\data\\19027835361399637";
RandomAccessFile f = new RandomAccessFile(fileName, "r");
byte[] b = new byte[(int)f.length()];
f.readFully(b);
System.out.println("b string--> " + new String(b));
System.out.println("b byte--> " + Arrays.toString(b));
String string = "hello 世界小姐";
byte[] bytes = string.getBytes();//獲得byte陣列
System.out.println("bytes-->" + Arrays.toString(bytes));//列印byte陣列
System.out.println("string-->" + new String(bytes));//獲得byte陣列轉換來的String資料,並列印
f.close();
System.out.println( "Hello World!" );
}
}
留言
張貼留言