zoukankan      html  css  js  c++  java
  • 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!");
            }
        }
    }
  • 相关阅读:
    c# 自定義事件
    c# 判斷事件中鼠標的左右按鍵
    I swear(我發誓)
    SQL Server实用操作小技巧集合
    富人和穷人的经典差异
    Mssql入门语句
    c# 匿名方法學習
    SOS: Autodesk MapGuide Studio Preview can not display "Feature Label" with Chinese text on the platform of MapGuide Open Source
    MapGuide open source开发心得二: 资源
    moblie development based on .net compact framework2 solution:activeX
  • 原文地址:https://www.cnblogs.com/superxuezhazha/p/5727609.html
Copyright © 2011-2022 走看看