迟迟不开学,只能在家自己看视频学学Java
希望能有大佬指点下。
在sheet.txt文件中将学生姓名以;的形式隔开,再通过源码读取,分割成数组,进行随机数组下标,来随机学生姓名
[Java] 纯文本查看 复制代码 import java.io.*;
//读取txt文件中数据提取出来
public class ReadDate {
public String read(){
File file = new File(System.getProperty("user.dir")+"/sheet/sheet.txt");
BufferedReader reader =null;
StringBuffer sbf = new StringBuffer();
try {
reader = new BufferedReader(new FileReader(file));
String s;
while (null != (s = reader.readLine())){
sbf.append(s);
}
reader.close();
return sbf.toString();
}catch (IOException e){
e.printStackTrace();
}finally {
if (null != reader){
try {
reader.close();
}catch (IOException e1){
e1.printStackTrace();
}
}
}
return sbf.toString();
}
}
[Java] 纯文本查看 复制代码 import java.util.Random;
//将数据以;切割成数组,并获取长度进行随机下标,打印对应随机下标的数据
public class RandomStu {
public void Split(String str){
String[] strs = str.split(";");
Random random = new Random();
int sub = random.nextInt(strs.length);
System.out.println(strs[sub]);
}
}
[Asm] 纯文本查看 复制代码 public class Main {
public static void main(String[] args) {
ReadDate readDate = new ReadDate();
String str = readDate.read();
RandomStu split = new RandomStu();
split.Split(str);
}
}
源码链接:https://wwe.lanzouj.com/iasQ5ed8e2b |