java中的IO流

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;
 }
 
}

 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值