Java I/O系统

    目录、文件管理使用File类,可进行文件(夹)的创建、删除、目录跳转、文件查询等操作。
    Inputstream,OutputStream是Java I/O的基础,所以输入输出相关的类都应该继承自它们。Reader和Writer是后加入的用于兼容Unicode与面向字符的I/O功能。任何自Inputstream或Reader派生而来的类都含有read()基本方法,用于读取单个字节或者字节数组;任何自OutputStream或Writer派生而来的类都有write()方法,用于写单个字节或字节数组。所有的Inputstream,OutputStream都有相应的Reader,Writer来提供天然的Unicode操作。

一、文件读写

1、从文件读取字符

//从文件读取字符
BufferedReader reader;
try {
    reader = new BufferedReader(new FileReader("path"));
    String line;
    while ((line = reader.readLine()) != null){
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}finally {
    //注意一定要关闭流,避免内存泄漏
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2、写入文件

 //写入文件
PrintWriter writer;
try {
    writer = new PrintWriter("filepath");
    writer.println("content ...");
} catch (IOException e) {
    e.printStackTrace();
}finally {
    //注意关闭流,以免造成内存泄漏
    if (writer != null)
        writer.close();
}

3、RandomAccessFile ,使用seek()来跳转到指定位置后进行读写操作,使用RandomAccessFile 需要对文件的排版很清楚。
RandomAccessFile rf = new RandomAccessFile("filepath", "rw");//r只读,rw读写
rf.seek(5 * 8);//定位到第五个字符位置
rf.write("contents".getBytes());

二、标准输入输出

1、读写

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while (true) {
    try {
        if (!((line = reader.readLine()) != null && line.length() != 0)) break;
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(line);
}

2、标准输入、出重定向

方便我们将内容存储到文件,以备后查

System.setIn(InputStream);
System.setOut(PrintStream);
System.setErr(PrintStream);

 

三、多线程文件加锁

FileOutputStream fos =  new FileOutputStream("filepath");
FileLock fileLock = fos.getChannel().tryLock();//非阻塞,如果文件已被其他线程locked且不能共享则返回false,否则则添加锁并返回true
FileLock fileLock2 = fos.getChannel().lock();//阻塞,阻塞进程直至可以获取锁;
fileLock.release();//释放锁
fileLock.isShared();//查询是否是共享锁

 

四、压缩、解压GZIP, Zip文件

//1、gzip适用于单个数据流(而不是一系列互异数据)
//压缩GZIP
BufferedOutputStream gout = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("file.gz")));
gout.write("content".getBytes());
//解压GZIP数据
BufferedReader greader = new BufferedReader(new InputStreamReader(
        new GZIPInputStream(new FileInputStream("filepath"))
));
greader.readLine();

//2、zip可添加多个参数
// zip压缩
ZipOutputStream zout = new ZipOutputStream(new CheckedOutputStream(
        new FileOutputStream("filepath"), new Adler32()));// 计算校验方法:Adler32快一些,CRC32慢一些但更准确
zout.setComment("注释说明");
zout.write("content".getBytes());

//zip解压数据
ZipInputStream zin = new ZipInputStream(new CheckedInputStream(new FileInputStream("filename"), new Adler32()));
BufferedInputStream zins = new BufferedInputStream(zin);
ZipEntry zipEntry;
while ((zipEntry = zin.getNextEntry()) != null){
    int x ;
    while ((x = zins.read()) != -1)
        System.out.println(x);
}
//zip解压数据方式二
ZipFile zfile = new ZipFile("filepath");
Enumeration<? extends ZipEntry> zentries = zfile.entries();
while (zentries.hasMoreElements()){
    ZipEntry ze = zentries.nextElement();
}

 

注意:所有的流用完都要记得手动关闭!

参考:I/O系统主要的类

ByteArrayInputStream    允许将内存缓冲区当作InputStream使用,与FilterInputStream一起使用;
StringBufferInputStream 将String转换成InputStream(已弃用);
FileInputStream         从文件中读取信息,与FilterInputStream一起使用;
PipedInputStream        产生用于写入PipedOutputStream数据,实现“管道化”;
SequenceInputStream     将两个或多个InputStream对象转换成一个,与FilterInputStream一起使用;
FilterInputStream       抽象类,子类如下:
    DataInputStream     读取基本类型及String对象;
    BufferedInputStream 
    LineNumberInputStream   弃用;
    PushbackInputStream 

ByteArrayInputStream    在内存中创建缓存区,所以送往流的数据都存储在这里;
FileOutputStream        将信息写到文件里;
PipedOutputStream       写入管道;
FileOutputStream        抽象类,子类如下:
    DataOutputStream    
    PrintStream         
    BufferedOutputStream
posted @ 2021-03-25 13:22  覆手为云p  阅读(140)  评论(0编辑  收藏  举报
停止精灵球