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("文本不存在!");
}
}
}