当前位置:网站首页>IO system - code example

IO system - code example

2022-06-12 05:43:00 Python's path to becoming a God

public class StreamDemo4 { 
    
    public static void main(String[] args) { 
    
        InputStream inputStream = null;
        try { 
    
            inputStream = new FileInputStream("abc.txt");
            int length = 0;
            // Add a buffer to read , Each time the data is added to the buffer , When the buffer is full , once   Read , Instead of reading every byte 
            byte[] buffer = new  byte[1024];
            while((length = inputStream.read(buffer,5,5))!=-1){ 
    
                System.out.println(new String(buffer,5,length));
            }
// int read = inputStream.read();
// System.out.println((char)read);
        } catch (FileNotFoundException e) { 
    
            e.printStackTrace();
        } catch (IOException e) { 
    
            e.printStackTrace();
        }finally { 
    
            try { 
    
                inputStream.close();
            } catch (IOException e) { 
    
                e.printStackTrace();
            }
        }
    }
}
public class CopyFile { 
    
    public static void main(String[] args) { 
    

        // Define source data file 
        File src = new File("abc.txt");
        // Define destination data file 
        File dest = new File("aaa.txt");

        // Create an input stream object 
        InputStream inputStream = null;
        // Create an output stream object 
        OutputStream outputStream = null;

        try { 
    
            inputStream = new FileInputStream(src);
            outputStream = new FileOutputStream(dest);

            // Input and output mode with cache 
            byte[] buffer = new byte[1024];
            int length = 0;
            // Complete the process of data transmission 
            while((length = inputStream.read(buffer))!=-1){ 
    
                outputStream.write(buffer);
            }

        } catch (FileNotFoundException e) { 
    
            e.printStackTrace();
        } catch (IOException e) { 
    
            e.printStackTrace();
        }finally { 
    
            try { 
    
                outputStream.close();
            } catch (IOException e) { 
    
                e.printStackTrace();
            }
            try { 
    
                inputStream.close();
            } catch (IOException e) { 
    
                e.printStackTrace();
            }
        }
    }
}
/** *  The character stream can read Chinese characters directly , Chinese garbled code will appear when processing byte stream  */
public class ReaderDemo3 { 
    
    public static void main(String[] args) { 
    
        Reader reader = null;
        try { 
    
            reader = new FileReader("abc.txt");
            int length = 0;
            char[] chars = new char[1024];
            // Add buffer 
            while((length = reader.read(chars))!=-1){ 
    
                System.out.println(new String(chars,0,length));
            }
// int read = reader.read();
// System.out.println((char)read);
        } catch (FileNotFoundException e) { 
    
            e.printStackTrace();
        } catch (IOException e) { 
    
            e.printStackTrace();
        }finally { 
    
            try { 
    
                reader.close();
            } catch (IOException e) { 
    
                e.printStackTrace();
            }
        }
    }
}
public class WriterDemo { 
    
    public static void main(String[] args) { 
    
        File file = new File("writer.txt");
        Writer writer = null;
        try { 
    
            writer = new FileWriter(file);
            writer.write("www.mashibing.com");
            writer.write(" Write something about it ");
            writer.flush();
        } catch (IOException e) { 
    
            e.printStackTrace();
        }finally { 
    
            try { 
    
                writer.close();
            } catch (IOException e) { 
    
                e.printStackTrace();
            }
        }
    }
}

原网站

版权声明
本文为[Python's path to becoming a God]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010613198938.html