zoukankan      html  css  js  c++  java
  • 基础知识:Java文件IO

    创建文件

       

    File file=new File("c:/test.txt");

    if (!file.exists()) {

    try {

    file.createNewFile();

    } catch (IOException e) {

    e.printStackTrace();

    }

       

    得到目录下文件名列表

       

    同样是file,传入目录的路径即可,得到名为dirFile类,利用dirisDirector方法即可判断是否为目录

       

    然后用filelist()方法得到目录下的所有文件名,利用dirgetpath获得目录路径,利用File类的separator方法得到分隔符,再加上文件名即可得到文件的全路径名

       

    写一个复制文件的程序

       

    主要用到FileInputStream以及FileOutputStream,用到前者的read方法和后者的write方法,另外还要用到一个byte数组用于存放读进来的数据

       

    FileInputStream fis=new FileInputStream("c:/test.txt");

    FileOutputStream fos=new FileOutputStream("c:/test_out.txt");

    byte[] buff=new byte[1024];

    int len=0;

    while ((len=fis.read(buff))>0) {

    fos.write(buff);

    }

    fis.close();

    fos.close();

       

    Stream

       

    根据数据的格式不同,可以分为字节流和字符流

       

    字节流的处理方式

       

    Java中的基础字节输入流和输出流是InputStreamOutputStream,通过它们衍生出FileInputStreamFileOutputStreamObjectInputStreamObjectOutputStreamBufferedInputStreamBufferedOutputStream;等

       

    字节流的一个最大的特点就是,每次的输入和输出都是一个字节,而计算机处理数据也总是以一个字节为基本单位的

       

    所以它应用在最原始的流的处理上,比如内存缓存操作,文件复制等不关心流的内容具体是什么

       

    但是在处理一些具体数据格式的时候,比如文本文件,就需要用到字符流,在这种情况下字符流的效率就要比字节流高很多

       

    字符流的处理方式

       

    字符流是由字节流包装而来,它的输入和输出流类型包括StringReaderStringWriter以及BufferedReaderBufferedWriter,对于前者的使用方法上来说根字节流的类似,还是用readwrite方法,而对于后者,多了一个针对文本数据的高效方法,那就是readline方法

       

    BufferedReader需要用到InputStreamReader作为输入,InputStreamReader将字节流转换为字符流,构造InputStreamReader对象时需要用到输入流InputStream以及字符编码作为参数

       

    InputStream iStream=new FileInputStream("c:/test.txt");

    InputStreamReader isReader=new InputStreamReader(iStream);

    BufferedReader bReader=new BufferedReader(isReader);

    String line=null;

    StringBuffer sBuffer=new StringBuffer();

    while ((line=bReader.readLine())!=null) {

    //System.out.println(line);

    sBuffer.append(line);

    }

    System.out.println(sBuffer);

    bReader.close();

       

       

    序列化和反序列化一个对象

       

    主要用到ObjectInputStreamObjectOutputStream,另外要序列化的类要继承Serializable接口

       

    class Student implements Serializable {

    private static final long serialVersionUID = 1L;

       

    然后在需要序列化和反序列化的地方用ObjectInputStream和ObjectOutputStream就可以了,注意输入输出的参数,还是要利用一个路径字符串创建一个字节流

       

    Student student = new Student("Tong", 23);

    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(

    "c:/objTest.dat"));

    oos.writeObject(student);

    oos.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream(

    "c:/objTest.dat"));

    Student student2 = (Student) ois.readObject();

    System.out.println(student2.toString());

      

  • 相关阅读:
    JS小白进阶之路(2)
    JS小白进阶之路(1)
    ajax.readyState与ajax.status一览
    Photoshop投影和CSS box-shadow转换
    layer弹层插件
    [Intervention] Ignored attempt to cancel a touchmove event with cancelable=false, for example because scrolling is in progress and cannot be interrupted
    css清除浮动影响
    css网页重置样式表(多版本)
    XSS攻击和CSRF攻击的定义及区别
    git的cd命令
  • 原文地址:https://www.cnblogs.com/keedor/p/4489444.html
Copyright © 2011-2022 走看看