目录
使用IO技术,开发出一个控制台的资源管理器!要求:从命令行输入一个路径!如果存在,将该目录下所有的文件和文件夹列举出来,如果不存在则输出不存在该路径。
从控制台输入一些字符串,并将该类信息保存到日志文件”log.txt”中去。
从控制台进行输入用户名以及用户密码,判断是否登录成功!要求准确的用户名和密码存在配置文件中!
使用IO技术,创建一个目录,然后复制一个文件到该目录!
public class Test {
public static void main(String[] args) {
File file = new File("a.txt");
work(file);
work2(file);
}
/**
* 创建文件,读文件 (字节型)
*/
public static void work(File file){
try(InputStream inputStream = new FileInputStream(file);
OutputStream outputStream = new FileOutputStream("b.txt")) { // 设置文件路径
int length; // 设置接收长度
byte[] by = new byte[1024]; // 设置每次接收字节数
while((length = inputStream.read(by)) != -1){ // 判断是否还有字节
outputStream.write(by,0,length); // 根据字节长度进行拷贝
System.out.println(length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 字符型
* @param file
*/
public static void work2(File file){
try(
Reader inputFile = new FileReader(file);
Writer outFile = new FileWriter("c.txt"); // 设置路径
) {
int length; // 设置拷贝长度
while((length = inputFile.read()) != -1){ // 判断是否进行循环
outFile.write(length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用IO技术,开发出一个控制台的资源管理器!要求:从命令行输入一个路径!如果存在,将该目录下所有的文件和文件夹列举出来,如果不存在则输出不存在该路径。
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入文件路径 : ");
file(sc.nextLine());
}
/**
* 判断是不是路径
* @param str
*/
public static void file(String str){
File file = new File(str);
if (file.exists()){
work(str);
}else{
System.out.println("路径错误!!!");
}
}
/**
* 递归循环
* @param str
*/
public static void work(String str){
File file = new File(str); // 目录
if (file.isFile()){ // 判断是不是文件
System.out.println(file.getName()); // 文件输出名字
return;
}else if (file.exists()){ // 判断是不是目录
File[] files = file.listFiles(); // 把当前目录下的文件和文件夹都存到数组里
for(File f : files){ // 遍历数组
System.out.println(f); // 输出
work(f.getName()); // 递归
}
}
}
}
从控制台输入一些字符串,并将该类信息保存到日志文件”log.txt”中去。
public class Test {
public static void main(String[] args) {
inputFile();
}
public static void inputFile(){
Scanner sc= new Scanner(System.in);
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 初始化时间
try(Writer writer = new FileWriter("log.txt")) { // 确定日志目录
while (true){ // 循环记录日志
System.out.println("请输入 : ");
String str = sc.nextLine();
writer.write(format.format(date.getTime()) + " : " + str + "\n"); // 格式 : 时间+内容+换行
System.out.println("是否继续(y/n) : ");
String s = sc.nextLine();
if (!"y".equals(s)){ //y继续,非y退出
return;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
从控制台进行输入用户名以及用户密码,判断是否登录成功!要求准确的用户名和密码存在配置文件中!
public class Test {
public static void main(String[] args) {
// work();
// check();
register();
}
/**
* 查看登录是否正确
*/
public static void work() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的账号 : ");
String id = sc.nextLine();
System.out.println("请输入您的密码 : ");
String pwd = sc.nextLine();
try (FileReader reader = new FileReader("account.txt")) { // 确认路径
Properties pro = new Properties();
pro.load(reader); // 文件中的数据加载到Map集合中了
String str = (pro.getProperty(id).equals(pwd)) ? "登录成功" : "账号或密码错误";
System.out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 打印输出所有账号密码
*/
public static void check() {
try (FileReader reader = new FileReader("account.txt")) {
Properties pro = new Properties();
pro.load(reader); // 文件中的数据加载到Map集合中了
System.out.println(pro);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 注册
*/
public static void register() {
Scanner sc = new Scanner(System.in);
System.out.println("请输入您的账号 : ");
String id = sc.nextLine();
System.out.println("请输入您的密码 : ");
String pwd = sc.nextLine();
try (FileWriter writer = new FileWriter("account.txt",true)) {
writer.write(id + "=" + pwd +"\n" );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
创建一个学生类,包含属性:学号、姓名、性别,包含show()方法用于显示学生的详细信息。创建测试类,在控制台上显示添加学生信息,要求程序循环运行,并依次提示接收学生类的所有属性值,保存到学生对象中,再将学生对象保存到集合对象中,并提示“是否继续添加(y/n):”,如果选择“y”则继续添加,否则退出循环,并将保存学生数据的集合对象通过序列化保存到“student.dat”文件中。实现从“student.dat”文件中反序列化保存学生数据的集合对象,并遍历打印输出学生信息。
public class Test {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
list = studentObject(); // 添加学生信息 + 循环打印学生信息
inputSerialize(list); // 序列化
outputSerialize(); // 反序列化
}
/**
* 循环添加学生信息new对象,到集合中
* @return
*/
public static List<Student> studentObject(){
Scanner sc = new Scanner(System.in); // 导入包
List<Student> list = new ArrayList<>(); // 创建新集合
while(true){ // 要求循环
System.out.println("请输入学生的id : ");
int id = sc.nextInt();
sc.nextLine(); // 接收回车
System.out.println("请输入学生的姓名 : ");
String name = sc.nextLine();
System.out.println("请输入学生的性别 : ");
String gender = sc.nextLine();
Student student = new Student(id,name,gender); // 创建新Student,并把控制台接收到的id,name,gender给Student
list.add(student); // 把新的对象放到集合中
System.out.println("是否继续(y/n) : ");
if (!"y".equals(sc.nextLine())){ // 判断用户输入的是否为y
list(list);
return list;
}
}
}
/**
* 遍历集合信息
* @param list
*/
public static void list(List<Student> list){
for (Student s : list){
s.show();
}
}
/**
* 把集合对象通过序列化保存到"student.dat"文件中
*/
public static void inputSerialize(List<Student> list){
// 序列化
try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("students.dat"))){
/* for (Student s : list){ // 好想个大傻逼,遍历个屁啊
output.writeObject(s); //序列化对象
output.flush(); // 刷新
}*/
output.writeObject(list); // 集合序列化
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 反序列化
*/
public static void outputSerialize(){
try(ObjectInputStream input = new ObjectInputStream(new FileInputStream("students.dat"));) {
List<Student> o = (List<Student>)input.readObject();// 反序列化
for (Student s : o){ // 遍历集合s
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}