2017-06-26

对象数组中遍历的应用:

package com.zhongruan.javase.test;

public class Ni {

public static void main(String[] args) {

// int []a=new int[]{1,2};
// int []b=new int[2];
//

fish []f=new fish[3];
f[0]= new fish();
f[1]=new fish();
f[2]=new fish();
f[0].age=2;
f[0].name="娃娃鱼";
f[0].type="feiy";

f[1].age=2;
f[1].name="娃娃鱼";
f[1].type="feiy";

f[2].age=2;
f[2].name="娃娃鱼";
f[2].type="feiy";


for(fish k:f){
System.out.println(k.age
);
}

}

}
class fish{
String name;
String type;
int age;


}

输出结果是:2   2   2

File类文件的方法及遍历应用:

package com.zhongruan.javase.test;

import java.io.File;
import java.io.IOException;

public class Iooo {

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建一个文件
File f=new File("xiaopang.txt");

//判断文件是否存在
boolean a=f.exists();
if(a){
System.out.println("wenjiancunzai");
}
else{
System.out.println("bucunzai");
}
//创建一个新文件
boolean b=f.createNewFile();
if(b){
System.out.println("wenjianchuangjianchenggong ");
}
else{
System.out.println("chuagnjianbuchenggong ");
}
//获取文件名
String str=f.getName();
System.out.println(str);
//获取文件的绝对路径
String pa=f.getAbsolutePath();
System.out.println(pa);
//判断是否成功删除文件
// boolean c=f.delete();
// if(c){
// System.out.println("删除成功");
// }
// else{
// System.out.println("未删除");
// }
boolean d=f.exists();
if(d){
System.out.println("文件存在");
}
else{
System.out.println("文件不存在");
}
long l=f.length();
System.out.println(l);
File a1=f.getParentFile();
System.out.println(a1);
boolean f1=f.isDirectory();
System.out.println(f1);
File f10=new File("D:/workspace/com.zhongruan.java.test");
String []s=f10.list();
for(String k:s){
System.out.println(k);
}
}

}

输出结果是:

wenjiancunzai
chuagnjianbuchenggong
xiaopang.txt
D:\workspace\com.zhongruan.java.test\xiaopang.txt
文件存在
0
null
false
.classpath
.project
.settings
bin
src
xiaopang.txt
收到了金风科技.txt

posted on 2017-06-26 19:32  Java256  阅读(143)  评论(0)    收藏  举报

导航