Java教程

节点流:字节流和字符流(Java语言)

本文主要是介绍节点流:字节流和字符流(Java语言),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
import java.io.*;
/**
 * 节点流:
 * 字节流和字符流
 * */
public class Text{
    public static void main(String[] args) throws IOException {
        m10();
    }
    private static void m1() throws IOException {
        File file = new File("d:\\a/b/c");
        file.createNewFile();
        System.out.println(file.exists());
        System.out.println(file.isFile());
        System.out.println(file.getPath());
        System.out.println(file.getName());
        System.out.println(file.isDirectory());
        System.out.println(file.getAbsolutePath());
        System.out.println(file.lastModified());
        System.out.println(file.length());
        System.out.println(file.getParent());
        System.out.println(file.getAbsoluteFile());
    }
    private static void m2(){
        File f=new File("d:\\a.txt");
        f.mkdirs();
        f.delete();
    }
    private  static void m3(String path){
        File f=new File(path);
        File files[] =f.listFiles();//所有文件+文件夹的名字
        if(files==null){
            return;
        }
        for(File file:files){
            System.out.println(file.getName());
            if(file.isDirectory()){
                m3(file.getPath());//递归
            }
        }
    }
    private static void m4(){
        File f=new File("D:\\IDEA/hello");
        String strs[]=f.list();//当前路径下所有文件+文件夹的名字
        for(String s:strs){
            System.out.println(s);
        }
    }
    private static void m5() throws IOException {
        InputStream in=new FileInputStream("d:\\a.txt");
        System.out.println(in.read());
        System.out.println(in.read());
        System.out.println(in.read());
        System.out.println(in.read());
        System.out.println(in.read());
        in.close();
    }
    private static void m6() throws IOException {
        InputStream in=new FileInputStream("d:\\a.txt");
        int x;
        while((x=in.read())!=-1){
            System.out.print((char)x);
        }
        in.close();
    }
    private static void m7() throws IOException{
        InputStream in=new FileInputStream("d:\\a.txt");
        byte b[]=new byte[120];
        int len;
        while((len=in.read(b))!=-1){
            System.out.println(new String(b,0,len));
        }
        in.close();
    }
    private static void m8() throws IOException{
        OutputStream out=new FileOutputStream("d:\\a.txt",true);
        out.write("啊啊啊\r".getBytes());
        out.write("啊啊啊\n".getBytes());
        out.write("哈哈哈\r\n".getBytes());
        out.write("啊啊啊\n\r".getBytes());
        out.close();
    }
    private static void m9() throws IOException{
        OutputStream out=new FileOutputStream("d:\\a.txt");
        InputStream in=new FileInputStream("d:\\a.txt");
        byte b[]=new byte[1024*1024*50];
        int len=0;
        while((len=in.read(b))!=-1){
            out.write(b,0,len);
        }
        out.close();
        in.close();
    }
    private static void m10() throws IOException{
        Reader reader=new FileReader("d:\\a.txt");
        char cs[]=new char[10];
        int len=0;
        while((len=reader.read(cs))!=-1){
//            System.out.println("aaaaaaa");
            System.out.print(new String(cs,0,len)+"\r\n");
        }
        reader.close();
    }
    private static void m11() throws IOException{
        Writer write=new FileWriter("d:\\c.txt");
        write.write("哈哈哈哈哈啊啊啊啊啊啊啊");
        write.write("哈哈哈哈哈啊啊啊啊啊啊啊");
        write.write("哈哈哈哈哈啊啊啊啊啊啊啊");
        write.write("哈哈哈哈哈啊啊啊啊啊啊啊");
        write.close();
    }
    public static void copy(String src, String dst) {
        try (InputStream in = new FileInputStream(src);
             OutputStream out = new FileOutputStream(dst)) {
            byte[] buff = new byte[1024];
            int n;
            while ((n = in.read(buff)) >= 0) {
                out.write(buff, 0, n);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
这篇关于节点流:字节流和字符流(Java语言)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!