java 输入输出流1 FileInputStrem&&FileOutStream

通过文件输入流读取问价

package unit6;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.FileLockInterruptionException;

public class mytype {

    public static void main(String[] args) {
        try{
            FileInputStream fin= new FileInputStream(args[0]);
            int ch=fin.read();
            while(ch!=-1){
                System.out.println((char )ch);
                ch=fin.read();
            }
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("use the right style: java mytype filename");
            System.exit(0);
        
        }catch (FileNotFoundException e2) {

            System.out.println("file does not find");
        }catch (IOException e3) {

            System.out.println("input stream error!");
        }
    }
}

通过文件输入输出流复制文件

package unit6;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.xml.sax.InputSource;

public class copy {

    public static void main(String[] args) {
        int numberRead=0;
        InputStream in = null;
        OutputStream out = null;
        byte buf[] = new byte[512];
        if(args.length!=2){
            System.out.println("Usage: java copy sourcefile destfile");
            System.exit(0);
        }
        try{
            in=new FileInputStream(args[0]);
            out= new FileOutputStream(args[1]);
            while((numberRead=in.read(buf))!=-1){
                out.write(buf,0,numberRead);
            }
        }catch (FileNotFoundException e1) {

            System.out.println(args[0]+" not found");
            System.exit(0);
        }catch (IOException e2) {

            System.out.println("Error reading/writing file.");
        }finally{
            try{
                in.close();
                out.close();
            }catch (Exception e) {

                e.printStackTrace();
            }
        }
        System.out.println("1 file copyed");
    }
}

按文件读入字符,并且对文件进行加密,保存为新的文件

package unit6;

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

public class jmcopy {

    public static void main(String[] args) {
        int ch;
        FileInputStream fin = null;
        FileOutputStream fout =null;
        try{
            fin=new FileInputStream(args[0]);
            fout=new FileOutputStream(args[1]);
            int key=args[2].length();
            ch=fin.read();
            while(ch!=-1){
                fout.write(ch^key);
                ch=fin.read();
            }
            fin.close();fout.close();
        }catch (ArrayIndexOutOfBoundsException e1) {

            System.out.println("fomat error,type: java jmcopy sourcefile destfile key");
            System.exit(0);
        }catch (FileNotFoundException e2) {

            System.out.println("file not found");
        }catch (IOException e3) {

            System.out.println("strem error!");
        }
    }
}
原文地址:https://www.cnblogs.com/superxuezhazha/p/5727609.html