栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

关于Java中IO流的一些使用方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于Java中IO流的一些使用方法

**

使用IO流读取数据

**

最近没事,就整理了一下Java中使用IO流去读取文档,上传,下载图片和移动文档或图片的类和方法,希望可以帮助到大家更好的掌握IO的读写技巧,下面我会关于这些内容做一个详细的展示,仅供大家参考。

使用IO读取文件数据
public static void copyFile(String oldFile, String newFile){
        //创建一个输入流
        FileInputStream is =null;
        //创建一个输出流
        FileOutputStream os =null;
        try {
            is = new FileInputStream(oldFile);
            os = new FileOutputStream(newFile);
            //创建一个缓冲区,用于一次性读取多个字节
            byte[] data = new byte[1024];
            //读取每次读取的字节数量
            int len = -1;
            while ((len=is.read(data))!=-1){
                //把每次读取到的数据写到一个新的文件夹里面
                os.write(data,0,len);
                System.out.println("复制成功!");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
public class Practice {
    public static void main(String[] args) {
        String oldFile ="";
        String newFile ="";
        FileUtil.copyFile(oldFile,newFile);
    }
}

通过上面的代码,我们可以实现用代码实现文档的复制粘贴功能。即使是跨计算机或者跨地区,我们也可以实现文件的复制和粘贴的功能。

使用IO流实现图片的上传和下载功能
public class TestInput_OutputStream {
    public static void main(String[] args) {
        //输入流
        FileInputStream fis = null;
        DataInputStream dis = null;
        //输出流
        FileOutputStream fos = null;
        DataOutputStream dos = null;

        try {
            //读取相关二进制文件
            fis = new FileInputStream("图片路径");
            dis = new DataInputStream(fis);
            //输出二进制相关文件路径
            fos= new FileOutputStream("复制到的路径");
            dos= new DataOutputStream(fos);

            byte[] data = new byte[1024];
            int len = -1;
            while ((len=dis.read(data))!=-1){
                dos.write(data,0,len);
            }
            //验证是否成功,我们可以在这里加上一个判断
            System.out.println("文件复制成功!");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (dos!=null){
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
总结

其实,在实现这些功能的过程中,有几点还是需要我们去注意的,比如,在完成操作之后,对于流的关闭,如果不关闭的话,可能会出现一些问题,其次就是对于路径的填写,这个就因人而异了,我个人还是喜欢写相对路径,毕竟相对路径使用起来比较方便,绝对路径固然是比较准确,但是,绝对路径写起来会比较繁琐。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/733153.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号