今天听的是java的中级视频,倒是觉得好多东西只是会用,而不是理解,记之,以备后用(20160110)
【扣丁学堂】
到.properties文件中
下面程序是对用户名,密码,登入成功这样的字分别在中文,英文的界面进行分别显示功能
package practice06;
/**
* 国际化
* 如果是要变化的文字,如对于中文网站是用户名,但对于英文网站就应该是username,
* 此时,就要在包下面建立file文件(->new->File),命名规则:自定字符_规定1_规定2.properties
* 如中文,规定1中填zh 规定2中填CN,这个具体我也不清楚
*
*/
import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
//下面两行就分别定义了中文和英文(用不同的方法)
//只用改变是locale和lecale2就可以切换中英文
Locale locale=Locale.CHINA;
Locale locale2=new Locale("en","US");
//这个下面第一个引号的内容是包名.自定字符
ResourceBundle rb=ResourceBundle.getBundle("practice06.info",locale2);
//替换的变量名是properties中的变量名
System.out.println(rb.getString("welcome"));
System.out.println(rb.getString("input.name"));
String username=input.next();
System.out.println(rb.getString("input.password"));
String password=input.next();
if("abc".equals(username)&&"123".equals(password)){
//动态字符,即在properties文件中的变量info前加上{0},通过下面的语句将动态字符abc添加到{0}处
//不过疑惑的是不管{0}的位置怎么样,abc都是在info字符串的最前面,我试图在输出info前加上一个输出语句,让abc看着像在中间,
String info=MessageFormat.format(rb.getString("info"), " abc ");
System.out.print(rb.getString("info1"));
System.out.println(info);
}
}
}
附上两个.properties文件的内容,为替换的内容
info_zh_CN.properties
welcome=\u6B22\u8FCE\u4F7F\u7528
input.name=\u7528\u6237\u540D\uFF1A
input.password=\u5BC6\u7801\uFF1A
info1=\u6B22\u8FCE\u767B\u9646
info={0}\u597D\u597D\u5B66\u4E60\uFF0C\u5929\u5929\u5411\u4E0A\uFF01
info_en_US.properties
welcome=welocme to codingke.com
input.name=input userName:
input.password=input password:
info1=wolcome!
info={0}login success,good good study,day day up!
Test02讲了一下随机数的产生和Arrays的一些常用方法
package practice06;
import java.util.Arrays;
import java.util.Random;
public class Test02 {
public static void main(String[] args){
System.out.println(Math.round(3.14159));
System.out.println(Math.round(3.14159*100)/100.0);
//返回0-1
System.out.println(Math.random());
System.out.println(Math.PI);
//伪随机数
//产生0-100
Random r=new Random();
System.out.println(r.nextInt(100));
//Arrays数组操作工具类
int[]nums={1,2,3,34,545,6};
Arrays.copyOf(nums, 10);
Arrays.sort(nums);
int []temp =new int[nums.length];
System.arraycopy(nums, 0, temp, 0, temp.length);
//二分查找算法,针对有序列,查找速度快
int index=Arrays.binarySearch(nums, 6);
System.out.println("index="+index);
}
}
Test03 写了一个二分查找的算法(当初用c随便写)很简单
package practice06;
/*
* 二分查找算法
*/
import java.util.Arrays;
public class Test03 {
public static void main(String[] args){
int[] nums={20,34,30,45,54,60};
Arrays.sort(nums);
System.out.println(binarySearch(nums, 30));
}
public static int binarySearch(int[]nums,int key){
int start=0;
int end=nums.length-1;
int mid=-1;
while(start<=end){
mid=(start+end)/2;
if(nums[mid]==key){
return mid;
}else if(nums[mid]>key){
end=mid-1;
}else if(nums[mid]<key){
start=mid+1;
}
}
return -1;
}
}
Test04是对系统时间的调用
package practice06;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test04 {
public static void main(String[] args){
Date now=new Date();
System.out.println(now);
//上面的输出的结果(当前):Sun Jan 10 16:33:41 CST 2016
Calendar c=Calendar.getInstance();
int year=c.get(Calendar.YEAR);
int month=c.get(Calendar.MONTH)+1;
int day=c.get(Calendar.DAY_OF_MONTH);
int week=c.get(Calendar.DAY_OF_WEEK);
int hour=c.get(Calendar.HOUR_OF_DAY);
int minute=c.get(Calendar.MINUTE);
int second=c.get(Calendar.MILLISECOND);
System.out.println(year+"年"+month+"月"+day+"日"+hour+"时 "+minute+"分 "+second+"秒"+"星期 "+(week-1));
//使用时间日期格式模式
DateFormat df= new SimpleDateFormat("yyyy年MM月dd HH:mm:ss SSS");
String nowDate=df.format(new Date());
System.out.println(nowDate);
//返回当前毫秒数
long haom=System.currentTimeMillis();//表示当前系统的时间(毫秒)
}
}
Test05对象比较器,就是对对象中的某个属性进行排序,用Arrays.sort()
程序分别就comparable和comparator两种方式举例说明
(右键->source,有快捷产生方法)
package practice06;
/*
* 对象比较器(对类进行排序)
* comparable接口,直接对类进行修改
* comparator接口,不改类,外加一个类进行修改
*/
import java.util.Arrays;
import java.util.Comparator;
public class Test05 {
public static void main(String[] args){
int[] nums={12,23,43,5,7,4,23};
Arrays.sort(nums);
//这种for语句,没见过,,,,打foreash出现
for(int i:nums){
System.out.println(i);
}
String[] names={"asd","da","daew"};
Arrays.sort(names);
//结合上面的for一起看
for(String str:names){
System.out.println(str);
}
Cat[] cats={new Cat("tom",2),
new Cat("jerry",3),
new Cat("huahua",5)};
Arrays.sort(cats);
for(Cat cat:cats){
System.out.println(cat);
}
Dog[] dogs={new Dog("tom",2),
new Dog("jerry",3),
new Dog("huahua",5)};
Arrays.sort(dogs, new DogComparator());
for (Dog dog : dogs) {
System.out.println(dog);
}
}
}
class Cat implements Comparable<Cat>{
private String name;
private int age;
public Cat(String name,int age) {
// TODO Auto-generated constructor stub
this.name=name;
this.age=age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getYear() {
return age;
}
public void setYear(int age) {
this.age = age;
}
@Override
public String toString() {
return "Cat [name=" + name + ", age=" + age + "]";
}
//通过此方法实现对象的比较
@Override
public int compareTo(Cat o) {
// TODO Auto-generated method stub
if(this.age<o.age){
return -1;
}else if(this.age>o.age){
return 1;
}
return 0;
}
}
class Dog{
private String name;
private int age;
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
}
class DogComparator implements Comparator<Dog>{
@Override
public int compare(Dog o1, Dog o2) {
// TODO Auto-generated method stub
if(o1.getAge()<o2.getAge()){
return -1;
}else if(o1.getAge()>o2.getAge()){
return 1;
}
return 0;
}
}
Test06 对象的克隆 好多代码都是按错误提示直接点击出现的
package practice06;
/*
* 对象的克隆
* 不太理解为什么,只知道怎么做
*/
public class Test06 {
public static void main(String[] args){
Dog1 dog=new Dog1("jack",4);
System.out.println(dog);
try {
Dog1 dog1=(Dog1) dog.clone();
System.out.println(dog1);
System.out.println(dog==dog1);
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Dog1 implements Cloneable{
private String name;
private int age;
public Dog1(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Dog1() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Dog [name=" + name + ", age=" + age + "]";
}
//重写Object的类
@Override
protected Object clone() throws CloneNotSupportedException {
// TODO Auto-generated method stub
return super.clone();
}
}
Test07 二叉树的实现(之前有讲的链表,一个道理,数据结构中的基础)
package practice06;
/*
* 很重要(和链表一样)
* 数据结构之二叉树
*/
public class Test07 {
public static void main(String[] args){
Binary bt=new Binary();
bt.addNode(8);
bt.addNode(4);
bt.addNode(23);
bt.addNode(45);
bt.addNode(1);
bt.printNode();
}
}
class Binary{
private Node root;
public void addNode(int data){
if(root==null){
root = new Node(data);
}else{
root.add(data);
}
}
public void printNode(){
if(root!=null){
root.print();
}
}
public class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.data=data;
}
public void add(int data){
if(this.data>data){
if(this.left==null){
this.left=new Node(data);
}else{
this.left.add(data);
}
}else if(this.data<=data){
if(this.right==null){
this.right=new Node(data);
}else{
this.right.add(data);
}
}
}
//中序遍历
public void print(){
if(this.left!=null){
this.left.print();
}
System.out.print(this.data+"->");
if(this.right!=null){
this.right.print();
}
}
}
}
Test08 文件的基本操作
package practice06;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/*
* file类:表示文件和目录路径名的抽象表示形式
* 可以实现文件的创建,删除,重命名
*
*/
public class Test08 {
/**
*
* @param target 目标文件夹
* @param ext 扩展名
*/
public static void findFile(File target,String ext){
if(target!=null){
//如果为目录,递归
if(target.isDirectory()){
File[] files=target.listFiles();
if(files!=null){
for (File file : files) {
findFile(file, ext);
}
}
}else{
String path=target.getAbsolutePath();
if(path.endsWith(ext)){
System.out.println(path);
}
}
}
}
public static void main(String[] args){
//f:/a.txt或f:\\a.txt或者下面的(这个可以跨平台)
//只是创建一个文件
File file=new File("f:"+File.separator+"a.txt");
System.out.println("文件是否存在"+file.exists());
//创建文件
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("文件的绝对路径"+file.getAbsolutePath());
long lastModeified=file.lastModified();//最后修改的时间
DateFormat df=new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
String lastTime=df.format(new Date(lastModeified));
System.out.println("最后修改的时间为"+lastTime);
System.out.println("文件的长度"+file.length());
System.out.println("是否为目录:"+file.isDirectory());
File file2 =new File("f:\\codingke_test");
if(!file2.exists()){
file2.mkdir();//创建目录
}
// file2.delete();//删除文件
String[] names=file2.list();
for (String name : names) {
System.out.println(name);
}
File[] files=file2.listFiles();
for (File file3 : files) {
System.err.println(file3.getPath()+"--"+file3.length());;
}
System.err.println("-----------------------");
//收索文件
findFile(new File("f:\\codingke_test"), ".txt");
}
}
Test09 字节流 字节输出流,字节输入流
这个有必要讲一下read方法,对于文本文件中的中文一个字是两个字节,
也就意味着一次读的字节数最小为两个字节,但是如果文本文件中有一个英文符号,就可能乱码
(我在想如果读一个字节是否可以,结果全是乱码,因为中文两个字节才是一个字)
后面要讲到的字符流解决了这个问题
package practice06;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 字节流
* 流:一组有序的,有起点和终点的字节集合,是对数据传输的总称或抽象
* 数据在两设备的传输称为流,本质是数据传输,根据数据传输特性将流首相为各种类
* 根据处理数据类型的不同分:字符和字节流
* 根据数据流向不同分为:输入流和输出流
* (以程序为主)输出为写,输入为读
* @author Administrator
*
*/
public class Test09 {
/**
* 字节输出流
*/
public static void read(){
File file=new File("f:\\a.txt");
try {
InputStream in = new FileInputStream(file);
//1M=1024K=1012*1024b
byte[]bytes=new byte[1024*1024*10];//10M的字节数组
/*
* 如果一次读两个字节,对于文本文件就可能会出错,一个中文两个字节,用英文逗号
* 视频学习,?帽喑谈虻ィ?
*/
int len=-1;//每次读取的数据长度
StringBuffer buf=new StringBuffer();
while((len=in.read(bytes))!=-1){
buf.append(new String(bytes,0,len));
}
in.close();
System.out.println(buf);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字节输入流
*/
public static void write(){
File file=new File("f:\\a.txt");
//针对文件创建一个输出流
try {
OutputStream out=new FileOutputStream(file);
//下面这行语句加true,如果程序执行多遍,则在文件中写入多遍
// OutputStream out=new FileOutputStream(file,true);
String info = "视频学习,让编程更简单!";
//写入数据
out.write(info.getBytes());//没有缓存
//关闭
out.close();//释放资源
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
write();
read();
}
}
Test10 字符流 字符输入流,字符输出流
package practice06;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
/**
* Writer:write()flush()close()
* Reader
* 如果操作的是文本类型的文件,我们建议使字符流
* 如果是费文本类型的文件,我们建议使用字节流
*
*
* @author Administrator
*
*/
public class Test10 {
/**
* 字符输入流
*/
public static void read(){
File file=new File("f:\\b.txt");
try {
Reader in=new FileReader(file);
char[]cs=new char[2];
int len=-1;
StringBuffer sb= new StringBuffer();
while((len=in.read(cs))!=-1){
sb.append(new String(cs,0,len));
}
in.close();
System.out.println(sb);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 字符输出流
*/
public static void write(){
File file=new File("f:\\b.txt");
try {
Writer out=new FileWriter(file);
String info="视频学习,让编程更简单!";
out.write(info);
out.write("\r\n");//输出换行符\r为回车,(规定)
out.flush();//刷新缓存并写入文件中
out.close();//关闭时是默认调用刷新的,此语句结束后才写入文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
write();
// read();
}
}
Test11 文件复制
package practice06;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 文件复制
* @author Administrator
*
*/
public class Test11 {
/**
*
* @param target 源文件
* @param dest 目标文件
*/
public static void copyFile(File target,String dest){
String filename=target.getName();
File destFile=new File(dest+filename);
try {
InputStream in=new FileInputStream(target);
OutputStream out=new FileOutputStream(destFile);
byte[]bytes=new byte[1024];
int len=-1;
while((len=in.read(bytes))!=-1){
out.write(bytes, 0, len);
}
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
File target=new File("f:\\codingke_test\\aa.txt");
String dest="f:\\";
copyFile(target, dest);
System.out.println("文件复制成功");
}
}
Test12 字节字符转换流
不知道什么意思,只是知道程序实现了用Scanner接入的功能,再用sysout输出
之前企图用接收长度len!=-1来退出,但是控制台输入的字符是不定的,也就是说不可能退出
最后改写为接收一行
package practice06;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;
/**
* 字节字符转换流
* @author Administrator
*
*/
public class Test12 {
public static String reader(InputStream in){
// OutputStream out =null;
// Writer writer = new OutputStreamWriter(out);
// writer.write("dsdsd");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
//下面没有结束符,读不出,上面改写了接收一行
/* Reader reader=new InputStreamReader(in);
char[] cs=new char[1024];
int len=-1;
StringBuffer buf=new StringBuffer();
try {
while((len=reader.read(cs))!=-1){
buf.append(new String(cs,0,len));
}
reader.close();
return buf.toString();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
*/ }
public static void main(String[] args) {
//
// Scanner input = new Scanner(System.in);
String str=reader(System.in);
System.out.println(str);
}
}