Java input and output stream 1 Fileinputstrem&&fileoutstream

Source: Internet
Author: User

Read the Ask price through the file input stream

 PackageUnit6;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;Importjava.io.IOException;Importjava.nio.channels.FileLockInterruptionException; Public classMyType { Public Static voidMain (string[] args) {Try{fileinputstream fin=NewFileInputStream (args[0]); intCh=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!"); }    }}

Copying files from the file input and output stream

 PackageUnit6;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportOrg.xml.sax.InputSource; Public classCopy { Public Static voidMain (string[] args) {intNumberread=0; InputStream in=NULL; OutputStream out=NULL; byteBuf[] =New byte[512]; if(args.length!=2) {System.out.println ("Usage:java copy sourcefile destfile"); System.exit (0); }        Try{ in=NewFileInputStream (args[0]); out=NewFileOutputStream (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"); }}

Read characters by file, encrypt files, save as new file

 PackageUnit6;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classjmcopy { Public Static voidMain (string[] args) {intch; FileInputStream Fin=NULL; FileOutputStream Fout=NULL; Try{fin=NewFileInputStream (args[0]); Fout=NewFileOutputStream (args[1]); intKey=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!"); }    }}

Java input and output stream 1 Fileinputstrem&&fileoutstream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.