Java File和byte数据之间的转换

时间:2025-02-08 15:36:02
package ;

import ;
import ;
import ;
import ;
import ;
import ;

public class FileTest
{
	
	public static void main(String[] args)
	{
		file2BetyArray();
		fileToBetyArray();
		BetyToFile("D:\\Study\\Java\\");
	}

	public static byte[] file2BetyArray()
	{
		File file = new File("D:\\Study\\Java\\");
		if (!()) {
			return null;
		}
		FileInputStream stream = null;
		ByteArrayOutputStream out = null;
		try {
			stream = new FileInputStream(file);
			out = new ByteArrayOutputStream(1000);
			byte[] b = new byte[1000];
			int n;
			while ((n = (b)) != -1) {
				(b, 0, n);
			}
			return ();// 此方法大文件OutOfMemory
		} catch (Exception e) {
			(());
		} finally {
			try {
				();
				();
			} catch (IOException e) {
				();
			}

		}
		return null;
	}

	public static byte[] fileToBetyArray()
	{
		FileInputStream fileInputStream = null;
		File file = new File("D:\\Study\\Java\\");
		byte[] bFile = null;
		try {
			bFile = new byte[(int) ()];
			fileInputStream = new FileInputStream(file);
			(bFile);
			();
			for (int i = 0; i < ; i++) {
				((char) bFile[i]);
			}
			("Done");
		} catch (Exception e) {
			();
		} finally {
			try {
				();
				();
			} catch (IOException e) {
				();
			}
		}
		return bFile;
	}

	public  static File BetyToFile( String filePath)
	{
		File file = new File(filePath);
		BufferedOutputStream stream = null;
		FileOutputStream fstream = null;
		byte[] data=new byte[(int)()];
		try {
			fstream = new FileOutputStream(file);
			stream = new BufferedOutputStream(fstream);
			(data);
		} catch (Exception e) {
			();
		} finally {
			try {
				if (stream != null) {
					();
				}
				if (null != fstream) {
					();
				}
			} catch (IOException e1) {
				();
			}
		}
		return file;
	}

}