1、String类型转化成int类型
String str = "890";
int num = Integer.parseInt(str);
2、Calcendar
public class Test7 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入年份:");
int year = scanner.nextInt();
System.out.print("请输入月份:");
int month= scanner.nextInt();
System.out.print("请输入日期:");
int day = scanner.nextInt();
String[] weekDays = DateFormatSymbols.getInstance(Locale.CHINA).getWeekdays();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
cal.set(Calendar.DATE, day);
int week = cal.get(Calendar.DAY_OF_WEEK);
System.out.println(sdf.format(cal.getTime())+"是");
System.out.println(weekDays[week]);
}
}
3、Properties
public void changeSecret(String source,String name,String secret){
Properties pro = new Properties();
Scanner sc = new Scanner(System.in);
try {
pro.load(new FileReader(source));
if(pro.containsKey(name)){
if(pro.getProperty(name).equals(secret)){
System.out.println("请输入新密码:");
String sec = sc.next();
pro.setProperty(name, sec);
pro.store(new FileWriter(source), "");
System.out.println("修改成功");
}else{
System.out.println("密码错误");
}
}else{
System.out.println("用户名不存在");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
利用相对路径读取文件:
public void readsrc(){
Properties properties=new Properties();
//通过程序获得src下面的文件的输入流
try {
properties.load(this.getClass().getClassLoader().getResourceAsStream("users.properties"));
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("pass"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4、可序列化Serializable
(1)现实现Serializable接口
public class Students implements Serializable{
private String name;
private String number;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
(2)直接将对象写入或读出
public void writeSerializable(String source){
ArrayList<Students> list = new ArrayList<Students>();
try {
BufferedReader reader = new BufferedReader(new FileReader(source));
String str = reader.readLine();
while(str != null){
Students stu = new Students();
String[] s = str.split(":");
stu.setNumber(s[0]);
stu.setName(s[1]);
list.add(stu);
str = reader.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ObjectOutputStream objectOutput = new ObjectOutputStream(new FileOutputStream("e://students.txt"));
objectOutput.writeObject(list);
objectOutput.flush();
objectOutput.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}