文章目录
一、idea环境下导入其他软件软件包
1.点击File
2.选择project Structure
3.选择模块、Dependencies->点击加号
4.选择JARs or directories
5.选择要导入的包 点击OK,弹出如下界面
6.选择->ok->应用,即可导包完成
此处可以看到三个不是java提供的包已经导入,以下一一介绍这三个包
包的资源免费获取
二、Junit包
-
概述:JUnit是一个Java语言的单元测试框架。它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个。 JUnit有它自己的JUnit扩展生态圈。多数Java的开发环境都已经集成了JUnit作为单元测试的工具
-
java导入Junit包后可以使用该功能。
方便于测试功能性代码,可以运行多个程序的入口 -
使用方式:通过 junit包下的注解来去使用
-
注解:标识了方法的作用是什么
注解1:@Test 标识了方法是可执行(直接执行)的
注解2:@Before 在Test之前执行
注解3:@After 在Test之后执行 -
注意1:注解可以有多个
注意2:注解有多个时,以方法名区分谁先执行
注意3:三个注解一起使用时,如果有多个Test,每一个Test都会调用一次Before和After
1.了解test功能和使用
观察如下代码
package jar;
import org.junit.Test;
import java.util.ArrayList;
public class Test1 {
static ArrayList<String> arraylist=new ArrayList<>();
public static void main(String[] args) {
addlist();
dellist();
printlist();
}
public static void addlist(){
System.out.println("当前为addlilst方法");
arraylist.add("hello");
arraylist.add("java");
arraylist.add("world");
arraylist.add("bye");
System.out.println(arraylist);
System.out.println(arraylist.size());
}
public static void dellist(){
System.out.println("当前为dellist方法");
arraylist.remove(3);
System.out.println(arraylist.size());
}
public static void printlist(){
System.out.println("当前为输出方法");
for(String s:arraylist){
System.out.println(s);
}
System.out.println(arraylist.size());
}
}
- 有三个方法,若要验证其中的一个方法是否正确,通过main函数每次都要运行其他所有方法
- 如果方法更多的话,显然很不方便
这里引入Junit包,对单个方法进行测试 - 对单个方法进行测试的时候,需将该方法设置为非静态
测试add方法
将main方法中的add方法注释
add方法设置为非静态,加上Test注解,旁边会有个绿色可运行标志
其他地方不变
点击方法旁绿色运行标志运行
由结果可知只对该方法进行了调试运行
2.注解before和after
测试del方法,发现测试del方法前,ArrayList中需要有数据,如果能在测试del方法前,调用add方法就行了,@before提供了该功能
测试dellist
这里只能看到ArrayList的大小,如果想测试完该方法后,遍历查看结果
可以在打印的方法前加上after注解
测试dellist并遍历输出
运行查看结果
3.多个注解一起运行时的顺序
由上可知,运行顺序为before-> test-> after
当有多个test before after,将遵循以下原则
- 三个注解一起使用时,如果有多个Test,每一个Test都会调用一次Before和After(对于某些命名,可能会存在一定的随机性,只能说字母大的先执行的可能性大)
- 有多个test和多个before时,根据方法名,方法名对应的字母或数字大的先执行
- 有多个after注解时,方法名对应的字母和数值大的后执行
代码演示–>测试show方法
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Teststudent {
//有main时,执行类只执行main方法里的,单运行main时,不执行before和after方法
public static void main(String[] args) {
System.out.println("main");
}
@Test//按方法名比较,先执行大的
public void show(){
System.out.println("show");
}
@Test
public void show1(){
System.out.println("show1");
}
@Before//按方法名比较,先执行大的
public void first1(){
System.out.println("这是最先执行的first1");
}
@Before
public void first2(){
System.out.println("这是最先执行的first2");
}
@After
public void b(){
System.out.println("最后执行的b");
}
@After//按方法名比较,先执行小的
public void a(){
System.out.println("最后执行的a");
}
}
对应的before和after按执行顺序都会执行
如果想看多个test注解的执行顺序,可以同过运行类的方式,但需要把main方法注释,否者执行的只是main方法。
三、JSON
- 概述:Json是一种轻量级的数据交换格式,采用一种“键:值”对的文本格式来存储和表示数据,在系统交换数据过程中常常被使用,是一种理想的数据交换语言。
- json可以简单的分为基本形式:json对象,json对象数组。两种基本格式组合变形出其他的形式,但其本质还是json对象或者json对象数组中的一种。
- json对象:数据括在大括号中
- json数组:数据括在方括号中
- json的解析工具两种:一种是谷歌的Gson包,一种是阿里巴巴的fastjson包
四、Gson包
- 概述:Google 开发的json序列化和反序列化工具,性能较为强大,用于数据在网络中的传输。
- Gson类:解析json的最基础的工具类
- toJson
- fromJson
代码演示
import com.google.gson.Gson;
public class Gsontest {
public static void main(String[] args) {
Student student=new Student("1111","张三","男","18","理科四班");
System.out.println(student.toString());
Gson gson=new Gson();
//将对象转为gson字符串,序列化
String strgson=gson.toJson(student);
System.out.println(strgson);
//将gson转为student对象,反序列化
Student str=gson.fromJson(strgson, Student.class);//此处不能填string类型,要填Object
System.out.println(str);
System.out.println("=======================");
//演示json数组
Student student1=new Student("1112","李四","男","19","文科二班");
Student student2=new Student("1113","王五","女","20","文科四班");
Student student3=new Student("1115","Java","女","18","理科三班");
Student[] students=new Student[]{
student,student1,student2,student3};
String s=gson.toJson(students);
System.out.println(s);
Student[] students1=gson.fromJson(s,students.getClass());
for(Student stu:students1){
System.out.println(stu);
}
}
}
五、FastJson包
-
概述:阿里巴巴Alibaba Fastjson Jar包是可以帮助使用Fastjson功能的Fastjson jar包,Fastjson是一个Java语言编写的高性能功能完善的JSON库,当然必备fastjson jar包。快速解析json神器,方便快捷的工具。
-
类中主要是get()方法
-
使用FastJson把json转化为对象:
JSON.parseObject(String json,Class 对象的反射); -
使用FastJson把对象转化为json:
JSON.toJSONString(Object src); -
使用FastJson 通过k获取v
JSON.parseObject(String json);生成JSONObjetc对象然后调用get(Object key)方法
代码演示
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class FastTest {
public static void main(String[] args) {
Student student1=new Student("11112","大熊","男","13","二(2)班");
System.out.println(student1.toString());
String string=JSON.toJSONString(student1);
System.out.println(string);
System.out.println("==============================");
String string1="{\"age\":22,\"clazz\":\"文科六班\",\"sex\":\"女\",\"id\":\"1500100001\",\"name\":\"施笑槐\"}";
JSONObject jsonObject=JSONObject.parseObject(string1);
System.out.println(jsonObject.get("id"));//也能输出,但是返回值不是String类型
Object id=jsonObject.get("id");
Object name=jsonObject.get("name");
Object sex=jsonObject.get("sex");
Object clazz=jsonObject.get("clazz");
Object age=jsonObject.get("age");
Student student2=new Student(id.toString(),name.toString(),sex.toString(),age.toString(),clazz.toString());
System.out.println(student2);
System.out.println("==============================================");
String string2="{\"age\":\"16\",\"clazz\":\"二(6)班\",\"id\":\"11115\",\"name\":\"胖虎\",\"sex\":\"男\"}";
Student student3=JSONObject.parseObject(string2,Student.class);
System.out.println(student3);
System.out.println("=======================");
Student[] students=new Student[]{
student1,student2,student3};
String jsonstr=JSON.toJSONString(students);
System.out.println(jsonstr);
System.out.println("=========================");
Student[] students1=JSON.parseObject(jsonstr,Student[].class);
for(Student s:students1){
System.out.println(s);
}
}
}
六、总结
IDEA导包: File->Project structure->Module->选择模块->Dependencies->点击加号->JARs or.....->包的所在位置 junit:方便于测试功能性代码,可以运行多个程序的入口 使用方式:通过 junit包下的注解来去使用 注解:标识了方法的作用是什么 注解1:@Test 标识了方法是可执行(直接执行)的 注解2:@Before 在Test之前执行 注解3:@After 在Test之后执行 注意1:注解可以有多个 注意2:注解有多个时,以方法名区分谁先执行 注意3:三个注解一起使用时,如果有多个Test,每一个Test都会调用一次Before和After JSON字符串:常用数据传输的格式 格式:大括号括起来的多个元素,每个元素之间以,作为分隔 每个元素中分为KV两个东西,之间以:作为分隔 Google下的Gson: 使用Gson把json转化为对象: fromJson(String json,Class 对象的反射); 使用Gson把对象转化为json: toJson(Object src); alibaba下的FastJson: 使用FastJson把json转化为对象: JSON.parseObject(String json,Class 对象的反射); 使用FastJson把对象转化为json: JSON.toJSONString(Object src); 使用FastJson 通过k获取v JSON.parseObject(String json);生成JSONObjetc对象然后调用get(Object key)方法 注意:json转化为对象时 属性要保持一直
七、案例
1.将txt文件读取写入json文件
txt部分内容参考
import com.alibaba.fastjson.JSON;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Testwrite {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new FileReader("E:\\IdeaProjects\\HuiHui\\students.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\IdeaProjects\\HuiHui\\data1\\students.json"));
String string;
//1500100001,施笑槐,22,女,文科六班
while ((string=br.readLine())!=null){
String[] strings=string.split(",");
if(strings.length==5){
Student student = new Student(strings[0], strings[1], strings[3], strings[2], strings[4]);
String strjso=JSON.toJSONString(student);
bw.write(strjso);
bw.newLine();
bw.flush();
}
}
br.close();
bw.close();
}
}
写入后部分json文件内容参考
2.将json写入txt文件
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class Testread {
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new FileReader("E:\\IdeaProjects\\HuiHui\\data1\\students.json"));
BufferedWriter bw = new BufferedWriter(new FileWriter("E:\\IdeaProjects\\HuiHui\\data1\\student.txt"));
String string;
while ((string=br.readLine())!=null){
Student student=JSONObject.parseObject(string,Student.class);
bw.write(student.getId());
bw.write(",");
bw.write(student.getName());
bw.write(",");
bw.write(student.getSex());
bw.write(",");
bw.write(student.getAge());
bw.write(",");
bw.write(student.getClazz());
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
3.将杂乱无章得数据整理为有序文件
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Map;
public class Test {
public static void main(String[] args)throws Exception {
BufferedReader br = new BufferedReader(new FileReader("E:\\IdeaProjects\\HuiHui\\data\\Student1.json"));
StringBuffer stringBuffer=new StringBuffer();
String string;
while ((string=br.readLine())!=null){
stringBuffer.append(string);
}
JSONObject jsonObject=JSONObject.parseObject(stringBuffer.toString());
Object clazz=jsonObject.get("clazz");
JSONObject jsonObject1=JSONObject.parseObject(clazz.toString());
for(Map.Entry<String, Object> s:jsonObject1.entrySet()){
String str=JSON.toJSONString(s.getValue());
String[] strings=JSON.parseObject(str,String[].class);
for (String string1:strings){
System.out.println(string1);
}
}
}
}
java入门基础学习(一)
java入门基础学习(二)
java入门基础学习(三)
java入门基础学习(四)
java入门基础学习(五)
java入门基础学习(六)
java入门基础学习(七)
java入门基础学习(八)
java入门基础学习(九)
java入门基础学习(十)
java入门基础学习(十一)
java入门基础学习(十二)
java进阶之常见对象(一)
java进阶之常见对象(二)
java进阶之冒泡排序
java进阶之选择排序
java进阶之面向对象(封装)
java进阶之面向对象(代码块、继承)
java进阶之面向对象(多态、抽象、接口)
java进阶之匿名内部类、访问修饰符、包
java进阶之io流(字节流,字符流)