我是最棒的!基础不牢,地动山摇!
1、定义一个方法:返回数组中最小的那个数(5分);
package cn.itsource.exam;
import java.util.Arrays;
/**
* 1、定义一个方法:返回数组中最小的那个数(5分);
*/
public class Exam1 {
public static void main(String[] args) {
int[] arr = {3,2,5,8,1,0};
System.out.println(getMin(arr));
}
public static int getMin(int[] arr){
Arrays.sort(arr);
return arr[0];
}
}
2. 定义一个方法,查询指定文件夹下所有的avi文件,并测试(15分)
package cn.itsource.exam;
import java.io.File;
/**
* 定义一个方法,查询指定文件夹下所有的avi文件,并测试(15分)
*/
public class Exam2 {
public static void main(String[] args) {
File file = new File("F:/test");
searchAvi(file);
}
public static void searchAvi(File file){
if(file == null || !file.exists()){
return;
}
if(file.isFile()){
throw new IllegalArgumentException("请传入文件夹");
}
File[] files = file.listFiles();
for (File f : files) {
if(f.isFile() && f.getName().endsWith(".avi")){
System.out.println(f.getAbsolutePath());
}else if (f.isDirectory()) {
searchAvi(f);
}
}
}
}
3**、有如下的一个字符串 String str =** “ksjhfkjashfkahfkahfiuuytwurekahd**”;**
① 请统计出其中每一个字母出现的次数,并把结果保存到一个文件中去:
② 请打印出字母次数最多的那一对
请用到知识:泛型、集合(Map或者List或者Set**)(20****分)**
package cn.itsource.exam;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
* 有如下的一个字符串 String str = “ksjhfkjashfkahfkahfiuuytwurekahd”;
① 请统计出其中每一个字母出现的次数,并把结果保存到一个文件中去:
② 请打印出字母次数最多的那一对
请用到知识:泛型、集合(Map或者List或者Set)(20分)
*/
public class Exam3 {
public static void main(String[] args) {
String str = "ksjhfkjashfkahfkahfiuuytwurekahd";
File file = new File("test.txt");
summary(str,file);
}
public static void summary(String str,File file){
char[] ch = str.toCharArray();
HashMap<Character, Integer> hashMap = new HashMap<Character,Integer>();
for (char s : ch) {
Integer c = hashMap.get(s);
if(c == null){
hashMap.put(s, 1);
}else {
c++;
hashMap.put(s, c);
}
}
// ArrayList list = new ArrayList();
FileWriter fw = null;
try {
fw = new FileWriter(file,true);
Set<Entry<Character, Integer>> entries = hashMap.entrySet();
for (Entry<Character, Integer> entry : entries) {
System.out.println(entry.getKey()+"----"+entry.getValue());
fw.write(entry.getKey()+"----"+entry.getValue()+"\r\n");
// sb.append(entry.getKey()+"----"+entry.getValue()+",");
}
System.out.println("写出成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(fw != null){
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
int maxValue = (Integer)getMaxValue(hashMap);
ArrayList<Object> list = getMaxKey(hashMap, maxValue);
System.out.println("出现次数最多的是:");
for (Object object : list) {
// System.out.println("出现次数最多的是:");
System.out.println(object + "----" + hashMap.get(object));
}
}
/**
* 获取最大值对应的value
* @param hashMap
* @param value
* @return
*/
public static ArrayList<Object> getMaxKey(HashMap<Character, Integer> hashMap,Integer value){
ArrayList<Object> list = new ArrayList<Object>();
Set<Entry<Character, Integer>> entries = hashMap.entrySet();
for (Entry<Character, Integer> entry : entries) {
if(entry.getValue() == value){
list.add(entry.getKey());
}
}
return list;
}
/**
* 获取最大的值
* @param hashMap
* @return
*/
public static Object getMaxValue(HashMap<Character, Integer> hashMap){
if(hashMap.size() == 0){
return null;
}
Collection<Integer> collection = hashMap.values();
Object[] objects = collection.toArray();
Arrays.sort(objects);
return objects[objects.length-1];
}
}
4**、交易明细文本文件内容如下:(高级)**
客户号 | 姓名 | 所属机构号 | 性别 | 帐号 | 发生时间 | 发生额 |
---|---|---|---|---|---|---|
000001 | 刘德华 | 0000 | 1 | 4155990188888888 | 2014-07-20 20:00:05 | 300.00 |
000201 | 梁晓龙 | 0002 | 1 | 4155990199999999 | 2014-07-20 20:00:05 | 500.00 |
000101 | 黄晓明 | 0012 | 1 | 4155990100000000 | 2014-07-20 20:00:05 | 1000.50 |
000102 | 张东健 | 0012 | 1 | 4155990155555555 | 2014-07-20 20:00:05 | 600.99 |
000301 | 苍老师 | 0013 | 0 | 41559901111111111 | 2014-07-22 20:10:05 | 5000.00 |
000001 | 刘德华 | 0000 | 1 | 4155990188888888 | 2014-07-25 20:00:05 | 200.00 |
---------------------------------------------------------------------------
一行是一条交易明细,每行分7****列。
设计一个类TransRecord****用来封装交易记录对象
(金额字段数据类型定为double)。
解析文件(将所有交易明细TransRecord对象)所有数据封装成为List。实现如下功能:
1).加载所有数据到程序中,并且打印到控制台;(就是将对象添加到集合并打印集合)(5分)
2).完成一个功能(方法):输入客户号,查询交易明细记录并打印出来,封装成一个方法,通过传入客户号作为参数(10分)
3).定义一个方法,计算总金额并打印到控制台;(遍历集合中对象,获取每个对象的金额相加)(10分)
4).定义一个方法,按金额升序排序,并且打印到控制台;Set自然或定制排序(10****分)
package cn.itsource.exam;
import java.util.Date;
/**
* 交易记录类
*
*/
public class Exam4_TransRecord implements Comparable<Exam4_TransRecord>{
/**客户号*/
private String customerNo;
/**姓名*/
private String name;
/**所属机构号*/
private String organNo;
/**性别*/
private Exam4_Gender gender;
/**账号*/
private String accountNumber;
/**发生时间*/
private Date startTime;
/**发生额*/
private double accrual;
@Override
public String toString() {
return "TransRecord [customerNo=" + customerNo + ", name=" + name + ", organNo=" + organNo + ", gender="
+ gender + ", accountNumber=" + accountNumber + ", startTime=" + startTime + ", accrual=" + accrual
+ "]\r\n";
}
public String getCustomerNo() {
return customerNo;
}
public String getName() {
return name;
}
public String getOrganNo() {
return organNo;
}
public int getGender() {
return gender == Exam4_Gender.MAN ? 1 :0;
}
public String getAccountNumber() {
return accountNumber;
}
public Date getStartTime() {
return startTime;
}
public double getAccrual() {
return accrual;
}
public void setCustomerNo(String customerNo) {
this.customerNo = customerNo;
}
public void setName(String name) {
this.name = name;
}
public void setOrganNo(String organNo) {
this.organNo = organNo;
}
public void setGender(int i) {
this.gender = i == 1 ? Exam4_Gender.MAN : Exam4_Gender.WOMEN;
}
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
public void setStartTime(Date startTime) {
this.startTime = startTime;
}
public void setAccrual(double accrual) {
this.accrual = accrual;
}
@Override
public int compareTo(Exam4_TransRecord o) {
//非null检验,直接调用工具类中的方法
// Examination.checkValidity(o);
Exam4.check(o);
if(this.getAccrual() > o.getAccrual()){
return 1;
}else if (this.getAccrual() < o.getAccrual()) {
return -1;
}else {
return 0;
}
}
}
package cn.itsource.exam;
/**
* 性别枚举
*/
public enum Exam4_Gender {
MAN(1),WOMEN(0);
Exam4_Gender(int i){
}
}
package cn.itsource.exam;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.TreeSet;
public class Exam4_Test {
public static void main(String[] args) {
//按照指定模板创建日期格式类
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// sdf.parse("2014-07-20 20:00:05");
//创建一个List容器用来存放所有的 交易对象
List<Exam4_TransRecord> list = new ArrayList<Exam4_TransRecord>();
//创建一个交易明细对象,并赋值
Exam4_TransRecord record1 = new Exam4_TransRecord();
record1.setCustomerNo("000001");
record1.setName("刘德华");
record1.setOrganNo("0000");
record1.setGender(1);
record1.setAccountNumber("4155990188888888");
try {
Date startTime1 = sdf.parse("2014-07-20 20:00:05");
String date = sdf.format(startTime1);
System.out.println(startTime1);
record1.setStartTime(startTime1);
} catch (ParseException e) {
e.printStackTrace();
}
record1.setAccrual(300.00);
//创建一个交易明细对象,并赋值
Exam4_TransRecord record2 = new Exam4_TransRecord();
record2.setCustomerNo("000201");
record2.setName("梁晓龙");
record2.setOrganNo("0002");
record2.setGender(1);
record2.setAccountNumber("4155990199999999");
try {
Date startTime2 = sdf.parse("2014-07-20 20:00:05");
record2.setStartTime(startTime2);
} catch (ParseException e) {
e.printStackTrace();
}
record2.setAccrual(500.00);
//创建一个交易明细对象,并赋值
Exam4_TransRecord record3 = new Exam4_TransRecord();
record3.setCustomerNo("000101");
record3.setName("黄晓明");
record3.setOrganNo("0012");
record3.setGender(1);
record3.setAccountNumber("4155990100000000");
try {
Date startTime3 = sdf.parse("2014-07-20 20:00:05");
record3.setStartTime(startTime3);
} catch (ParseException e) {
e.printStackTrace();
}
record3.setAccrual(1000.50);
//创建一个交易明细对象,并赋值
Exam4_TransRecord record4 = new Exam4_TransRecord();
record4.setCustomerNo("000102");
record4.setName("张东健");
record4.setOrganNo("0012");
record4.setGender(1);
record4.setAccountNumber("4155990155555555");
try {
Date startTime4 = sdf.parse("2014-07-20 20:00:05");
record4.setStartTime(startTime4);
} catch (ParseException e) {
e.printStackTrace();
}
record4.setAccrual(600.99);
//创建一个交易明细对象,并赋值
Exam4_TransRecord record5 = new Exam4_TransRecord();
record5.setCustomerNo("000301");
record5.setName("苍老师");
record5.setOrganNo("0013");
record5.setGender(0);
record5.setAccountNumber("41559901111111111");
try {
Date startTime5 = sdf.parse("2014-07-22 20:10:05");
record5.setStartTime(startTime5);
} catch (ParseException e) {
e.printStackTrace();
}
record5.setAccrual(5000.00);
//创建一个交易明细对象,并赋值
Exam4_TransRecord record6 = new Exam4_TransRecord();
record6.setCustomerNo("000001");
record6.setName("刘德华");
record6.setOrganNo("0000");
record6.setGender(1);
record6.setAccountNumber("4155990188888888");
try {
Date startTime6 = sdf.parse("2014-07-25 20:00:05");
record6.setStartTime(startTime6);
} catch (ParseException e) {
e.printStackTrace();
}
record6.setAccrual(200.00);
list.add(record1);
list.add(record2);
list.add(record3);
list.add(record4);
list.add(record5);
list.add(record6);
System.out.println(list);
//完成一个功能(方法):输入客户号,查询交易明细记录并打印出来,封装成一个方法,通过传入客户号作为参数(10分)
printRecord("000101", list);
//定义一个方法,计算总金额并打印到控制台;(遍历集合中对象,获取每个对象的金额相加)(10分)
printAllMoney(list);
//类实现了comparable接口,按照金额升序
TreeSet<Exam4_TransRecord> treeSet = new TreeSet<Exam4_TransRecord>(list);
System.out.println(treeSet);
}
/**
* 完成一个功能(方法):输入客户号,查询交易明细记录并打印出来,封装成一个方法,通过传入客户号作为参数(10分)
* @param customerNo
*/
public static void printRecord(String customerNo,List<Exam4_TransRecord> list){
Exam4.check(customerNo);
Exam4.check(list);
boolean flag = false;
for (Exam4_TransRecord e : list) {
if(customerNo.equals(e.getCustomerNo())){
System.out.println(e);
flag = true;
}
}
if(flag == false){
System.out.println("未找到改用户信息");
}
}
/**
* 定义一个方法,计算总金额并打印到控制台;(遍历集合中对象,获取每个对象的金额相加)(10分)
* @param list
* @return
*/
public static void printAllMoney(List<Exam4_TransRecord> list){
Exam4.check(list);
double sum = 0;
for (Exam4_TransRecord e : list) {
sum += e.getAccrual();
}
System.out.println("总金额为:"+sum);
}
// public static void printByAccrual(List list){
// Exam4.check(list);
//
// }
}
package cn.itsource.exam;
import java.util.List;
public class Exam4 {
public static void check(String customerNo){
if(customerNo == null || customerNo.length() == 0){
throw new IllegalArgumentException("参数不能为空");
// throw new Error("出现错误");
}
}
public static void check(List<Exam4_TransRecord> list){
if(list == null || list.size() == 0){
throw new IllegalArgumentException("参数不能为空");
}
}
public static void check(Exam4_TransRecord o){
if(o == null){
throw new IllegalArgumentException("参数不能为空");
}
}
}
5. 定义一个方法,根据传入的文件路径,将一个不定类型(这里只是txt或者非txt)的文件(File对象),复制到传入的路径下,文件名为唯一不重复的即可,文件类型跟传入的文件类型一致。测试该方法(附加题:20分)
package cn.itsource.exam;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ThreadLocalRandom;
/**
* 定义一个方法,根据传入的文件路径,将一个不定类型(这里只是txt或者非txt)的文件(File对象)
* ,复制到传入的路径下,文件名为唯一不重复的即可,文件类型跟传入的文件类型一致。测试该方法(附加题:20分)
*/
public class Exam5 {
public static void main(String[] args) {
// File file = new File("1.1.1.txt");
// System.out.println(getSuffix(file));
// File file1 = new File("F:/test");
// File file = new File("F:\\test\\B\\C\\ddd.txt");
// System.out.println(file.getName());
// System.out.println("ddd.txt".equals(file.getName()));
// System.out.println(file1.getName());
File file2 = new File("C:\\Users\\tyx666\\Pictures\\Saved Pictures\\timg.jpg");
for(int i = 0; i < 100;i++ ){
copyFile("F:/test2", file2);
}
// copyFile("F:/test2", file2);
// boolean flag = searchFile("ddd.txt", file1);
// System.out.println(flag);
}
/**
*
* @param path 路径
* @param file 要复制的文件
*/
public static void copyFile(String path,File file){
if(file == null || !file.exists()){
return;
}
if(file.isDirectory()){
throw new IllegalArgumentException("请传入具体文件");
}
File filePath = new File(path);
if(!filePath.isDirectory()){
return;
}
String[] strs = file.getName().split("\\.");
StringBuilder sb = new StringBuilder();
String fileName = sb.append(path).append("/").append(strs[0]).
append(getUniqueFileName()).append(ThreadLocalRandom.current().nextInt(0, 1000000)).
append(getSuffix(file)).toString();
// String suffix = getSuffix(file);
// String fileName = filePath + getUniqueFileName() + suffix;
if(searchFile(fileName, filePath)){
ThreadLocalRandom random = ThreadLocalRandom.current();
String s = String.valueOf(random.nextInt(0,10000));
fileName += s;
}
File destFile = new File(fileName);
copy(file, destFile);
}
public static String getSuffix(File file){
String fileName = file.getName();
String suffix = fileName.substring(fileName.lastIndexOf("."));
return suffix;
}
/**
* 获取唯一文件名方法
* @return
*/
public static String getUniqueFileName(){
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String formatDate = sdf.format(new Date());
return formatDate;
}
/**
* 复制文件方法
* @param file
* @param destFile
*/
public static void copy(File file,File destFile){
if(file == null || !file.exists()){
return;
}
if(file.isDirectory()){
throw new IllegalArgumentException("请传入具体文件");
}
//当文件后缀为txt时
if(file.getName().endsWith(".txt")){
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(file);
fw = new FileWriter(destFile);
char[] ch = new char[1024];
int read = -1;
while((read = fr.read(ch)) != -1){
fw.write(ch, 0, read);
}
System.out.println("写出txt文件成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
IOUtils.closeIO(fr, fw);
}
}else {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(destFile);
byte[] b= new byte[1024];
int read = -1;
while ((read = fis.read(b)) != -1) {
fos.write(b, 0, read);
}
System.out.println("写出非txt文件成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
IOUtils.closeIO(fis, fos);
}
}
}
/**
* 搜索指定的路径下是否包含该文件
* @param fileName
* @param path
* @return
*/
public static boolean searchFile(String fileName,File path){
if(path == null || !path.exists()){
throw new IllegalArgumentException("路径不存在或路径为空");
}
if(!path.isDirectory()){
throw new IllegalArgumentException("请传入路径");
}
if(fileName == null){
throw new IllegalArgumentException("文件名不能为空");
}
File[] files = path.listFiles();
// System.out.println(Arrays.toString(files));
if(files != null){
for (File f : files) {
// System.out.println(fileName + " " + f.getName());
if(f.isFile() && fileName.equals(f.getName())){
// System.out.println("666");
return true;
}else if(f.isDirectory()){
searchFile(fileName, f);
}
}
}
return false;
}
}
关流工具类
package cn.itsource.exam;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
public class IOUtils {
/**
* 关流
* @param is
* @param os
*/
public static void closeIO(InputStream is,OutputStream os){
try {
if(os != null){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(is != null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 关流
* @param is
* @param os
*/
public static void closeIO(Reader reader,Writer writer){
try {
if(writer != null){
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}