public File getFileFromBytes(byte[] b, String outputFile){
BufferedOutputStream stream = null;
File file = null;
try{
file = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(file);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e){
e.printStackTrace();
} finally{
if (stream != null){
try{
stream.close();
} catch (IOException e1){
e1.printStackTrace();
}
}
}
return file;
}
- public static ByteArrayOutputStream readFile(String filename) {
-
try {
-
FileInputStream fileInStream = new FileInputStream(filename);
-
ByteArrayOutputStream fileByteStream = new ByteArrayOutputStream();
-
int i = 0;
-
while ((i = fileInStream.read()) != -1) {
- fileByteStream.write(i);
- }
- fileInStream.close();
-
return fileByteStream;
-
} catch (Exception e) {
- e.printStackTrace();
- }
-
return null;
- }
-
-
public static void writeFile(String fileName, byte[] data) {
-
try {
-
FileOutputStream fos = new FileOutputStream(fileName);
- fos.write(data);
- fos.close();
-
} catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
-
public static byte[] getBytes(InputStream is) throws Exception {
-
byte[] data = null;
-
-
Collection chunks = new ArrayList();
-
byte[] buffer = new byte[1024 * 1000];
-
int read = -1;
-
int size = 0;
-
-
while ((read = is.read(buffer)) != -1) {
-
if (read > 0) {
-
byte[] chunk = new byte[read];
-
System.arraycopy(buffer, 0, chunk, 0, read);
- chunks.add(chunk);
- size += chunk.length;
- }
- }
-
-
if (size > 0) {
-
ByteArrayOutputStream bos = null;
-
try {
-
bos = new ByteArrayOutputStream(size);
-
for (Iterator itr = chunks.iterator(); itr.hasNext();) {
-
byte[] chunk = (byte[]) itr.next();
- bos.write(chunk);
- }
- data = bos.toByteArray();
-
} finally {
-
if (bos != null) {
- bos.close();
- }
- }
- }
-
return data;
- }
分享到:
相关推荐
java文件读写操作大全java文件读写操作大全java文件读写操作大全java文件读写操作大全java文件读写操作大全
java学习笔记,新手入门,java文件读写的一些例子!
java文件读写,读取properties配置文件,读取目录及文件
Java实现对文件的读写,可设置编码格式
java 文件读写 java 文件读写java 文件读写 java 文件读写java 文件读写 java 文件读写java 文件读写 java 文件读写
java文件读写,这是本人总结的java读写文件的一下操作 java文件读写,这是本人总结的java读写文件的一下操作 java文件读写,这是本人总结的java读写文件的一下操作
Java文件读写.pdf
/** * 一、BufferedReader类 public class BufferedReader extends Reader * 从字符输入流中读取文本,缓冲... * 三、FileWriter(少量文字) 和 BufferedWriter(大量文字)实现简单文件写操作 * @author hulk */
java文件读写操作大全 一、获得控制台用户输入的信息 二、复制文件 三、写文件 四、文件重命名 五、转移文件目录 六、读文件 七、创建文件(文件夹) 八、删除文件(目录)
JAVA 文件读写操作 在 JDK 1.0 中,通常是用 InputStream & OutputStream 这两个基类来进行读写操作的。 InputStream 中的 FileInputStream 类似一个文件句柄,通过它来对文件进行操作
完整的java文件读写工具类 包括读取、创建、删除文件, 检查、创建、删除目录
java中用FileUtil实现文件读写
java文件读写,不知道的可以看看 很简单,高手就不用看了,没意思
利用JAVA文件读写流编写的学生点名系统,当时用来作为大学课程报告写得。
java文件读写代码.pdf