Javase (9) _java IO system

Source: Internet
Author: User

first, the file class

1. File can represent the name of a particular file, or the name of a group of files under a directory, in fact, FilePath is a better name for this class.
2. Directory List Example:

ImportJava.io.File;ImportJava.io.FilenameFilter;Importjava.util.Arrays;ImportJava.util.regex.Pattern; Public classDirlist { Public Static voidMain (string[] args) {File path=NewFile ("./data/baiduvideo/"));        String[] list; if(Args.length = = 0) List=path.list (); Else{List= Path.list (NewDirfilter (args[0])); } arrays.sort (List,string.case_insensitive_order);//Sort By letter         for(String diritem:list) System.out.println (Diritem); }}classDirfilterImplementsFilenameFilter {Privatepattern pattern;  PublicDirfilter (String regex) {pattern=pattern.compile (regex); } @Override Public BooleanAccept (File dir, String name) {returnpattern.matcher (name). matches (); }}//This is the internal implementation of the list method, the policy design pattern/*Public string[] List (FilenameFilter filter) {String names[] = list (); if ((names = = null) | | (filter = = null))    {return names;    } ArrayList v = new ArrayList ();    for (int i = 0; i < names.length; i++) {if (Filter.accept (this, names[i]) {V.add (names[i]); }} return (string[]) (V.toarray (New String[v.size ()));}*///anonymous internal class rewrite, poor readability Public classDirlist { Public Static voidMainFinalstring[] args) {File path=NewFile ("./data/baiduvideo/"));        String[] list; if(Args.length = = 0) List=path.list (); Else{List= Path.list (NewFilenameFilter () {PrivatePattern pattern = Pattern.compile (args[0]); @Override Public BooleanAccept (File dir, String name) {returnpattern.matcher (name). matches ();        }            }); } arrays.sort (List,string.case_insensitive_order);//Sort By letter         for(String diritem:list) System.out.println (Diritem); }}

Other common uses of the file class are slightly.

Second, IO basic use
 PackageTestio;ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;ImportJava.io.ByteArrayInputStream;ImportJava.io.DataInputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.PrintWriter;ImportJava.io.StringReader; Public classBufferedinputfile {//1. Buffered input file     Public StaticString read (string filename)throwsioexception{//BufferedReader is designed to improve performanceBufferedReader in =NewBufferedReader (Newfilereader (filename));        String s; StringBuilder SB=NewStringBuilder ();  while((S=in.readline ())! =NULL) Sb.append (s); Sb.append ("\ n");//ReadLine has deleted the line breakIn.close (); returnsb.tostring (); }     Public Static voidMain (string[] args)throwsIOException {System.out.println (read ("E:/eclipse/testnginx/src/testio/bufferedinputfile.java")); }} Public classMemoryinput {//2. Input from memory     Public Static voidMain (string[] args)throwsIOException {StringReader in=NewStringReader (bufferedinputfile. Read ("E:/eclipse/testnginx/src/testio/memoryinput.java")); intC;  while((c = In.read ())! = 1) System.out.print (Char) c); }} Public classFormattedmemoryinput {//3, formatted memory input--read characters in bytes, prevent garbled     Public Static voidMain (string[] args)throwsIOException {DataInputStream in=NewDataInputStream (NewBytearrayinputstream (Bufferedinputfile.read ("E:/eclipse/testnginx/src/testio/memoryinput.java"). GetBytes ()));  while(true) System.out.print (Char) In.readbyte ()); }} Public classBasicfileoutput {//1, basic text output, in order to provide a format mechanism, the use of PrintWriter    /*static String file = "Basicfileoutput.out";                public static void Main (string[] args) throws IOException {BufferedReader in = new BufferedReader (New FileReader (        "E://eclipse//testnginx//src//testio//basicfileoutput.java"));        PrintWriter out = new PrintWriter (new BufferedWriter (new FileWriter (file));        String s;        int linecount = 1;        while ((s = in.readline ()) = null) out.println (linecount++ + ":" + s);        Out.close ();    In.close (); }*/    //mode 2 input from memory    StaticString file = "Basicfileoutput.out";  Public Static voidMain (string[] args)throwsIOException {BufferedReader in=NewBufferedReader (NewStringReader (Bufferedinputfile.read ("E:/eclipse/testnginx/src/testio/basicfileoutput.java"))); PrintWriter out=NewPrintWriter (NewBufferedWriter (NewFileWriter (file)); //PrintWriter out = new PrintWriter (file);//a quick way to PrintWriterString S; intLineCount = 1;  while((s = in.readline ())! =NULL) out.println (LineCount++ + ":" +s);        Out.close ();    In.close (); }}

Javase (9) _java IO system

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.