《疯狂Java讲义》(三十一)---- 输入输出

  • File类

不管是文件还是目录都是使用File来操作的,File能新建,删除,重命名文件和目录,File不能访问文件内容本身,如果需要访问文件内容本身,则需要使用输入输出流。

import java.io.File;
import java.io.IOException;

public class FileDemo {

    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub

        File file = new File(".");
        System.out.println(file.getName());
        System.out.println(file.getParent());
        System.out.println(file.getAbsoluteFile());
        System.out.println(file.getAbsoluteFile().getParent());
        File tmpFile = File.createTempFile("aaa", ".txt", file);
        tmpFile.deleteOnExit();
        File newFile = new File(System.currentTimeMillis() + "");
        System.out.println("if newFile exists:" + newFile.exists());
        newFile.createNewFile();
        newFile.mkdir();
        String[] fileList = file.list();
        System.out.println("---------------");
        for(String fileName : fileList) {
            System.out.println(fileName);
        }
        System.out.println("---------------");
        File[] roots = File.listRoots();
        for(File root : roots) {
            System.out.println(root);
        }
        
    }

}
  • 文件过滤器
import java.io.File;
import java.io.FilenameFilter;

public class FilenameFilterDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        File file = new File(".");
        String[] nameList = file.list(new MyFilenameFilter());
        for(String name : nameList) {
            System.out.println(name);
        }
    }

}

class MyFilenameFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return name.endsWith(".java") || new File(name).isDirectory();
    }
}
  • 输入流/输出流

字节流主要由InputStream和OutputStream作为基类,而字符流则主要由Reader和Writer作为基类。

package com.ivy.io;

import java.io.FileInputStream;
import java.io.IOException;

import com.sun.org.apache.bcel.internal.generic.NEW;

public class FileInputStreamDemo {

    public static void main(String[] args) throws IOException{
        // TODO Auto-generated method stub

        FileInputStream fis = new FileInputStream("./src/com/ivy/io/FileInputStreamDemo.java");
        byte[] bbuf = new byte[1024];
        int hasRead =0;
        while((hasRead = fis.read(bbuf)) > 0) {
            System.out.println(new String(bbuf, 0, hasRead));
        }
        fis.close();
    }

}
package com.ivy.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try (FileInputStream fis = new FileInputStream("./src/com/ivy/io/FileOutputStreamDemo.java");
                FileOutputStream fos = new FileOutputStream("newFile.txt")) {
            byte[] bbuf = new byte[1024];
            int hasRead = 0;
            while((hasRead = fis.read(bbuf)) > 0) {
                fos.write(bbuf, 0, hasRead);
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}
package com.ivy.io;

import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try (
                FileWriter fw = new FileWriter("fileWriterOutput.txt")) {
            fw.write("hello world!
");
            fw.write("hello Java!
");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

}
  • 处理流

如果流的构造参数不是一个物理节点,而是已经存在的流,那么这种流就一定是处理流。 而所有节点流都是直接以物理IO节点作为构造参数的。

package com.ivy.io;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

public class PrintStreamDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try (
                FileOutputStream fos = new FileOutputStream("test.txt");
                PrintStream ps = new PrintStream(fos)) {
            ps.println("nomal string");
            ps.println(new PrintStreamDemo());
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } 
    }

}
  • 字节流/字符流的选择

如果进行输入/输出的内容是文本内容,则应该考虑使用字符流;如果进行输入/输出的内容是二进制内容,则应该考虑使用字节流。

  • 转换流

输入输出体系中还提供了两个转换流,这两个转换流用于实现将字节流转换为字符流。InputStreamReader将字节输入流转换为字符输入流,OutputStreamWriter将字节输出流转换成字符输出流。

package com.ivy.io;

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

public class KeyInDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try (
                InputStreamReader reader = new InputStreamReader(System.in);
                BufferedReader br = new BufferedReader(reader)) {
            String buffer = null;
            while((buffer = br.readLine()) != null) {
                if (buffer.equals("exit")) {
                    System.exit(1);
                }
                System.out.println(buffer);
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
    }

}

BufferedReader流具有缓冲功能,它可以一次读取一行文本--以换行符作为标志,如果它没有读到换行符,则程序阻塞,等到读到换行符为止。因为BufferedReader有readLine()方法,可以非常方便地一次读入一行内容,所以经常把读取文本内容的输入流包装成BufferedReader,用来方便地读取输入流的文本内容。

  • 重定向标准输入/输出

Java的标准输入/输出分别通过System.in和System.out来代表,分别代表键盘和显示器。在System类里有三个重定向标准输入/输出的方法。

setErr(PrintStream err), setIn(InputStream in), setOut(OutputStream os)

  •  RandomAccessFile
package com.ivy.io;

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

public class RandaomAccessFileDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try (RandomAccessFile raf = new RandomAccessFile("./src/com/ivy/io/RandaomAccessFileDemo.java", "r"))
        {
            raf.seek(300);
            byte[] bbuf = new byte[1024];
            int hasRead = 0;
            while((hasRead = raf.read(bbuf)) > 0) {
                System.out.println(new String(bbuf, 0, hasRead));
            }
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }

}
原文地址:https://www.cnblogs.com/IvySue/p/6400212.html