码迷,mamicode.com
首页 > 编程语言 > 详细

java之IO输出字节流相关操作

时间:2017-06-11 23:35:56      阅读:317      评论:0      收藏:0      [点我收藏+]

标签:port   文件的   get   字节流   man   print   文件   传递   close   

输出字节流:

--------| OutputStream 是所有输出字节流 的父类。 抽象类
-----------| FileOutStream 向文件输出数据的输出字节流。

FileOutputStream如何使用呢?
1. 找到目标文件
2. 建立数据的输出通道。
3. 把数据转换成字节数组写出。
4. 关闭资源

方法一:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class outstream {

    public static void main(String[] args) throws IOException {
            writetest();
            System.out.println("写入完成了,,,,,,");
    }
    public static void writetest() throws IOException{
        File file=new File("c:\\a.txt");
        FileOutputStream fileOutputStream=new FileOutputStream(file);
        fileOutputStream.write(h);
        fileOutputStream.write(e);
        fileOutputStream.write(l);
        fileOutputStream.write(l);
        fileOutputStream.write(o);
        fileOutputStream.close();
    }

}

此时将hello写入a.txt文件中。

每次只能将一个字符写入,比较麻烦。

方法二:

    //使用字节数组把数据写出。
        public static void writeTest2() throws IOException{
            //找到目标文件
            File file = new File("F:\\b.txt");
            //建立数据输出通道
            FileOutputStream fileOutputStream = new FileOutputStream(file,true);
            //把数据写出。
            String data = "\r\nhello world";
            fileOutputStream.write(data.getBytes());
            //关闭资源
            fileOutputStream.close();
        }

FileOutputStream要注意的细节:
1. 使用FileOutputStream 的时候,如果目标文件不存在,那么会自动创建目标文件对象。
2. 使用FileOutputStream写数据的时候,如果目标文件已经存在,那么会先清空目标文件中的数据,然后再写入数据。
3.使用FileOutputStream写数据的时候, 如果目标文件已经存在,需要在原来数据基础上追加数据的时候应该使用new FileOutputStream(file,true)构造函数,第二参数为true。
4.使用FileOutputStream的write方法写数据的时候,虽然接收的是一个int类型的数据,但是真正写出的只是一个字节的数据,只是
把低八位的二进制数据写出,其他二十四位数据全部丢弃。
00000000-000000000-00000001-11111111 511
11111111---> -1

 

 练习:将文件拷贝至其他路径下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
需求: 拷贝一张图片。
*/
public class CopyImage {

    public static void main(String[] args) throws IOException {
        //找到目标文件
        File inFile = new File("F:\\美女\\1.jpg");
        File destFile = new File("E:\\1.jpg");
        //建立数据的输入输出通道
        FileInputStream fileInputStream = new  FileInputStream(inFile);
        FileOutputStream fileOutputStream = new FileOutputStream(destFile); //追加数据....
        //每新创建一个FileOutputStream的时候,默认情况下FileOutputStream 的指针是指向了文件的开始的位置。 每写出一次,指向都会出现相应移动。
        //建立缓冲数据,边读边写
        byte[] buf = new byte[1024]; 
        int length = 0 ; 
        while((length = fileInputStream.read(buf))!=-1){ //最后一次只剩下了824个字节
            fileOutputStream.write(buf,0,length); //写出很多次数据,所以就必须要追加。
        }
        //关闭资源 原则: 先开后关,后开先关。
        fileOutputStream.close();
        fileInputStream.close();
    }

}

 异常处理:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.management.RuntimeErrorException;

/*
 IO异常 的处理

 */
public class Demo1 {

    public static void main(String[] args) {
    //    readTest();
        
        copyImage();
    }
    // 拷贝图片
    public static void copyImage() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            // 找到目标文件
            File inFile = new File("F:\\美女\\1.jpg");
            File outFile = new File("E:\\1.jpg");
            // 建立输入输出通道
            fileInputStream = new FileInputStream(inFile);
            fileOutputStream = new FileOutputStream(outFile);
            // 建立缓冲数组,边读边写
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = fileInputStream.read(buf)) != -1) {
                fileOutputStream.write(buf, 0, length);
            }
        } catch (IOException e) {
            System.out.println("拷贝图片出错...");
            throw new RuntimeException(e);
        } finally {
            // 关闭资源
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                    System.out.println("关闭输出流对象成功...");
                }
            } catch (IOException e) {
                System.out.println("关闭输出流资源失败...");
                throw new RuntimeException(e);
            } finally {
                if (fileInputStream != null) {
                    try {
                        fileInputStream.close();
                        System.out.println("关闭输入流对象成功...");
                    } catch (IOException e) {
                        System.out.println("关闭输入流对象失败...");
                        throw new RuntimeException(e);
                    }
                }

            }
        }
    }

    public static void readTest() {
        FileInputStream fileInputStream = null;
        try {
            // 找到目标文件
            File file = new File("F:\\aaaaa.txt");
            // 建立数据输入通道
            fileInputStream = new FileInputStream(file);
            // 建立缓冲数组读取数据
            byte[] buf = new byte[1024];
            int length = 0;
            while ((length = fileInputStream.read(buf)) != -1) {
                System.out.print(new String(buf, 0, length));
            }
        } catch (IOException e) {
            /*
             * //处理的代码... 首先你要阻止后面的代码执行,而且要需要通知调用者这里出错了... throw new
             * RuntimeException(e);
             * //把IOException传递给RuntimeException包装一层,然后再抛出,这样子做的目的是
             * 为了让调用者使用变得更加灵活。
             */
            System.out.println("读取文件资源出错....");
            throw new RuntimeException(e);
        } finally {
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                    System.out.println("关闭资源成功...");
                }
            } catch (IOException e) {
                System.out.println("关闭资源失败...");
                throw new RuntimeException(e);
            }
        }
    }

}

 

java之IO输出字节流相关操作

标签:port   文件的   get   字节流   man   print   文件   传递   close   

原文地址:http://www.cnblogs.com/AllenRandolph/p/6986593.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!