`
huobengle
  • 浏览: 864187 次
文章分类
社区版块
存档分类
最新评论

java学习心得

 
阅读更多

今天是周末,好久都没有写作业了,今天趁周末有空一口气写出来准备星期一就交。(说实在的,学计算机的做作业正是无聊,尤其是书后的练习。。。)
不过,既然作了,还是有点收获的。毕竟java平台是最近一个月才转过来的,俺以前一直是搞VB和C#的。如果不是学习开了java课,可能暂时还不会转移到java平台。java 的开发工具实在没有 Visual Studio .NET2003 好用,就连 VS6.0都赶不上。<呵呵>当然,Eclipse、NetBeans IDE也是不错的开发工具,俺也打算今后采用 Linux+Apache+MySQL作系统了。C#将不会是首选了。

下面把今天写的程序中,有用的几个列出了。
1-获取指定目录的信息(文件/文件夹)

import java.io.*;
import java.util.Date;
public class Alone13_1 {
public static void main(String[] args) throws IOException {
File f=new File(new BufferedReader(
new InputStreamReader(System.in)).readLine());
File []fs=f.listFiles();
for(int i=0;i<f.length();i++)
{
if(fs[i].isDirectory())
System.out.println("目录:"+fs[i]);
else
{
System.out.print("文件:"+fs[i]);
System.out.print(" 大小:"+fs[i].length()/1024+"Kbyte ");
if(fs[i].isHidden())System.out.print("隐藏属性:是 ");
else System.out.print("隐藏属性:是 ");
System.out.print("日期:"+
new Date(fs[i].lastModified()).toLocaleString()+"/n");
}
}
}
}

2-java中对象序列化并写入文件的例子(注意:未实现把对象从文件中读取,只实现了写入)
package lx;
import java.io.*;
public class P201_1 {
public static void main(String[] args) throws IOException {
student[] s=new student[100];
int n=0;
String fp="D://obj.data";
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(fp));
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String data=null;
System.out.println("/n请输入名字及国语,英语,数学,科学的成绩。(eof:输入完毕)");
while(true)
{
data=br.readLine();
if(data.equals("eof"))break;
s[n]=new student(data);
n++;
}
System.out.println("写入文件的对象为:");
for(int i=0;i<n;i++){
oos.writeObject(s[i]);
s[i].get(); }
oos.close();
System.out.println("已将"+n+"个对象写入文件!");
}
}
class student implements Serializable{
private static final long serialVersionUID = 5297152510487875285L;
String name;
int[] record=new int[4];
int total;
float avg;
String grade;
student(String data)
{
total=0;
avg=0;
set(data);
}
public void set(String data)
{
int i=data.indexOf(" ");
name=data.substring(0,i);
data=data.substring(i+1,data.length());
for(int k=0;k<4;k++)
{
int p=data.indexOf(" ");
if(p!=-1){
record[k]=Integer.parseInt(data.substring(0,p));
data=data.substring(p+1,data.length());
}
else
record[k]=Integer.parseInt(data);
total+=record[k];
}
avg=total/4;
}
public void get()
{
System.out.print("姓名:"+name+" 国语:"+record[0]+" 英语:"+
record[1]+" 数学:"+record[2]+" 科学:"+record[3]+"/n");
}
}

今天一共写了12道题目,不过觉得只有上面2个题目最有意思。其他的几乎是在重复n年前学习C语言时的作业题。也许我国的教育就是喜欢用新式武器去处理陈年芝麻吧。。。

小结:
从控制台输入数据的办法。(注意:java只能从控制台输入字符串,如果需要其他数据,需要自行转换)
例如:输入字符串(需导入 import java.io.*;)
String s=null;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
从控制台输入整数:
int n1=Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
System.out.println(n1);
对象的序列化需要加入关键字implements Serializable,并且在类体中加入
private static final long serialVersionUID = 5297152510487875285L;
(至于原因嘛,今天偷懒了,没有仔细研究,等明天好好看看书。呵呵)







分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics