file与 byte[] 互转

byte 转file

String filepath="D:\\"+getName();
          File file=new File(filepath);
          if(file.exists()){
              file.delete();
          }
          FileOutputStream fos = new FileOutputStream(file);   
          fos.write(data,0,data,.length);   
          fos.flush();   
          fos.close();

file转 byte

FileInputStream inputStream = new FileInputStream(file);
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();  
        int ch;  
        while ((ch = inputStream.read()) != -1) {  
         bytestream.write(ch);  
        }  
        byte[] data= bytestream.toByteArray();  
        bytestream.close();  
        inputStream.close();    
        return data;

时间: 2025-02-01 09:07:27

file与 byte[] 互转的相关文章

java File和Byte[]数组 相互转换

public class Test { public static void main(String[] args){ String filePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src\\1.docx"; String outFilePath = "E:\\softoon\\workspace_softoon\\TestMobile\\src"; String outFileName = "

File和byte[]转换

http://blog.csdn.net/commonslok/article/details/9493531 public static byte[] File2byte(String filePath) { byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new Byt

C#: int 与 byte[] 互转

public static int ToInt32(params byte[] v) { var r = 0; var len = v.Length; if (len > 4) { len = 4; } for (var i = 0; i < len; i++) { r |= v[i] << 8 * (len - i - 1); } return r; } public static byte[] ToBytes(int v) { var len = 0; for (var i =

字符串string和内存流MemoryStream及比特数组byte[]互转

定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 2.字符串转流 (1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes("

BigInteger与byte[]互转注意问题

1.byte[]转BigInteger a.高位字节在后 用new BigInteger(byte[])可以实现byte[]转BigInteger,但是要注意字节顺序:高位字节在后 所以在转之前要把byte[]反转一下 b.正整数 需要在byte[]后面加个字节[00] 源代码 public BigInteger toBigInteger(byte[] v) { byte[] r = new byte[v.Length + 1]; Array.Copy(v.Reverse().ToArray()

string和byte[]互转

一.string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); 二.byte[]转成string: string str = System.Text.Encoding.Default.GetString ( byteArray );

Stream 与 byte[] 互转

public byte[] StreamToBytes(Stream stream) { byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); stream.Seek(0, SeekOrigin.Begin);// 设置当前流的位置为流的开始 return bytes; } public Stream BytesToStream(byte[] bytes) { Stream stream = ne

Bitmap byte[] 互转

1.bitmap转byte[] int bytes = bmp.getByteCount(); ByteBuffer buf = ByteBuffer.allocate(bytes); bmp.copyPixelsToBuffer(buf); byte[] byteArray = buf.array(); 2.byte[]转bitmap Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

python str byte 互转

# bytes object b = b"example" # str object s = "example" # str to bytes bytes(s, encoding = "utf8") # bytes to str str(b, encoding = "utf-8") # an alternative method # str to bytes str.encode(s) # bytes to str bytes