Home > Java > javaTutorial > body text

How to write files and copy files through OutputStream in Java

PHPz
Release: 2023-04-13 17:18:07
forward
2335 people have browsed it

    通过OutputStream写入文件与文件复制

    1.知识点

    1,首先不管是InputStream读read,还是OutputStream写write,都支持读写一定长度的byte[]。

    2,当然,还支持一个字节一个字节的读写,那么一个字节一个字节的读写,读出来的字节和写入的字节都是用的int类型的参数。

    3,int参数只会使用它的8个二进制位,也就是说类似于将int强行转换为byte,我感觉很无聊的设计,还不如直接用byte作为参数,这样搞是因为int太火吗?

    4,所以所谓的复制,就是把所有的字节读出来,然后写入文件即可。

    2.写入文件示例

    写入对象FILE_FOR_WRITE中

    //工具类
    package com.maoge.stream;
    import java.io.File;
    public class StreamUtils {
        //用于定义每次读取的长度
        public static final int BUFFER_LENGTH=512;
        //文件对象
        public static File FILE_FOR_READ;
        public static File FILE_FOR_WRITE;
        static{
            FILE_FOR_READ=new File("C:\\test.txt"); 
            FILE_FOR_WRITE=new File("C:\\result.txt"); 
        }
    }
    Copy after login

    写入过程

    package com.maoge.stream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    public class OutputStreamDemo {
        public static void main(String[] args){
            writeToFile();
        }
        public static void writeToFile(){
            byte buffer[]=buffer=new String("hello world!").getBytes();
            OutputStream out=null;
            try {
                out = new FileOutputStream(StreamUtils.FILE_FOR_WRITE);
                out.write(buffer, 0, buffer.length);
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }finally{
                try {
                    out.close();
                } catch (IOException ioEx) {
                    System.out.println(ioEx.toString());
                }
            }    
        }
    }
    Copy after login

    3.文件复制示例

    直接演示复制图片,因为图片也是二进制位组成的

    源文件test.jpg这个需要找个图片,目的文件result.jpg这个是复制后生产的

    //工具类
    package com.maoge.stream;
    import java.io.File;
    public class StreamUtils {
        //用于定义每次读取的长度
        public static final int BUFFER_LENGTH=512;
        //文件对象
        public static File FILE_FOR_READ;
        public static File FILE_FOR_WRITE;
        static{
            FILE_FOR_READ=new File("C:\\test.jpg"); 
            FILE_FOR_WRITE=new File("C:\\result.jpg"); 
        }
    }
    Copy after login

    复制过程,该方法是OutputStreamDemo类的静态方法。

    public static void fileCopy(){
            FileInputStream in=null;
            FileOutputStream out=null;
            try {
                in=new FileInputStream(StreamUtils.FILE_FOR_READ);
                out=new FileOutputStream(StreamUtils.FILE_FOR_WRITE);
                int c;
                while((c=in.read())!=-1)
                    out.write(c);
            } catch (Exception ex) {
                System.out.println(ex.toString());
            }finally{
                try {
                    in.close();
                } catch (IOException ioEx) {
                    System.out.println(ioEx.toString());
                }
                try {
                    out.close();
                } catch (IOException ioEx) {
                    System.out.println(ioEx.toString());
                }
            }
        }
    Copy after login

    java写入文件问题(FileOutputStream的使用)

    实现在控制台写入相关文字,并添加到指定的文件中,并实现读取文件内容

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.Scanner;
     
    public class Fileout {
        Scanner inScanner = new Scanner(System.in);
        public void f(String address) throws IOException
        {
        
        FileOutputStream out=new FileOutputStream(address, true);
        String buf;
        System.out.println("请输入");
        buf= inScanner.nextLine();
        byte buy[]=buf.getBytes();
        out.write(buy);
        
        FileInputStream fin=new FileInputStream(address);    
        byte[] bs=new byte[1024];    
        int count=0;    
     
        while((count=fin.read(bs))>0)                
        {
        
            
            String str=new String(bs,0,count);    //反复定义新变量:每一次都 重新定义新变量,接收新读取的数据
     
            System.out.println(str);        //反复输出新变量:每一次都 输出重新定义的新变量
        }
        fin.close();
        }
        public static void main(String[] args) throws IOException {
            
            Fileout out=new Fileout();
            out.f("f://student.txt");    
        }
    }
    Copy after login

    The above is the detailed content of How to write files and copy files through OutputStream in Java. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template