当前位置: 代码迷 >> Java Web开发 >> [分享]io操作小例子
  详细解决方案

[分享]io操作小例子

热度:295   发布时间:2006-12-07 13:10:52.0
[分享]io操作小例子


import java.io.File;
import java.io.IOException;
class FileDemo{
public static void main(String args[]){
try{
File file=new File("file1.txt");

System.out.println("文件是否存在: "+file.exists());

System.out.println("是文件吗: "+file.isFile());

System.out.println("是文件夹吗: "+file.isDirectory());

System.out.println("是否隐藏: "+file.isHidden());

System.out.println("名称: "+file.getName());
System.out.println("标准文件名: "+file.getCanonicalFile());

System.out.println("相对路径: "+file.getPath());
System.out.println("绝对路径: "+file.getAbsolutePath());
System.out.println("标准路径: "+file.getCanonicalPath());

System.out.println("最后修改时间: "+file.lastModified());

System.out.println("文件大小: "+file.length()+" 字节");


}

catch(IOException ex){
ex.printStackTrace();
}
}
}

以下是部分代码
package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import java.util.StringTokenizer;
public class MainClass {

public static void main(String[] args) {
try {
FileReader fr = new FileReader("F:\\JSP\\Hibernate\\shoptest\\test\\file1.txt");
BufferedReader br = new BufferedReader(fr);
String temp = null;
List result=new ArrayList();
do {
temp = br.readLine();
if(temp==null)break;
String shop[]=temp.split("#");

StringTokenizer first=new StringTokenizer(shop[0],"*");
Item item=new Item();
item.setShopId(first.nextElement().toString());
item.setTime(first.nextElement().toString());
List list=new ArrayList();
for(int i=1;i<shop.length;i++){
Order order=new Order();
StringTokenizer last=new StringTokenizer(shop[i],"*");
order.setId(last.nextElement().toString());
order.setCount(new Integer(Integer.parseInt(last.nextElement().toString())));
order.setPrice(new Integer(Integer.parseInt(last.nextElement().toString())));
list.add(order);
}
item.setList(list);

result.add(item);

} while (temp != null);
fr.close();
br.close();

display(result);
} catch (IOException ex) {
ex.printStackTrace();
}
}
public static void display(List result){
for(int i=0;i<result.size();i++){
Item item= (Item)result.get(i);
System.out.println("shopid : "+item.getShopId());
System.out.println("time : "+item.getTime());
System.out.println("sum : "+item.getSum());
List temp=item.getList();
for(int j=0;j<temp.size();j++){
Order order=(Order)temp.get(j);
System.out.println("id : "+order.getId()+" count : "+order.getCount()+" price : "+order.getPrice()+" total : "+order.getTotal());
}
}
}

}
线程
import java.lang.Runnable;
import java.lang.InterruptedException;
//import java.lang.Thread;
class Test1 implements Runnable{
public void run(){
try{
for(int i=0;i<10;i++){
System.out.println("在做第1件事情。。。");
Thread.sleep(500);
}
System.out.println(" 1..Over ***********************");
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
class Test2 implements Runnable{
public void run(){
try{
for(int i=0;i<10;i++){
System.out.println("在做第2件事情。。。");
Thread.sleep(500);
}
System.out.println(" 2..Over ***********************");
}
catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
public class RunnableDemo{
public static void main(String args[]){
Test1 t1=new Test1();
Test2 t2=new Test2();
Thread thread1=new Thread(t1);
Thread thread2=new Thread(t2);
thread1.start();
thread2.start();
}
}

[此贴子已经被作者于2006-12-31 15:13:48编辑过]

搜索更多相关主题的帖子: 例子  分享  

----------------解决方案--------------------------------------------------------

不错,学习一下。


----------------解决方案--------------------------------------------------------
  相关解决方案