IO文件

在Windows下的路径分隔符和Linux下的路径分隔符是不一样的,当直接使用绝对路径时,跨平台会暴出“No such file or diretory”的异常。

 

Separator:

比如说要在temp目录下建立一个test.txt文件,在Windows下应该这么写:
File file1 = new File ("C:\tmp\test.txt");
在Linux下则是这样的:
File file2 = new File ("/tmp/test.txt");

如果要考虑跨平台,则最好是这么写:
File myFile = new File("C:" + File.separator + "tmp" + File.separator, "test.txt");

File类有几个类似separator的静态字段,都是与系统相关的,在编程时应尽量使用。

package cn.mndl;
import java.io.File ;
import java.io.IOException ;

public class Hello {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         File f = new File ("E:" + File.separator + "test.txt") ;
         if(f.exists()){
             f.delete() ;
             System.out.println("删除成功") ;
         } else {
             try{
                 f.createNewFile() ;
                 System.out.println("name of teh currect file :" + f.getName()) ;
                 System.out.println("parents director of the currect file :" + f.getParent()) ;
                 System.out.println("full path of currect file :" +f.getPath()) ;
                 System.out.println("is readable ? :"+f.canRead());
                 System.out.println("is writable ?" + f.canWrite());
             } catch (IOException e){
                 e.printStackTrace();
                 
             }
         }
    }

}

 

package cn.mndl;
import java.io.BufferedReader;
import java.io.File ;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException ;

public class Hello {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try{
            //第一步,使用FILE类定义一个文件
            File inFile = new File("in.txt") ;
            File outFile = new File("out.txt") ;
            //第二步,用一个字节流或者字符流的子类进行流对象 的实例化
            FileReader fi  = new FileReader(inFile) ;
            BufferedReader bfi = new BufferedReader(fi) ;
            FileWriter fo = new FileWriter(outFile);
            //第三步,读写操作
            String  l = "" ;
            String [] arrs = null ;
            while((l = bfi.readLine())!= null){
                arrs = l.split(",") ;
                System.out.println(arrs[0]);
            }
            //第四步,关闭字节或者字节流
            fi.close();
            bfi.close();
            fo.close();    
        } catch (IOException e){
            e.printStackTrace() ;
        }
        
         
    }

}

 

 

对象流

package cn.mndl;
import java.io.BufferedReader;
import java.io.File ;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException ;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Student implements Serializable {
    private int id ;
    private String name ;
    public Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public String toString(){
        return "id: " + this.id + ", name : " + this.name;
    }
    
}
public class Hello {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        Student stu = new Student(20,"zhuopeng") ;
        Student s ;
        try{
        FileOutputStream out = new FileOutputStream("123.txt") ;//输出到文件
        ObjectOutputStream so = new ObjectOutputStream(out) ;
        so.writeObject(stu) ;
        so.close();
        out.close();
        
        
        FileInputStream in = new FileInputStream("123.txt") ;//从文件中读入
        ObjectInputStream io = new ObjectInputStream(in) ;
        s = (Student) io.readObject() ;
        System.out.println(s) ;
        
        } catch(FileNotFoundException e){
            e.printStackTrace() ;
        } catch(Exception e){
            e.printStackTrace();
        }
    }

}

 

posted @ 2016-01-14 21:57  式微胡不归  阅读(237)  评论(0编辑  收藏  举报