Wu Yuxiong - natural born JAVA development of learning: flow (Stream), file (File) and IO

BufferedReader br = new BufferedReader(new 
                      InputStreamReader(System.in));
// use BufferedReader console reads the character 
 
Import the java.io. * ; 
 
public  class BRRead {
     public  static  void main (String args []) throws IOException {
         char C;
         // use System.in create BufferedReader 
        BufferedReader br = new new BufferedReader ( new new the InputStreamReader (the System.in)); 
        System.out.println ( "input character, press 'q' to quit." );
         // read characters 
        do { 
            C = ( char ) br.read (); 
            the System .out.println (c); 
        } while (c != 'q');
    }
}

//使用 BufferedReader 在控制台读取字符
import java.io.*;
 
public class BRReadLines {
    public static void main(String args[]) throws IOException {
        // 使用 System.in 创建 BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        System.out.println("Enter lines of text.");
        System.out.println("Enter 'end' to quit.");
        do {
            str = br.readLine();
            System.out.println(str);
        } while (!str.equals("end"));
    }
}

import java.io.*;
 
//演示 System.out.write().
public class WriteDemo {
    public static void main(String args[]) {
        int b;
        b = 'A';
        System.out.write(b);
        System.out.write('\n');
    }
}
import java.io.*;
 
public class fileStreamTest {
    public static void main(String args[]) {
        try {
            byte bWrite[] = { 11, 21, 3, 40, 5 };
            OutputStream os = new FileOutputStream("test.txt");
            for (int x = 0; x < bWrite.length; x++) {
                os.write(bWrite[x]); // writes the bytes
            }
            os.close();
 
            InputStream is = new FileInputStream("test.txt");
            int size = is.available();
 
            for (int i = 0; i < size; i++) {
                System.out.print((char) is.read() + "  ");
            }
            is.close();
        } catch (IOException e) {
            System.out.print("Exception");
        }
    }
}
// File name: fileStreamTest2.java 
Import java.io. * ; 
 
public  class fileStreamTest2 {
     public  static  void main (String [] args) throws IOException { 
 
        File f = new new File ( "a.txt" ); 
        FileOutputStream FOP = new new FileOutputStream (F);
         // build FileOutputStream object, file does not exist is automatically created 
 
        OutputStreamWriter Writer = new new OutputStreamWriter (FOP, "UTF-. 8" );
         // build OutputStreamWriter objects, the encoding parameters can be specified, the default is the operating system default encoding, windows on is gbk
 
        Writer.append ( "Chinese input" );
         // written to the buffer 
 
        Writer.append ( "\ r \ the n-" );
         // wrap 
 
        Writer.append ( "English" );
         // cache is flushed red, write to file, if the following has no written content, and also written directly to close 
 
        writer.Close (); 
        // close the write stream, while the buffer will write the contents of the file, so the above comment out 
 
        fop.close ( ); 
        // close the output stream, freeing system resources 
 
        FileInputStream FIP = new new FileInputStream (F);
         // build the object FileInputStream 
 
        the InputStreamReader Reader = new new the InputStreamReader (FIP, "UTF-. 8" );
        // build InputStreamReader objects, and writing the same encoding 
 
        StringBuffer SB = new new StringBuffer ();
         the while (reader.ready ()) { 
            sb.append (( char ) reader.Read ());
             // switch to char added StringBuffer subject 
        } 
        System.out.println (sb.toString ()); 
        reader.Close (); 
        // Close for read 
 
        fip.close (); 
        // close the input stream, free system resources 
 
    } 
}
import java.io.File;
 
public class CreateDir {
    public static void main(String args[]) {
        String dirname = "/tmp/user/java/bin";
        File d = new File(dirname);
        // 现在创建目录
        d.mkdirs();
    }
}
import java.io.File;
 
public class DirList {
    public static void main(String args[]) {
        String dirname = "/tmp";
        File f1 = new File(dirname);
        if (f1.isDirectory()) {
            System.out.println("目录 " + dirname);
            String s[] = f1.list();
            for (int i = 0; i < s.length; i++) {
                File f = new File(dirname + "/" + s[i]);
                if(f.isDirectory ()) { 
                    System.out.println (S [I] + "is a directory" ); 
                } the else { 
                    System.out.println (S [I] + "is a file" ); 
                } 
            } 
        } the else { 
            System.out.println (dirname + "is not a directory" ); 
        } 
    } 
}

import java.io.File;
 
public class DeleteFileDemo {
    public static void main(String args[]) {
        // 这里修改为自己的测试目录
        File folder = new File("/tmp/java/");
        deleteFolder(folder);
    }
 
    // 删除文件及目录
    public static void deleteFolder(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File f : files) {
                if (f.isDirectory()) {
                    deleteFolder(f);
                } else {
                    f.delete();
                }
            }
        }
        folder.delete();
    }
}

 

Guess you like

Origin www.cnblogs.com/tszr/p/10962960.html