Java中文件与字节数组转换

注:来源于JavaEye

文件转化为字节数组:

http://www.javaeye.com/topic/304980

[c-sharp] view plaincopy

  1. /**
  2. * 文件转化为字节数组
  3. *
  4. * @param file
  5. * @return
  6. */
  7. public static byte[] getBytesFromFile(File file) {
  8. byte[] ret = null;
  9. try {
  10. if (file == null) {
  11. // log.error("helper:the file is null!");
  12. return null;
  13. }
  14. FileInputStream in = new FileInputStream(file);
  15. ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
  16. byte[] b = new byte[4096];
  17. int n;
  18. while ((n = in.read(b)) != -1) {
  19. out.write(b, 0, n);
  20. }
  21. in.close();
  22. out.close();
  23. ret = out.toByteArray();
  24. } catch (IOException e) {
  25. // log.error("helper:get bytes from file process error!");
  26. e.printStackTrace();
  27. }
  28. return ret;
  29. }

字节数组转化为文件

http://www.javaeye.com/topic/304982

[java] view plaincopy

  1. /**
  2. * 把字节数组保存为一个文件
  3. *
  4. * @param b
  5. * @param outputFile
  6. * @return
  7. */
  8. public static File getFileFromBytes(byte[] b, String outputFile) {
  9. File ret = null;
  10. BufferedOutputStream stream = null;
  11. try {
  12. ret = new File(outputFile);
  13. FileOutputStream fstream = new FileOutputStream(ret);
  14. stream = new BufferedOutputStream(fstream);
  15. stream.write(b);
  16. } catch (Exception e) {
  17. // log.error("helper:get file from byte process error!");
  18. e.printStackTrace();
  19. } finally {
  20. if (stream != null) {
  21. try {
  22. stream.close();
  23. } catch (IOException e) {
  24. // log.error("helper:get file from byte process error!");
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. return ret;
  30. }
时间: 2024-08-06 03:42:52

Java中文件与字节数组转换的相关文章

java 读取文件的字节数组

/*文件64位编码*/ public static void main(String[] args) {    byte[] fileByte = toByteArray(newFile);   String imgStr = new BASE64Encoder().encode(fileByte);  } /*读取文件的字节数组*/public static byte[] toByteArray(File file) throws IOException { File f = file; if

Java中如何将字符串数组转换成字符串

如果将"字符串数组"转换成"字符串",只能通过循环,没有其他方法: public static String getExecSqlString(String str){ StringBuffer sb = new StringBuffer(); String prefixStr = str.substring(0,str.indexOf("(")); String subStr = str.substring(str.indesOf("

java中如何将字符串数组转换成字符串(转)

如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"}; StringBuffer sb = new StringBuffer(); for(int i = 0; i < str.length; i++){ sb. append(str[i]); } String s = sb.toString(); 如果是 “字符数组” 转 “字符串” 可以通过下边的方

java中文件的I/O操作

java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt");//此时为字节流 byte[] b = new byte[31];//定义字节数组存储从文件夹中读取的数据,大小最多31字节 fStream.read(b);//将test.txt的数据读到b中 String line = new String(b,"UTF-8");//将字节

文件转为字节数组工具类

java将文件转为字节数组 /** * 将文件转为字节 * @param listPd * @return */ public static byte[] t3(String path){ File file = new File("F:/util02/apache-tomcat-8.5.23/driverImgs/"+path); FileInputStream stream =null; ByteArrayOutputStream bos =null; byte[] bs; try

转:Java中String与byte[]的转换

fuzhaoyang 转:Java中String与byte[]的转换 原文地址:http://blog.csdn.net/llwan/article/details/7567906 String s = "fs123fdsa";//String变量 byte b[] = s.getBytes();//String转换为byte[] String t = new String(b);//bytep[]转换为String 做JAVA经常会碰到中文乱码问题,还有各种编码的问题,特别是Stri

Java中数据类型及其之间的转换

Java中数据类型及其之间的转换 基本的数据类型 基本类型有以下四种: 1)int长度数据类型有:byte(8bits).short(16bits).int(32bits).long(64bits). 2)float长度数据类型有:单精度(32bits float).双精度(64bits double),JAVA中将小数默认为double类型,若要定义float需在数据后面用f声明: 3)boolean类型变量的取值有:ture.false 4)char数据类型有:unicode字符,16位 对

java中文件的读与写

最近一直在学习java中如何读取和写出文件,看了java API之后,发现在java.io中有很多关于文件读与写的类,下面就介绍几个经常用到的. 首先是:InputStream和OutputStream,API中说它俩是所有抽象类表示字节输入输出流的超类,所以在它们下面派生了很多子类.例如:FileInputStream和OutputStream等等.这些类都是以单字节的形式读入数据的,所以如果读入的数据中有中文字符,那么就会出现中文乱码现象. 其次是:Reader和Writer,这两个类是用于

java中文件的读写

Java中文件读写操作的作用是什么?回答这个问题时应该先想到的是Java只是一门语言,我们的一种使用工具而已,这样答案就明晰了,就是将外来的各种数据写入到某一个文件中去,用以保存下来:或者从文件中将其数据读取出来,供我们使用.就如下电影过程,从网络资源中下载一部电影保存于你电脑中(写文件),当你想看的时候就用播放器打开(读文件).Java中如何对文件进行读写操作?先理一理,Java中的流分两种,字节流和字符流,其中字节流的两个基类是InputStream和OutputStream;字符流的两个基