码迷,mamicode.com
首页 > 编程语言 > 详细

java基础-JAVA IO

时间:2017-03-19 14:01:56      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:dom   dex   inpu   system   文件读取   adl   打开   ted   cep   

首先JAVA IO为什么不叫文件而叫IO? IO其实是对文件或者网路的抽象,抽象成“流”,“流”是一维的,并且是hi单向流动的:从文件中读数据是流、向文件中写数据也是流、从网络中读数据是流、向网络中写数据也是流等等。

按数据走向来分可以分为输入流,输出流

按处理的数据类型可以分为字符流和字节流

首先要区分JAVA IO针对的对象, 把自己比作一个程序,向程序写数据就是Input操作,从程序向外输出数据就是Output操作

JAVA IO大致分为字节流和字符流。字节流:数据时二进制,人不可读,有InputStream, OutputStream接口。 字符流:数据是基于字符的,认识可读的,有Reader,Writer接口。

各种流的继承关系:

技术分享

反正我第一次看见的时候很头疼,这么多类压根分不清,弄不明白。但是常用的有FileReader, BufferedReader, FileWriter,FileInputStream, FileOutputStream。具体用的时候可以参考API文档

下面结合实际,写一下代码看看具体的接口是如何使用的

File f = new File("src/Java/io/ReadMySelf.java");
System.out.println(f.exists()); //判断当前文件是否存在
System.out.println(f.getAbsolutePath()); //得到当前文件的绝对路径 F:\JavaLearning\src\Java\io\ReadMySelf.java

诸如此类的操作还有f.canRead(), f.canWrite(), f.canExcute(),f.creatNewFile()会判断当前文件是否存在,若不存在会生成新的文件

File[] files = f.listFiles();  //对files进行遍历可以的到当前文件下的所有文件目录

 

接下来展示用字符流来读取文件:

public class ReadMySelf {
    public static void main(String[] args) throws Exception {
      File f = new File("src/Java/io/ReadMySelf.java");
      FileReader fr = null;
      try {
          fr = new FileReader(f);
          char[] buffer = new char[10];  
          int length = -1;
          while((length = fr.read(buffer)) != -1) {  //每次读取10个字节
          if(length == 10) {
              System.out.print(buffer);
          }else {
              System.out.println(new String(buffer, 0, length));
          }
        }
      } catch(Exception e) {
          e.printStackTrace();
      } finally {
          if(fr != null) {
            fr.close();
          }
      }
}

也可以使用BufferedReader逐行读取

public class ReadMySelf {
    public static void main(String[] args) throws Exception {
    File f = new File("src/Java/io/ReadMySelf.java");
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(f));
        String line = null;
        while((line = br.readLine()) != null) {
        System.out.println(line);
        }
    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        if(br != null) {
        br.close();
        }
    }
}  

使用StringWriter/FileWriter写数据,这里采用StringWriter

public class ReadMySelf {
    public static void main(String[] args) throws Exception {
    File f = new File("src/Java/io/ReadMySelf.java");
    StringWriter sw = new StringWriter();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(f));
        String line = null;
        while((line = br.readLine()) != null) {
        sw.write(line);
        sw.write("\n");
        }
        System.out.println(sw.toString());
    } catch(Exception e) {
        e.printStackTrace();
    } finally { 
        if(br != null) {
        br.close();
        }
    }
}

下面才采用字节流来读取一个图片,并写入本地程序文件夹下: 

public class ReadMySelf {
    public static void main(String[] args) throws Exception {
    
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        byte[] buffer = new byte[1024];
        File f = new File("F:/JavaLearning/bd_logo.png");
        System.out.println(f.exists());
        bis = new BufferedInputStream(new FileInputStream("F:/JavaLearning/bd_logo.png"));
        bos = new BufferedOutputStream(new FileOutputStream("src/Java/io/a.png"));
       
        int length = -1; 
        while((length = bis.read(buffer)) != -1) {
          bos.write(buffer, 0, length);
        }
        bos.flush();
    } catch(Exception e) {
        
    } finally {
        bis.close();
        bos.close();
    }
}

以上都是顺序读写文件,还可以使用RandomAccessFile随机读写文件,有两种模式“”rw“” “”r“”(只读)
RandomAccessFile raf=new RandomAccessFile (file,"rw")
内部包含一个文件指针,打开文件时指针在开头pointer=0;读写操作时指针会往后移动,使用seek(int index); 可以操作指针

public class ReadMySelf {
    public static void main(String[] args) throws Exception {
    
    RandomAccessFile file = new RandomAccessFile("src/Java/io/readme.txt", "rw");//若文件不存在会在本地创建
    file.writeInt(20);  //占4个字节
    file.writeDouble(3.1415926);    //占8个字节
    file.writeUTF("这是一个UTF字符串");     //这个长度写在当前文件指针的前两个字节出,可以用readShort()读取
    file.writeBoolean(true); //占1个字节
    file.writeShort(155); //占2个字节
    file.writeLong(1563231521); //占8个字节
    file.writeUTF("又是一个UTF字符串");    
    file.writeFloat(35.12f);  //占4个字节
    file.writeChar(‘a‘); //占2个字节
    
    file.seek(0);   //把文件指针设置到文件起始处
    //以下从file文件读取数据,要注意指针位置
    System.out.println("-----------从file文件指定位置读取数据--------");
    System.out.println(file.readInt());
    System.out.println(file.readDouble());
    System.out.println(file.readUTF());
    
    file.skipBytes(3); //文件指针跳过三个字节
    System.out.println(file.readLong());
}

以上是学JAVA IO时自己做的笔记,还有很多不足,自己还需努力。

 


 

java基础-JAVA IO

标签:dom   dex   inpu   system   文件读取   adl   打开   ted   cep   

原文地址:http://www.cnblogs.com/HarryHook/p/6579729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!