package raf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RAFDemo1 {
public static void main(String[] args) throws IOException{
RandomAccessFile raf=new RandomAccessFile("./raf.98k","rw");
raf.write(1);
System.out.println("写出了");
raf.close();
}
}
package raf;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RAFDemo2 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("./raf.98k", "r");
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;
public class RAFDemo3 {
public static void main(String[] args) throws IOException {
RandomAccessFile raf=new RandomAccessFile("raf.dfg", "rw");
long pos =raf.getFilePointer();
System.out.println("pos:"+pos);
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.writeInt(d);
System.out.println("pos:"+raf.getFilePointer());
raf.writeLong(123l);
System.out.println("pos:"+raf.getFilePointer());
raf.writeDouble(123.123);
System.out.println("pos:"+raf.getFilePointer());
raf.seek(0);
System.out.println("pos:"+raf.getFilePointer());
int num=raf.readInt();
System.out.println(num);
System.out.println("pos:"+raf.getFilePointer());
raf.seek(8);
long l=raf.readLong();
System.out.println(l);
System.out.println("pos:"+raf.getFilePointer());
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;
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 str=new String(data,"utf-8");
System.out.println(str);
raf.close();
}
}
package raf;
import java.io.IOException;
import java.io.RandomAccessFile;
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发货价防寒无房户吴爱华分段未婚夫安稳办法阿胃复安无纺布";
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;
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;
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;
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 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;
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;
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;
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 len=-1;
byte [ ] data=new byte[1024*10];
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;
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;
}
byte[] data=line.getBytes("utf-8");
raf.write(data);
}
}
}