raf部分API

package raf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * java.io.RandomAccessFile
 * RAF是专门用来读写文件数据的类,其基于指针对文件数据随机读写
 * @author ChenYi
 * @date 2019.08.26
 */
public class RAFDemo1 {
	public static void main(String[] args) throws IOException{
		/*
		 *常用构造器
		 *RandomAccessFile(String path,String mode); 
		 *RandomAssessFile(File file,String mode);
		 *
		 *mode为操作权限:
		 *r:只读模式
		 *rw:读写模式
		 */
		RandomAccessFile raf=new RandomAccessFile("./raf.98k","rw");
		/*
		 * void write(int a);
		 * 向文件中写入1字节,写入的是给定的int只对应的2进制的"低八位"
		 */
		raf.write(1);//0-255之间不会超过
		System.out.println("写出了");
		raf.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 从文件中读取字节
 * @author ChenYi
 * @date 2019.08.26
 */
public class RAFDemo2 {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile("./raf.98k", "r");
		/*
		 * int read();
		 * 从文件中读取一个字节,并以int型返回
		 * 若返回-1,则表示文件末尾
		 */
		int d=raf.read();
		System.out.println(d);
		d=raf.read();
		System.out.println(d);
		raf.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 使用RAF读写基本数据类型,以及基于指针的读写操作
 * @author ChenYi
 * @date 2019.08.27
 */
public class RAFDemo3 {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile("raf.dfg", "rw");
		/*
		 * 获取当前RAF的指针位置
		 * 创建RAF时默认从0开始,即:文件第一个字节的位置
		 */
		long pos =raf.getFilePointer();
		System.out.println("pos:"+pos);
		
		/*
		 * 将该int值写入文件
		 * 01111111 11111111 11111111 11111111
		 * 
		 * d>>>24
		 * 00000000 00000000 00000000 01111111 11111111 11111111 11111111
		 */
		int d=Integer.MAX_VALUE;
		raf.write(d>>>24);
		System.out.println("pos:"+raf.getFilePointer());
		raf.write(d>>>16);
		System.out.println("pos:"+raf.getFilePointer());
		raf.write(d>>>8);
		System.out.println("pos:"+raf.getFilePointer());
		raf.write(d);
		System.out.println("pos:"+raf.getFilePointer());
		/*
		 * RAF提供了方便写出基本类型的方法
		 */
		raf.writeInt(d);//相当于上面四句,连续将4个字节的int值写出
		System.out.println("pos:"+raf.getFilePointer());
		raf.writeLong(123l);//连续写8个字节将long类型写出
		System.out.println("pos:"+raf.getFilePointer());
		raf.writeDouble(123.123);
		System.out.println("pos:"+raf.getFilePointer());
		/*
		 * void seek(long pos)
		 * 移动到指定位置
		 */
		raf.seek(0);
		System.out.println("pos:"+raf.getFilePointer());
		
		int num=raf.readInt();
		System.out.println(num);
		System.out.println("pos:"+raf.getFilePointer());
		
		//读取long值
		raf.seek(8);
		long l=raf.readLong();
		System.out.println(l);
		System.out.println("pos:"+raf.getFilePointer());
		
		//读取double值
		raf.seek(16);
		double dou=raf.readDouble();
		System.out.println(dou);
		System.out.println("pos:"+raf.getFilePointer());
		
		System.out.println("写完了");
		raf.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 读取文本数据
 * @author ChenYi
 * @date 2019.08.27
 */
public class ReadStringDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile("./raf.txt", "r");//默认有./
		
		byte[ ] data=new byte[(int)raf.length()];
		raf.read(data);
		/*
		 * String提供了将字节还原为字符串的构造方法:
		 * String(byte[] data,String csn)
		 * 将给定的字节数组按照指定的字符集还原为字符串
		 */
		String str=new String(data,"utf-8");
		System.out.println(str);
		raf.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 写出文本数据
 * @author ChenYi
 * @date 2019.08.27
 */
public class WriteStringDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile("raf.txt", "rw");
		String line="hi,hello,good bay,sunning发货价防寒无房户吴爱华分段未婚夫安稳办法阿胃复安无纺布";
		/*
		 * String提供了转换为字节的方法:
		 * byte[] getByte(String csn);
		 * 将当前字符串按照指定的字符集转换为字节
		 */
		byte[] data=line.getBytes("utf-8");
		raf.write(data);
		System.out.println("写完了");
		raf.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 修改用户昵称
 * 程序启动后,要求用户输入用户名及新昵称,然后修改注册文件中对应的该用户的昵称
 * 若此用户不存在,则提示查无此人
 * @author ChenYi
 * @date 2019.08.27
 */
public class UpdateDemo {
	public static void main(String[] args) throws IOException {
		Scanner scan=new Scanner(System.in);
		RandomAccessFile raf=new RandomAccessFile("zhuce.txt", "rw");
		System.out.println("请输入您的姓名");
		String username=scan.nextLine();
		boolean update=false;
		for (int i = 0; i <raf.length()/100; i++) {
			raf.seek(i*100);
			byte[] data=new byte[32];
			raf.read(data);
			String name=new String(data,"utf-8").trim();
			if (username.equals(name)) {
				System.out.println("请输入您的新昵称");
				String newnickname=scan.nextLine();
				raf.seek(100*i+64);
				data=newnickname.getBytes("utf-8");
				data=Arrays.copyOf(data, 32);
				raf.write(data);
				System.out.println("修改成功");
				update=true;
				break;
			}
		}
		if (!update) {
			System.out.println("查无此人");
		}
		System.out.println("运行完毕");
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 将注册的所有用户信息读取出来
 * @author ChenYi
 * @date 2019.08.27
 */
public class ShowAllUserDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile("zhuce.txt", "r");
		for (int i = 0; i < raf.length()/100; i++) {
			
			byte[] data=new byte[32];
			//读取姓名
			raf.read(data);
			String username=new String(data,"utf-8").trim();
			System.out.println("姓名:"+username);
			//读取密码
			raf.read(data);
			String password=new String(data,"utf-8").trim();
			System.out.println("密码:"+password);
			//读取昵称
			raf.read(data);
			String nickname=new String(data,"utf-8").trim();
			System.out.println("昵称:"+nickname);
			//读取年龄
			int age=raf.readInt();
			System.out.println("年龄:"+age);
			System.out.println("pos:"+raf.getFilePointer()+"\n");
		}
		System.out.println("没了!");
		raf.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 读取文本数据
 * @author ChenYi
 * @date 2019.08.27
 */
public class ReadStringDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf=new RandomAccessFile("./raf.txt", "r");//默认有./
		
		byte[ ] data=new byte[(int)raf.length()];
		raf.read(data);
		/*
		 * String提供了将字节还原为字符串的构造方法:
		 * String(byte[] data,String csn)
		 * 将给定的字节数组按照指定的字符集还原为字符串
		 */
		String str=new String(data,"utf-8");
		System.out.println(str);
		raf.close();
	}
}
package raf;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.Scanner;

/**
 * 用户注册
 * 
 * 用户在控制台输入四项信息:
 * 用户名,密码,昵称,年龄
 * 前三个为字符串,最后一个为int值
 * 并将该信息写入user.dat文件中保存
 * 
 * 每条记录占用100字节,其中用户名,密码,昵称各占32字节,年龄是int值固定4字节
 * 字符串故意留白的好处是便于修改,并且固定长度格式也就固定了,读取方便.但是缺点是会浪费一部分空间
 * @author ChenYi
 * @date 2019.08.27
 */
public class RegDemo {
	public static void main(String[] args) throws IOException {
		System.out.println("欢迎注册!");
		
		Scanner scan=new Scanner(System.in);
		
		System.out.println("请输入用户名:");
		String username=scan.nextLine();
		
		System.out.println("请输入密码:");
		String password=scan.nextLine();
		
		System.out.println("请输入昵称:");
		String nickname=scan.nextLine();
		
		System.out.println("请输入年龄:");
		int age=scan.nextInt();
		
		System.out.println(username+","+password+","+nickname+","+age);
		
		RandomAccessFile raf=new RandomAccessFile("zhuce.txt", "rw");
		//先将指针移动到文件末尾,以便追加新纪录
		raf.seek(raf.length());
		
		//写用户名
		byte[] data=username.getBytes("utf-8");
		//扩容
		data=Arrays.copyOf(data, 32);
		raf.write(data);
		
		//密码
		data=password.getBytes("utf-8");
		data=Arrays.copyOf(data, 32);
		raf.write(data);
		
		//昵称
		data=nickname.getBytes("utf-8");
		data=Arrays.copyOf(data, 32);
		raf.write(data);
		
		//年龄
		raf.writeInt(age);
		System.out.println("pos:"+raf.getFilePointer());
		raf.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 复制文件
 * @author ChenYi
 * @date 2019.08.27
 */
public class CopyDemo {
	public static void main(String[] args) throws IOException {
		RandomAccessFile src=new RandomAccessFile("ad.jpeg", "r");
		RandomAccessFile desc=new RandomAccessFile("ad1.jpeg","rw");
		int d=-1;
		long start=System.currentTimeMillis();
		while ((d=src.read())!=-1) {
			desc.write(d);
		}
		long end=System.currentTimeMillis();
		System.out.println("复制完毕!耗时:"+(end-start));
		src.close();
		desc.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 通过提高每次实际读写的数据量,减少实际读写的次数可以提高读写的效率
 * 
 * 单字节读写是随机读写
 * 一组字节一组字节读写是块读写
 * @author ChenYi
 * @date 2019.08.27
 */
public class CopyDemo2 {
	public static void main(String[] args) throws IOException  {
		RandomAccessFile src=new RandomAccessFile("卡布奇诺.mp3", "r");
		RandomAccessFile desc=new RandomAccessFile("卡布奇诺2.mp3", "rw");
		/*
		 * int read(byte[] data)
		 * 一次性读取给定字节数组长度的字节量并存入数组,返回值为本次实际读取到的字节量,
		 * 若返回值是-1,则表示本次读取时已经是文件末尾
		 * 
		 * void write(byte[] data,int i,int len);
		 * 将给定数组中从下标i处的连续len个字节一次性写出
		 */
		int len=-1;//每次实际读取的字节量
		byte [ ] data=new byte[1024*10];//10k
		
		long start=System.currentTimeMillis();
		while ((len=src.read(data))!=-1) {
			desc.write(data,0,len);
		}
		long end=System.currentTimeMillis();
		System.out.println("复制成功!耗时:"+(end-start));
		src.close();
		desc.close();
	}
}
package raf;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
/**
 * 练习:完成简易记事本工具
 * 程序启动后,要求用户在控制台输入文件名,
 * 然后对该文件进行操作.
 * 之后用户在控制台输入的每一行字都要写入改文件中(字符集统一用utf-8)
 * 当用户单独输入exit时程序退出
 * @author ChenYi
 * @date 2019.08.27
 */
public class Note {
	public static void main(String[] args) throws IOException {
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入您想打开的文件名");
		String name=scan.next();
		RandomAccessFile raf=new RandomAccessFile(name, "rw");
		System.out.println("请输入您想输入的文字:");
		while (true){
			String line=scan.nextLine();
			if ("exit".equals(line)) {
				System.out.println("写完了");
				raf.close();
				break;
			}
//			raf.write(line.getBytes("utf-8"));
			byte[] data=line.getBytes("utf-8");
			raf.write(data);
		}
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值