JavaSE03

14 篇文章 0 订阅
import java.io.File;
import java.io.IOException;

/**
 * java.io.File
 * File的每一个实例用于表示硬盘上的一个文件或者目录。
 * 
 * 使用File可以:
 * (1)访问其表示的文件或目录的属性信息(名字,大小等)
 * (2)操作文件或目录(创建,删除)
 * (3)访问一个目录中的所有子项,但是不能访问文件数据
 */
public class CreateFileDemo {
	public static void main(String[] args) throws IOException {
		/*
		 * 在表示文件或目录时指定的路径尽量不要使用绝对路径,因为操作系统格式不一致
		 * 
		 * 相对路径最常用,但是相对路径相对哪里要看程序在哪个环境上运行,
		 * 运行环境决定着相对路径
		 * 
		 * 在eclipse中运行程序时,“当前目录”指定该类所处的项目目录用“./”表示
		 * 在这里./可以省略不写
		 */
		/*在当前目录中新建一个文件demo.txt
		 * file.createNewFile()方法创建文件
		 * 
		 */
		File file = new File("D:/demo.txt");
		
		/*
		 * boolean exists()该方法适用于判断当前File表示的文件或目录是否存在
		 * 存在则返回true
		 */
		if(!file.exists()){
			file.createNewFile();
			System.out.println("文件已创建!");
		} else {
			System.out.println("文件已存在!");
		}
		
	}
}

import java.io.File;

/**
 * 使用File创建目录
 * @author 臻冉
 *
 */
public class CreateMkdirDemo {
	public static void main(String[] args) {
		/*
		 * File提供了mkdir()方法,创建目录
		 */
		File file = new File("./demo");
		if(!file.exists()){
			file.mkdir();
			System.out.println("目录已创建!");
		} else {
			System.out.println("目录已存在!");
		}
	}
}

import java.io.File;

/**
 * 使用File创建多个目录
 */
public class CreateMkDirsDemo {
	public static void main(String[] args) {
		/*
		 * File提供了一个Mkdirs()可创建多个目录
		 */
		File file = new File("./a/b/c/d/e");
		if(!file.exists()){
			file.mkdirs();
			System.out.println("多个目录创建完毕");
		} else {
			System.out.println("目录已存在!");
		}
		
	}
}

import java.io.File;

public class DeleteFileDemo {
	public static void main(String[] args) {
		/*
		 * 将当前文件中的demo.txt文件删除
		 */
		File file = new File("./demo.txt");
		if(!file.exists()){
			System.err.println("文件不存在");
		} else {
			file.delete();
			System.out.println("删除完毕!");
		}
	}
}

import java.io.File;

/**
 * 使用File删除目录
 * @author 臻冉
 *
 */
public class DeleteMkdirDemo {
	public static void main(String[] args) {
		File file = new File("./demo");
		if(file.exists()){
			file.delete();
			System.out.println("目录删除完毕!");
		} else {
			System.out.println("目录不存在!");
		}
		
		
		/*
		 *删除目录有一个前提,
		 *即该目录必须 是一个空白目录才可以删除
		 *因此以下方式不可取
		 */
		/*File file = new File("./a/b/c/d");
		if(file.exists()){
			file.delete();
			System.out.println("目录删除完毕!");
		} else {
			System.out.println("目录不存在!");
		}*/
	}
}

/**
 * 删除多个目录
 * 利用"递归"删除
 */
public class DeleteMkDirsDemo {
	public static void main(String[] args) {
		//删除多个目录
		File file =new File("./a");
		fileDelete(file);
	}

	private static void fileDelete(File file) {
		File[] files = file.listFiles();
		for(int i=0;i<files.length;i++){
			if(files[i].isFile()){//判断是不是文件,如果是文件则删除
				files[i].delete();
				System.out.println("删除的文件是:"+files[i].getName());
			}else if(files[i].isDirectory()){
				fileDelete(files[i]);
			}
		}
		file.delete();//删除空白的目录
		System.out.println("删除完毕!");
		
	}
}


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

public class demo {
	 public static void main(String[] args) throws Exception {
	  Scanner scanner=new Scanner(System.in);
	  System.out.println("请输入用户名");
	  String userName=scanner.next();
	  System.out.println("请输入用户密码");
	  String uersPwd=scanner.next();
	  RandomAccessFile raf=new RandomAccessFile("user.dat","rw");
	  
	  byte[] data=new byte[32];
	  boolean flagName=false;//账号不存在
	  boolean flagPwd=false;//密码错误
	  for(int i=0;i<raf.length()/100 ; i++){
	   //设置指针的位置
	   raf.seek(i*100);
	   raf.read(data);
	   //System.out.println("名字:"+new String (data,"UTF-8"));
	   String nameData=new String(data,"UTF-8").trim();
	   if(nameData.equals(userName)){//校验用户输入的名字和文本
	    //如果账号存在则比较密码
	    raf.read(data);
	    String pwdData=new String(data,"UFT-8").trim();
	    flagName=true;
	    if(pwdData.equals(uersPwd)){//校验用户输入的密码和文本提取出来的用户密码
	     //登陆成功
	     
	     flagPwd=true;
	     System.out.println("登陆成功");
	     //查询该用户的基本信息
	     raf.read(data);
	     
	     String nickData=new String (data,"UFT-8").trim();//读取此用户的昵称
	     int ageData=raf.readInt();//读取此用户的年龄
	     System.out.println("个人信息: 姓名"+nameData+",密码"+pwdData+",昵称"+nickData+",年龄"+ageData);
	    }
	    
	    //System.out.println("账号存在");
	    
	   }
	   
	   //提示错误信息
	   if(!flagName){
	    System.out.println("账号不存在");
	    return;
	   }
	   if(!flagPwd){
	    System.out.println("密码错误!");
	    
	   }
	  }
	 }
	 
}

/**
 * FileFilter是一个接口,用于路径下的文件名/目录过滤器
 * @author 臻冉
 *
 */
public class DemoFileFiter {
	public static void main(String[] args) {
		File file = new File("D:/");
		File[] files = file.listFiles(new FileFilter(){
			public boolean accept(File pathname){
				System.out.println("正在过滤"+pathname.getName());
				return pathname.getName().endsWith(".zip");
			}
		});
		for(int i=0;i<files.length;i++){
			System.out.println("过滤后的文件是:"+files[i]);
		}
	}
}


/**
 * 通过File获取文件信息
 * @author 臻冉
 *
 */
public class FileDemo02 {
	public static void main(String[] args) {
		File file = new File("./demo.txt");
		//获取名字,File类提供了getName()获取文件或者目录名字
		String name = file.getName();
		System.out.println(name);
		//获取文件字节长度,File类提供了length()获取文件数据或者目录的长度
		long length = file.length();
		System.out.println(length);
		
		//是否可读可写
		boolean cr = file.canRead();
		boolean cw = file.canWrite();
		System.out.println("可读"+cr);
		System.out.println("可写"+cw);
		
		//是否为隐藏文件
		boolean h = file.isHidden();
		System.out.println(h);
		
	}
}


/**
 * 获取一个目录中的所有子项
 */
public class ListFilesDemo {
	public static void main(String[] args) {
		/*
		 * 在File中提供了ListFiles()方法返回File数组:可以获取当前目录中的所有子项
		 * “.”在当前目录
		 */
		File file = new File(".");//.指的是当前目录
		File[] dirs = file.listFiles();
		System.out.println(dirs.length);
		for(int i=0;i<dirs.length;i++){
			/**
			 *  boolean isFile()判断当前File表示的是否为文件
			 *  
			 *  boolean isDirectory()判断当前File表示的是否为目录
			 */
			
			if(dirs[i].isFile()){
				System.out.println("文件:"+dirs[i].getName());
			} else if(dirs[i].isDirectory()){
				System.out.println("目录:"+dirs[i].getName());
			}
			
		}
		
	}
	
}

/**
 * JDK1.5之后推出一个新的特性,for  each
 * 新循环并取去带传统的for循环工作,它用来遍历
 * 集合或者数组使用
 * 
 * 
 * 增强for循环
 * @author 臻冉
 *
 */
public class NewForDemo {
	public static void main(String[] args) {
		int[] arr = {1,2,3,4,5,6};
		for(int i=0;i<arr.length;i++){
			System.out.print(arr[i]);
		}
		System.out.println();
		/*
		 * 利用增强for循环写
		 * for(遍历后的类型 存储每个元素的变量:数组或者集合){}
		 */
		for(int a:arr){//把arr中所有的元素循环
			System.out.print(a);
		}
		
	}
}

/**
 * 利用RAF对MP3进行复制
 * @author 臻冉
 *
 */
public class RAF_CopyMP3Demo {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf_read = new RandomAccessFile("C:/Users/臻冉/Desktop/韩甜甜 - 你的答案 (现场)(片段).mp3","r");
		RandomAccessFile raf_write = new RandomAccessFile("C:/Users/臻冉/Desktop/韩甜甜 - 你的答案 (现场)(复制).mp3","rw");
		int len =-1;
		long time1 = System.currentTimeMillis();//获取当前时间的毫秒数
		System.out.println("正在复制,请等待……");
		while((len=raf_read.read())!=-1){
			raf_write.write(len);
		}
		long time2 = System.currentTimeMillis();
		System.out.println("复制完毕消耗时间是"+(time2-time1));
		System.out.println("复制完毕!");
		raf_read.close();
		raf_write.close();
	}
}

/**
 * 由于硬盘的物理性,导致随机读写的效率很低,因此我们推荐"块读写";
 * 块读写的效率还是很不错的,
 * 随机速写:但自己读写数据
 * 块读写:一次一组字节读写数据
 * 所有若想提高读写效率我们需要提高每次读写的数据量,减少
 * 实际读写次数来达到优化的性能
 * 
 * 1byte			8位2进制
 * 1kb				1024byte
 * 1mb				1024kb
 * 1gb				1024mb
 * 
 * @author 臻冉
 *
 */
public class RAF_CopyMP3Demo02 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf_read = new RandomAccessFile("C:/Users/臻冉/Desktop/韩甜甜 - 你的答案 (现场)(片段).mp3","r");
		RandomAccessFile raf_write = new RandomAccessFile("C:/Users/臻冉/Desktop/韩甜甜 - 你的答案 (现场)(复制).mp3","rw");
		byte[] bytes = new byte[1024*10];
		int len = -1;
		System.out.println("正在复制……");
		long time1 = System.currentTimeMillis();
		while((len=raf_read.read(bytes))!=-1){
			raf_write.write(bytes,0,len);//每次写入byte个字节从0开始到len个
		}
		long time2 = System.currentTimeMillis();
		System.out.println("块复制后消耗的时间"+(time2-time1));
		raf_write.close();
		raf_read.close();
	}
}


/**
 * 从文件中读取字节
 * @author 臻冉
 *
 */
public class RAF_read {
	public static void main(String[] args) throws IOException {
		RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
		/*
		 * RandomAccessFile提供了int read();
		 * 1.从文件中读取一个字节并以int形式返回
		 * 2若返回值为-1则表示读取到了文件末尾
		 */
		int d = raf.read();
		System.out.println(d);
		d = raf.read();
		System.out.println(d);//表示读到末尾
		raf.close();
	}
}


public class RAF_readDemo01 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf01.txt", "rw");
		
		int len = -1;//保存读取到的每一个字节数据
		while((len=raf.read())!=-1){//数据读取-1时则读到文件的末尾
			System.out.println((char)len);
		}
	}
}


/**
 * 利用RAF读取中文数据
 * @author 臻冉
 *
 */
public class RAF_readDemo02 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf02.txt","r");
		byte[] b = new byte[100];
		int len = raf.read(b);//读取100个字节并存放在b字节数组中,返回的是实际读到的字节数量
		/**
		 * 读取出来的字节要转换为字符串
		 */
		/*
		String str = new String(b,"UTF-8");
		System.out.println(str);
		System.out.println("实际读到的字节量:"+len);
		*/
		/*
		 * String(byte[] data,int offset,int len,String csn)
		 * 将给定的字节数中从下表offset处连续读取len个
		 */
		String str = new String(b, 0,len,"UTF-8");
		System.out.println(str);
		raf.close();//关闭资源
	}
}


/**
 * RAF提供了一个方法字节跳过skipBytes(int n);
 * n:跳过n个字节
 * @author 臻冉
 *
 */
public class RAF_skipByteDemo {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf01.txt", "r");
		//读取world
		byte[] b = new byte[5];
		raf.skipBytes(5);
		int len = raf.read(b);
		String str = new String(b,0,len,"UTF-8");
		System.out.println(str);
		raf.close();
	}
}


/**
 * 用户登录,并显示此用户的所有信息
 * @author 臻冉
 *
 */
public class RAF_userloginDemo {
	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入用户名:");
		String userName = scanner.next();
		System.out.println("请输入用户密码:");
		String userPwd = scanner.next();
		RandomAccessFile raf = new RandomAccessFile("user.dat", "rw");
		byte[] data = new byte[32];
		boolean flagName = false;//账号不存在
		boolean flagPwd = false;//密码错误
		for(int i=0;i<raf.length()/100;i++){
			//设置指针为
			raf.seek(i*100);
			raf.read(data);
			String nameData = new String(data,"UTF-8").trim();
			if(nameData.equals(userName)){		//校验用户输入的名字和文本提取出来的用户名字
				//如果账号存在则比较密码
				raf.read(data);
				String pwdData = new String(data,"UTF-8").trim();
				flagName=true;
				if(pwdData.equals(userPwd)){//校验用户输入的密码和文本提取出来的用户名字
					//登陆成功
					flagPwd = true;
					System.out.println("登录成功");
					//查询该用户的所有信息
					raf.read(data);
					//读取此用户的昵称
					String nickData = new String(data,"UTF-8").trim();
					int ageData = raf.readInt();//读取此用户的年龄
					System.out.println("个人信息:"+nameData+","+pwdData+","+nickData+","+ageData);
				}
			}
		}
		//提示错误信息
		if(!flagName){
			System.out.println("账号不存在");
			return;
		}
		if(!flagPwd){
			System.out.println("密码错误!");
			return ;
		}
		raf.close();
		scanner.close();
	}
}


/**
 * 读取用户所有数据
 * @author 臻冉
 *
 */
public class RAF_userReadDemo {
	public static void main(String[] args) throws Exception {
		/*
		 * 将读取出来的用户信息输出,格式我name,pwd,nick,age
		 */
		
		RandomAccessFile raf = new RandomAccessFile("user.dat","r");
		byte[] data = new byte[32];
		for(int i=0;i<raf.length()/100;i++){//循环3次
			System.out.println("第"+i+"次指针位置"+raf.getFilePointer());
			//读取用户名
			raf.read(data);
			String name = new String(data,"UTF-8").trim();
			System.out.println("获取name第"+i+"次指针位置"+raf.getFilePointer());
			
			//读取密码
			raf.read(data);
			String password = new String(data,"UTF-8").trim();
			
			//读取昵称
			raf.read(data);
			String nick = new String(data,"UTF-8").trim();
			
			//读取年龄
			int age = raf.readInt();
			System.out.println("用户信息:"+name+","+password+","+nick+","+age);
		}
	}

}


/**
 * 完成用户注册功能
 * 用user.dat文件保存用户信息
 * 每个用户的信息包括:用户名,密码,昵称,年龄
 * 其中年龄用int值,其余都是字符串
 * @author 臻冉
 *
 */
public class RAF_userRegisterDemo {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf =new RandomAccessFile("user.dat","rw");
		System.out.println(raf.length());
		//设置指针位置
		raf.seek(raf.length());
		String name="文";
		String password="1234";
		String nickName="李";
		int age=18;
		
		//对数组扩容写入姓名
		byte[] data = name.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);
		
		//写入年龄(int类型占用4个字节)
		age=10;
		raf.writeInt(age);
		System.out.println("注册完毕");
		System.out.println(raf.getFilePointer());//查看指针位置
		raf.close();
	}
}


/*
 * 修改用户密码
 * 前提必须登录成功之后才能修改密码
 */
public class RAF_userUpdate {
	public static void main(String[] args) throws Exception {
		Scanner scanner = new Scanner(System.in);
		System.out.println("密码修改功能");
		System.out.println("请输入用户名:");
		String userName = scanner.next();
		System.out.println("请输入用户密码:");
		String userPwd = scanner.next();
		RandomAccessFile raf = new RandomAccessFile("user.dat", "rw");
		byte[] data = new byte[32];
		boolean flagName = false;//账号不存在
		boolean flagPwd = false;//密码错误
		for(int i=0;i<raf.length()/100;i++){
			//设置指针为
			raf.seek(i*100);
			raf.read(data);
			String nameData = new String(data,"UTF-8").trim();
			if(nameData.equals(userName)){		//校验用户输入的名字和文本提取出来的用户名字
				//如果账号存在则比较密码
				raf.read(data);
				String pwdData = new String(data,"UTF-8").trim();
				flagName=true;
				if(pwdData.equals(userPwd)){//校验用户输入的密码和文本提取出来的用户名字
					//登陆成功
					flagPwd = true;
					System.out.println("登录成功");
					//修改密码
					System.out.println("密码修改");
					System.out.println("请输入新的密码:");
					String newPwd = scanner.next();
					//设置指针位置
					raf.seek(i*100+32);
					byte[] bytes = newPwd.getBytes("UTF-8");
					bytes = Arrays.copyOf(bytes, 32);
					raf.write(bytes);
					System.out.println("密码修改成功");
				}
			}
		}
		//提示错误信息
		if(!flagName){
			System.out.println("账号不存在");
			return;
		}
		if(!flagPwd){
			System.out.println("密码错误!");
			return ;
		}
		raf.close();
		scanner.close();
	}
}

public class RAF_writeDemo01 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf01.txt", "rw");
		raf.write("helloworld".getBytes());
		raf.close();
		System.out.println("写入完毕");
	}
}


/**
 * 利用RAF写中文的数据
 * @author 臻冉
 *
 */
public class RAF_writerDemo02 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile  raf = new RandomAccessFile("raf02.txt","rw");
		//String str= "世界上唯一不能复制的是人生";
		String str= ",世界上唯一不能复制的是人生";
		raf.seek(39);
		raf.write(str.getBytes("UTF-8"));
		System.out.println("写入完毕!");
		raf.close();
	}
}


/**
 * String提供了将字符串转化为字节的方法
 * byte[]  getByte()
 * @author 臻冉
 *
 */
public class RAFDemo {
	public static void main(String[] args) throws IOException {
//		String str = "HelloWrold!lfaglkdja;";
		String str = "owrold";
		System.out.println(str.length());
		byte[] n = str.getBytes();//将字符串转换为字节
		RandomAccessFile raf = new RandomAccessFile("raf.dat", "rw");
		//获取指针位置(游标位置)RAF提供了指针位置的方法getFilePointer()
		long p = raf.getFilePointer();//测试写之前指针位置
		System.out.println("写入之前指针位置"+p);
		raf.seek(4);//设置指针位置

		raf.write(n);
		p=raf.getFilePointer();
		System.out.println("写入之后指针位置"+p);
		System.out.println("写入完毕!");
		raf.close();
	}
}

//数据库底层操作
/**
 * 按照系统默认字符集转换(不推荐,存在平台差异)
 * byte[] getByte(String scn)
 * 按照给定的字符集转换,字符集的名字不区分大小写
 * GBK:国际编码  中文2个字符
 * UTF-8:Unicode的字节,也成万国码,中文三个字节
 * IS08859-1:欧洲字符集,不支持中文
 */
public class RAFDemo02 {
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("raf.dat", "rw");
		raf.seek(10);
		String str = ",我很好";
		byte[] b = str.getBytes("UTF-8");
		raf.write(b);
		System.out.println("输入完毕!");
		raf.close();
	}
}

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

/**
 * java.io.RandomAccessFile
 * 该类是专门用来读写文件数据,基于指针读写
 */
public class RandomAccessFileDemo {
	public static void main(String[] args) throws IOException {
		/*
		 * 创建RandomAccessFile常用构造方法:
		 * RandomAccessFile(String path ,String mode)
		 * RandomAccessFile(File file,String mode)
		 * 第一个参数是指要进行读写操作的文件
		 * 第二个参数为权限,常用:
		 * r:只读模式
		 * rw:读写模式
		 * ./可以忽略不写,默认也是当前目录
		 */
		
		/*
		//方式一
		RandomAccessFile raf = new RandomAccessFile("demo.txt","rw");
		System.out.println(raf);
		
		//方式二
		File file = new File("text.txt");
		RandomAccessFile raf1 = new RandomAccessFile(file,"rw"); 
		System.out.println(raf1);
		*/
		
		RandomAccessFile raf = new RandomAccessFile("raf.dat","r");
		/*
		 * void write(int d)
		 * 向文件中写入一个字节数据,写的是给定int值对应的2进制的“低八位”
		 * 
		 * 000000000 00000000 00000000  01010000
		 * 
		 * void writeInt(int d)写入的是int值的字节量			必须是ASCII
		 */
		//97转化为二进制1100001,由于常用编辑器会在打开的时候
		//将字节显示,1-127之间的会查询ASCII编码,会显示a,其他值则查询系统默认码表。
		raf.write(9);
		//raf.writeInt(4);
		System.out.println("输入完毕!");
		/*
		 * RandomAccessFile在文件访问的操作全部结束后,要调用close()方法来释放
		 * 与其关联的所有系统资源
		 */
		raf.close();
		
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值