Day29:lombok.jar和junit.jar的使用,以及文件的复制、存储、提取操作

lombok.jar和junit.jar的下载地址:
链接:https://pan.baidu.com/s/142l8uBP8K8sR1f8f_XyDgg
提取码:gkp6

Lombok浅析
简介:

lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码,lombok能够达到的效果就是在源码中不需要写一些通用的方法,但是在编译生成的字节码文件中会帮我们生成这些方法。

安装:

1.下载lombok插件:https://www.projectlombok.org/download
2.cmd窗口运行命令:java -jar lombok.jar
在这里插入图片描述
3.选择eclipse.exe所在位置,安装。

Junit简介:
JUnit是用于编写和运行可重复的自动化测试的开源测试框架, 这样可以保证我们的代码按预期工作。JUnit可广泛用于工业和作为支架(从命令行)或IDE(如Eclipse)内单独的Java程序。

直接导入需要的项目位置即可。
使用eclipse自带的Junit也是可以的。

文件的复制练习
使用junit测试

package com.biubiubiu.demo03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class CopyDemo {
	@Test
	public void copyFile() {
		File file = new File("D:\\11\\picture.jpg");
		FileInputStream fis = null;
		FileOutputStream fos = null;
		if(file.exists()) {
			try {
				byte[] bytes = new byte[(int)file.length()];
				fis = new FileInputStream(file);
				fis.read(bytes);
				fos = new FileOutputStream("C:\\Users\\11142\\Desktop\\picture2.jpg",true);
				fos.write(bytes);
				fos.flush();
			} catch (Exception e) {
				e.printStackTrace();
			}finally {
				try {
					fis.close();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

文件的存储以及提取

package com.biubiubiu.demo03;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class Student {
	private String name;
	private int age;
	private char sex;

	@Override
	public String toString() {
		return getName() + "," + getAge() + "," + getSex();
	}

}

package com.biubiubiu.demo03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

import org.junit.Test;

public class TestDemo {
	ArrayList<Student> stulist = new ArrayList<Student>();

	@Test
	public void add() {
		Student s1 = new Student("王五", 34, '男');
		Student s2 = new Student("里斯", 34, '男');
		Student s3 = new Student("张三", 34, '男');
		stulist.add(s1);
		stulist.add(s2);
		stulist.add(s3);
		System.out.println("集合大小:" + stulist.size());

		StringBuffer str = new StringBuffer();
//	 	str.append(";");
//		str.append(s2);
//	 	str.append(";");
//	 	str.append(s3);
//		System.out.println(str);
		for (Student student : stulist) {
			str.append(student + ";");
		}

		byte[] content = str.toString().getBytes();
		try {
			File file = new File("C:\\Users\\dell\\Desktop\\Student.txt");
			FileOutputStream out = new FileOutputStream(file, true);
			out.write(content);
			out.close();
		} catch (Exception e) {

		}
	}

	@Test
	public void show() {
		try {
			File file = new File("C:\\Users\\dell\\Desktop\\Student.txt");
			byte[] content = new byte[(int) file.length()];
			FileInputStream out = new FileInputStream(file);
			out.read(content);
			out.close();

			String str = new String(content);
			System.out.println(str);
			String[] students = str.split(";");
			for (String student : students) {
				String[] value = student.split(",");
				Student stu = new Student(value[0], Integer.parseInt(value[1]), value[2].charAt(0));
				stulist.add(stu);
			}
		} catch (Exception e) {

		}

		System.out.println("*****" + stulist.size());
		for (Student student : stulist) {
			System.out.println(student);
		}
	}
}

文件的基本操作

package com.biubiubiu.demo03;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.junit.Test;

public class FileStreamDemo {
	@Test
   public void filedemo() {
	   File file=new File("C:\\","student.txt");
	   System.out.println(file.getName());//获取文件名称
	   System.out.println(file.getPath());///得到一个文件的路径名
	   System.out.println(file.getAbsolutePath());//获取绝对路径
	   System.out.println(file.isFile());//判断是否是文件
	   System.out.println(file.exists());//判断文件或者目录是否存在
	   System.out.println(file.length());
	   try {
	    byte[] b=new byte[(int)file.length()];
		FileInputStream input=new FileInputStream(file);//把File类转为FileInputStream类
		input.read(b);//把流数据读取后存放在数组b中
		input.close();//关闭流
		
		//-----对字节数组进行处理
		for (byte c : b) {
			System.out.println(c);
		}
		String str=new String(b);
		System.out.println(str);
		
		//------------------------------------对文件进行写的操作
		String content="**,是个好蛋";
		byte[] s=content.getBytes();//把字符串转为字节数组
		FileOutputStream out=new FileOutputStream(file,false);//把File类转为FileOutputStream类
		out.write(s);
		out.close();
	   } catch (FileNotFoundException e) {
         //System.out.println("文件找不到");
		   e.printStackTrace();
	   }catch(IOException e1) {
		   e1.printStackTrace();
	   }
	   
	   System.out.println("***********");
   }
	
	
   @Test
   public void copypaster() {
	   //把c:\\kgc\\fangzi.jpg图片复制到桌面中
	   File file=new File("C:\\fangzi.jpg");
	   String filename=file.getName();//获取文件名
	   if(file.exists()) {
		   byte[] content=new byte[(int)file.length()];//准备用来存放文件内容的数组
		   
		   try {
		   FileInputStream input=new FileInputStream(file);
		   input.read(content);
		   input.close();
		   
		   //在桌面中创建一个文件
		   File f=new File("C:\\Users\\Administrator\\Desktop",filename);
		   System.out.println(f.exists());
		   FileOutputStream out=new FileOutputStream(f);
		   out.write(content);
		   out.close();
		   
		   }catch(FileNotFoundException e) {
			   
		   }catch(IOException e1) {
			   
		   }
	   }
	   else {
		   System.out.println("文本不存在!");
	   }
   }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值