<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    InputStream & OutputStream

    package think.in.java.io;

    import java.io.IOException;
    import java.io.StringReader;
    /**
     * read( ) returns the next character as an int and thus it must be cast to a char to print properly.
     * 
    @author WPeng
     * 
    @since 2012-11-5
     
    */
    public class MemoryInput {

        
    public static void main(String[] args) throws IOException{
            StringReader in 
    = new StringReader(BufferedInputFile.read("MemoryInput.java"));
            
    int c;
            
    while((c = in.read()) != -1){
                System.out.println(c);
                System.out.println((
    char)c);
            }
        }
    }

    package think.in.java.io;

    import java.io.BufferedInputStream;
    import java.io.ByteArrayInputStream;
    import java.io.DataInputStream;
    import java.io.EOFException;
    import java.io.FileInputStream;
    import java.io.IOException;

    /**
     * DataInputStream, which is a byteoriented I/O class (rather than char-oriented). 
     * Thus you must use all InputStream classes rather than Reader classes.
     * 
     * 典型的裝飾者模式 - decorator pattern
     * available( ) method to find out how many more characters are available. 
     * Here’s an example that shows how to read a file one byte at a time:
     * 
    @author WPeng
     *
     * 
    @since 2012-11-5
     
    */
    public class FormattedMemoryInput {

        
    public static void main(String[] args) throws IOException{
            
    try {
                DataInputStream in 
    = 
                    
    new DataInputStream(
                            
    new ByteArrayInputStream(
                                    BufferedInputFile.read(
    "FormattedMemoryInput.java").getBytes()));
                
    while(true){
                    System.out.print((
    char)in.readByte());
                }
            } 
    catch (EOFException e) {
                
    // TODO: handle exception
            }
            
            DataInputStream in 
    = 
                
    new DataInputStream(
                        
    new BufferedInputStream(
                                
    new FileInputStream("FormattedMemoryInput.java")));
            
    while(true){
                System.out.print((
    char)in.readByte());
            }
        }
    }

    package think.in.java.io;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.StringReader;

    /**
     * want to buffer the output by wrapping it in a BufferedWriter. 
     * this wrapping  dramatically increase performance of I/O operations.
     * 
    @author WPeng
     *
     * 
    @since 2012-11-5
     
    */
    public class BasicFileOutput {

        
    static String file = "BasicFileOutput.out";
        
    static String file_2 = "FileOutputShortcut.out";

        
    public static void main(String[] args) throws IOException {
            BufferedReader in 
    = new BufferedReader(
                    
    new StringReader(
                            BufferedInputFile.read(
    "BasicFileOutput.java")));
            
    // PrintWriter -> BufferedWriter
            PrintWriter out = new PrintWriter(
                    
    new BufferedWriter(new FileWriter(file)));
            
    int lineCount = 1;
            String s;
            
    while ((s = in.readLine()) != null)
                out.println(lineCount
    ++ + "" + s);
            
    /**
             * You’ll see an explicit close( ) for out, 
             * because if you don’t call close( ) for all your output files, 
             * you might discover that the buffers don’t get flushed, 
             * so the file will be incomplete.
             
    */
            out.close();
            
    // Show the stored file:
            System.out.println(BufferedInputFile.read(file));
            
            
    /**
             * Java SE5 added a helper constructor to PrintWriter 
             * so that you don’t have to do all the decoration by hand 
             * every time you want to create a text file and write to it.
             
    */
            
    // Here’s the shortcut:
            PrintWriter out_2 = new PrintWriter(file_2);
            
    int lineCount_2 = 1;
            String s_2;
            
    while((s_2 = in.readLine()) != null )
                out_2.println(lineCount_2
    ++ + "" + s_2);
            out_2.close();
            
    // Show the stored file:
            System.out.println(BufferedInputFile.read(file_2));
        }
    }

    package think.in.java.io;

    import java.io.IOException;
    import java.io.RandomAccessFile;

    /**
     * Using a RandomAccessFile is like using a combined DataInputStream and DataOutputStream 
     * (because it implements the same interfaces: DataInput and DataOutput)
     * When using RandomAccessFile, you must know the layout of the file so that you can manipulate it properly. 
     * RandomAccessFile has specific methods to read and write primitives and UTF-8 strings.
     * cannot combine it with any of the aspects of the InputStream and OutputStream subclasses
     * 
     * 
    @author WPeng
     * 
    @since 2012-11-5
     
    */
    public class UsingRandomAccessFile {
        
    static String file = "rtest.dat";
        
    static void display() throws IOException{
            RandomAccessFile rf 
    = new RandomAccessFile(file, "r");
            
    for(int i=0; i<7; i++){
                System.out.println(
    "Value " + i + "" + rf.readDouble());
            }
            System.out.println(rf.readUTF());
            rf.close();
        }
        
        
    public static void main(String[] args) throws IOException{
            RandomAccessFile rf 
    = new RandomAccessFile(file, "rw");
            
    for(int i=0; i<7; i++){
                rf.writeDouble(i
    *1.414);
            }
            rf.writeUTF(
    "The end of the file");
            rf.close();
            display();
            
            rf 
    = new RandomAccessFile(file, "rw");
            
    /** 
             * can use seek( ) to move about in the file and change the values.
             * a double is always eight bytes long, 
             * to seek( ) to double number 5 you just multiply 5*8 to produce the seek value.
             
    */
            rf.seek(
    5*8);
            rf.writeDouble(
    47.0001);
            rf.close();
            display();
        }
    }
    File reading & writing utilities
    package think.in.java.io;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.TreeSet;

    /**
     * Static functions for reading and writing text files as a single string, and 
     * treating a file as an ArrayList.
     * 
    @author WPeng
     *
     * 
    @since 2012-11-5
     
    */
    public class TextFile extends ArrayList<String>{

        
    // Read a file as a single string:
        public static String read(String fileName){
            StringBuilder sb 
    = new StringBuilder();
            
    try {
                BufferedReader in 
    =  new BufferedReader(
                        
    new FileReader(
                                
    new File(fileName).getAbsoluteFile()));
                
    try{
                    String s;
                    
    while((s = in.readLine()) != null){
                        sb.append(s);
                        sb.append(
    "\n");
                    }
                }
    finally{
                    in.close();
                }
            } 
    catch (IOException e) {
                
    throw new RuntimeException(e);
            }
            
    return sb.toString();
        }
        
        
    // Write a single file in one method call:
        public static void write(String fileName, String text){
            
    try {
                PrintWriter out 
    = new PrintWriter(
                        
    new File(fileName).getAbsoluteFile());
                
    try{
                    out.print(text);
                }
    finally{
                    out.close();
                }
            } 
    catch (IOException e) {
                
    throw new RuntimeException(e);
            } 
        }
        
        
    // Read a file, split by any regular expression:
        public TextFile(String fileName, String splitter){
            
    super(Arrays.asList(read(fileName).split(splitter)));
            
    // Regular expression split() often leaves an empty string at the first position
            if (get(0).equals("")){
                remove(
    0);
            }
        }
        
        
    // Normally read by lines:
        public TextFile(String fileName){
            
    this(fileName, "\n");
        }
        
        
    public void write(String fileName){
            
    try {
                PrintWriter out 
    = new PrintWriter(
                        
    new File(fileName).getAbsoluteFile());
                
    try{
                    
    for(String item : this){
                        out.println(item);
                    }
                }
    finally{
                    out.close();
                }
            } 
    catch (Exception e) {
                
    throw new RuntimeException(e);
            }
        }
        
        
    /**
         * Simple test: 
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            String file 
    = read("TextFile.java");
            write(
    "test.txt", file);
            TextFile text 
    = new TextFile("test.txt");
            text.write(
    "test2.txt");
            
    // Break into unique sorted list of words:
            TreeSet<String> words = new TreeSet<String>(
                    
    new TextFile("TextFile.java","\\w+"));
            
    // Display the capitalized words:
            System.out.println(words.headSet("a"));
        }

    }
    Reading & Writing binary files
    package think.in.java.io;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;

    /**
     * Utility for reading files in binary form.
     * 
    @author WPeng
     *
     * 
    @since 2012-11-6
     
    */
    public class BinaryFile {
        
        
    public static byte[] read(File bfile) throws IOException{
            BufferedInputStream bf 
    = new BufferedInputStream(
                    
    new FileInputStream(bfile));
            
    try {
                
    byte[] data = new byte[bf.available()];
                bf.read(data);
                
    return data;
            } 
    finally{
                bf.close();
            }
        }
        
        
    public static byte[] read(String bFile) throws IOException{
            
    return read(new File(bFile).getAbsoluteFile());
        }
        
        
    public static void write(File bfile, byte[] data) throws IOException{
            BufferedOutputStream bf 
    = new BufferedOutputStream(
                    
    new FileOutputStream(bfile));
            
    try{
                bf.write(data);
            }
    finally{
                bf.close();
            }
        }
        
        
    public static void write(String bFile, byte[] data) throws IOException{
            write(
    new File(bFile).getAbsoluteFile(), data);
        }
        
        
    public static void main(String[] args) throws IOException  {
            
    byte[] data = read("123.jpg");
            write(
    "321.jpg", data);
        }

    }
    Redirecting standard I/O
    package think.in.java.io;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.BufferedReader;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;

    /**
     * Demonstrates standard I/O redirection
     * I/O redirection manipulates streams of bytes
     * 
    @author WPeng
     *
     * 
    @since 2012-11-6
     
    */
    public class Redirecting {

        
    public static void main(String[] args) throws IOException {
            
    /**
             * This program attaches standard input to a file and redirects standard output and standard error to another file.
             * 
    */
            PrintStream console 
    = System.out;
            
    /**
             * Notice that it stores a reference to the original System.
             * out object at the beginning of the program, 
             * and restores the system output to that object at the end.
             * 
    */
            BufferedInputStream in 
    = new BufferedInputStream(
                    
    new FileInputStream("Redirecting.java"));
            PrintStream out 
    = new PrintStream(
                    
    new BufferedOutputStream(
                            
    new FileOutputStream("test.out")));
            System.setIn(in);
            System.setOut(out);
            System.setErr(out);
            
            BufferedReader br 
    = new BufferedReader(
                    
    new InputStreamReader(System.in));
            String s;
            
    while((s=br.readLine()) != null){
                System.out.println(s);
            }
            out.close();
            System.setOut(console);
        }

    }
    Process control
    execute other operating system programs from inside Java, and to control the input and output from such programs.

    package think.in.java.io;

    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    /**
     * Run an operating system command and send the output to the console.
     * 
    @author WPeng
     *
     * 
    @since 2012-11-6
     
    */
    public class OSExcute {
        
        
    public static void command(String command){
            
    boolean err = false;
            
    try {
                
    // To capture the standard output stream from the program as it executes, you call getInputStream( ).
                Process process = new ProcessBuilder(command.split(" ")).start();
                BufferedReader results 
    = new BufferedReader(
                        
    new InputStreamReader(process.getInputStream()));
                String s;
                
    while((s=results.readLine()) != null){
                    System.out.println(s);
                }
                
                BufferedReader errors 
    = new BufferedReader(
                        
    new InputStreamReader(process.getErrorStream()));
                
    // Report errors and return nonzero value to calling process if there are problems.
                while((s = errors.readLine()) != null){
                    System.err.println(s);
                    err 
    = true;
                }
            } 
    catch (Exception e) {
                
    // Compensate for window 2000, which throws an exception for the default command line.
                if(!command.startsWith("CMD /C")){
                    command(
    "CMD /C" +command);
                }
    else{
                    
    throw new RuntimeException();
                }
            }
            
            
    if(err){
                
    throw new OSExecuteException("Errors executing " + command);
            }
        }

        
    /**
         * OSExcute Demo
         * 
    @param args
         
    */
        
    public static void main(String[] args) {
            OSExcute.command(
    "set");
        }

    }

    posted on 2012-11-05 10:09 鹽城小土包 閱讀(331) 評論(0)  編輯  收藏 所屬分類: J2EE

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲AV无码一区二区乱子仑| 中文字幕免费不卡二区| 一级毛片正片免费视频手机看| 日韩免费在线视频| 国产成人高清精品免费鸭子| 国产V亚洲V天堂无码久久久| 亚洲av无码成人精品区一本二本 | 亚洲AV无码一区二区乱子伦| 亚洲人成电影网站色www| 免费看一区二区三区四区| 国产亚洲综合久久系列| 日本高清不卡中文字幕免费| 好男人www免费高清视频在线| 国产精品国产亚洲精品看不卡| 免费看无码特级毛片| 亚洲日本视频在线观看| 久草免费手机视频| 亚洲成人高清在线观看| 免费毛片在线视频| 亚洲精品免费在线视频| 日本免费中文字幕| 亚洲国产精品综合福利专区| 免费网站看v片在线香蕉| 羞羞视频免费网站日本| 亚洲成av人片天堂网| 野花高清在线观看免费3中文 | 久久久亚洲欧洲日产国码aⅴ| 一级做受视频免费是看美女| 免费看香港一级毛片| 草久免费在线观看网站| 亚洲AV无码精品色午夜在线观看| av大片在线无码免费| 91亚洲自偷在线观看国产馆| 91精品免费不卡在线观看| 婷婷亚洲综合五月天小说| 色www永久免费网站| 亚洲自偷自偷在线制服| 国产一二三四区乱码免费| 亚洲免费观看视频| 视频免费在线观看| 亚洲欧洲无码一区二区三区|