您的位置:首页 > 编程语言 > Java开发

java中的IO流

2016-05-12 18:35 405 查看
1、内存流

   a、ByteArrayOutputStream

   b、ByteArrayInputStream

  2、对象流

   a、ObjectOutputStream

   b、ObjectInputStream

   所有的对象流操作的对象必须实现了序列化接口。

  3、随机读写

   a、RandomAccessFile

    a、seek(long pos):指针将停留在最后一次访问的位置上,seek方法,

    是将指针移动到初始位置。

    b、getFilePointer:返回文件当前指针的所在位置。
4、File类

   a、可读/可写/执行

   b、创建文件

   c、删除文件或者目录的操作

   d、虚拟机终止时,删除文件或目录

   e、判断文件或者文件目录是否存在

   f、得到一个文件或目录的绝对路径(可以返回File或String)

   g、得到文件的文件名

   h、返回当前文件的父目录

   i、判断一个目录是否为绝对路径

   j、判断抽象路径是否为一个文件夹

   k、判断是否为一个文件

   l、判断该文件是否为一个隐藏文件

   m、查看这个文件最后一次修改的时间

   n、返回一个目录下的文件以及文件目录

   o、返回文件长度1kb = 1024byte  1m = 1024 kb  1g = 1024M 1T = 1024G

   p、创建文件目录

   q、创建制定文件目录,如果中间目录不存在,则依次创建

   r、返回一个URI

   URI URL

   总结:URL是URI的子集。

  5、IO概念种类:

   a、基于字节的操作

   b、基于字符的操作

   c、基于磁盘的操作:File

   d、基于网络的操作:Socket

 

package main;

import java.io.File;

public class CoreJavaDay19 {

 public static void main(String[] args) {

//  IOUtils.ramStream();

//  File file = new File("C:\\Users\\Administrator\\Desktop\\Z.JJ");

//  Message message = new Message();

//  message.setName("隔壁老王");

//  message.setContent("今晚在家吗?");

//  message.setDate("2016-4-25 11:03");

//  IOUtils.objectWriteStream(file, message);

//  

//  Message message2 = IOUtils.objectReadStream(file);

//  System.out.println(message2.toString());

  

  File file = new File("C:\\Users\\Administrator\\Desktop\\DEMO.txt");

//  IOUtils.randomRead(file, 20);

//  IOUtils.randomWrite(file, "#", 3);

  IOUtils.insertByRandomAccessFile(file, 1, "###");

 }

}

 

package main;

import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.RandomAccessFile;

public class IOUtils {

 /**

  * 内存流

  */

 public static void ramStream(){

  String string = "helloworld";

  ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(string.getBytes());

  ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

  

  int hasRead = 0;

  

  while((hasRead = byteArrayInputStream.read()) != -1){

   char c = (char)hasRead;

   byteArrayOutputStream.write(Character.toUpperCase(c));

  }

//  System.out.println(byteArrayOutputStream.toString());

  String result = new String(byteArrayOutputStream.toByteArray(), 0, string.length());

  System.out.println(result);

 }

 

 /**

  * 对象流 写入操作

  */

 public static void objectWriteStream(File file, Message message){

  ObjectOutputStream objectOutputStream = null;

  try {

   objectOutputStream = new ObjectOutputStream(new FileOutputStream(file, false));

   objectOutputStream.writeObject(message);

   objectOutputStream.flush();

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }finally{

   if(objectOutputStream != null){

    try {

     objectOutputStream.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

   }

  }

 }

 

 /**

  * 对象流读取操作

  * @param file

  * @return

  */

 public static Message objectReadStream(File file){

  ObjectInputStream objectInputStream = null;

  Message message = null;

  

  try {

   objectInputStream = new ObjectInputStream(new FileInputStream(file));

   message = (Message) objectInputStream.readObject();

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  } catch (IOException e) {

   e.printStackTrace();

  }catch (ClassNotFoundException e) {

   e.printStackTrace();

  }finally{

   if(objectInputStream != null){

    try {

     objectInputStream.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

   }

  }

  return message;

 }

 

 /**

  * RandomAccessFile

  */

 

 public static void randomRead(File file, int pointer){

  /*

   * r代表以可读方式打开

   * rw代表可读可写方式打开

   */

  RandomAccessFile randomAccessFile = null;

  try {

   randomAccessFile = new RandomAccessFile(file, "r");

   System.out.println("当前指针的位置是:" + randomAccessFile.getFilePointer());

   randomAccessFile.seek(pointer);

   byte[] bytes = new byte[1024];

   int hasRead = 0;

   

   while((hasRead = randomAccessFile.read(bytes)) != -1){

    System.out.println(new String(bytes, 0, hasRead));

   }

   

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  }catch(IOException e){

   e.printStackTrace();

  }finally{

   if(randomAccessFile != null){

    try {

     randomAccessFile.close();

    } catch (IOException e) {

     e.printStackTrace();

    }

   }

  }

 }

 

 /**

  * 写文件

  */

 public static void randomWrite(File file, String appendContent,long seek){

  RandomAccessFile randomAccessFile = null;

  try {

   randomAccessFile = new RandomAccessFile(file, "rw");

   randomAccessFile.seek(randomAccessFile.length());

   randomAccessFile.write(appendContent.getBytes());

  } catch (FileNotFoundException e) {

   e.printStackTrace();

  }catch (IOException e) {

   e.printStackTrace();

  }

 }

 

 /**

  * 随机插入

  */

 public static void insertByRandomAccessFile(File file, long pointer, String insertContent){

  try {

   File tempFile = File.createTempFile("temp", null);

   tempFile.deleteOnExit();

   

   RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");

   

   FileOutputStream fileOutputStream = new FileOutputStream(tempFile);

   FileInputStream fileInputStream = new FileInputStream(tempFile);

   

   randomAccessFile.seek(pointer);

   System.out.println(randomAccessFile.getFilePointer());

   byte[] bytes = new byte[1024];

   int hasRead = 0;

   while((hasRead = randomAccessFile.read(bytes)) != -1){

    fileOutputStream.write(bytes, 0, hasRead);

   }

   randomAccessFile.seek(pointer);

   System.out.println(randomAccessFile.getFilePointer());

   fileOutputStream.flush();

   randomAccessFile.write(insertContent.getBytes());

   System.out.println(randomAccessFile.getFilePointer());

   while((hasRead = fileInputStream.read(bytes)) != -1){

    randomAccessFile.write(bytes, 0, hasRead);

   }

   

   randomAccessFile.close();

   fileOutputStream.close();

   fileInputStream.close();

  } catch (IOException e) {

   e.printStackTrace();

  }

 }

 

}

package main;

import java.io.Serializable;

public class Message implements Serializable{

 private static final long serialVersionUID = 1L;

 

 private String name;

 private String content;

 private String date;

 

 public String getName() {

  return name;

 }

 public void setName(String name) {

  this.name = name;

 }

 public String getContent() {

  return content;

 }

 public void setContent(String content) {

  this.content = content;

 }

 public String getDate() {

  return date;

 }

 public void setDate(String date) {

  this.date = date;

 }

 @Override

 public String toString() {

  return "发送消息者:" + name + " 消息内容:" + content + " 发送时间:" + date;

 }

 

}

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  io流 内存 URI URL File