Java IO文件流

创建文件

 
//add file
       File file =new File("E:\java用文件","file.txt");
       try{
           file.createNewFile();
           System.out.println("creat ok!");
       }
       catch (IOException e){
           System.out.println("creat go die!");
           e.printStackTrace();
       }

 

  


 删除文件

 
//delete
file.delete();
 

  

 


读取文件数据
 
//way first
File file =new File("E:\java用文件","file.txt");

                InputStream input = null;
                byte b[] = new byte[(int)file.lenght()];
                                            //根据文件的大小定义数组空间

                try {
                    input = new FileInputStream(file);

                    input.read(b);
                    input.close();

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

                }


//way second    常用
File file =new File("E:\java用文件","file.txt");

                InputStream input = null;
                byte b[] = new byte[(int)file.length()];

                int temp = 0;
                int len = 0;
                try{
                    input = new FileInputStream(file);
                                 //使用FileInputStream为InputStream实例化
                    while((temp = input.read())!= -1)
                    {            //使用while循环读取文件内容,到达结尾是temp=-1
                        b[len] = (byte)temp;
                        len++;
                    }
                    input.close();        //关闭输入流
                }catch(IOException ex){   //捕捉异常
                    ex.printStackTrace();

                }
                System.out.println(new String(b));
 

  



向文件写入内容
 
//会覆盖文件原本内容
File file =new File("E:\java用文件","file.txt");
                OutputStream out = null;        //声明 OutputStream对象
                String str = "Hello";
                byte b[] = str.getBytes();      //把字符串转换为b数组
                try{
                    out = new FileOutputStream(file);
                                               //用 FileOutputStream实例化OutputStream
                    out.write(b);              //写入数据
                    out.close();               //关闭输出流
                    System.out.println("write ok!");
                }catch (IOException ev){       //捕捉异常
                    System.out.print("write go die!");
                    ev.printStackTrace();      //打印异常
                }



//在末尾追加内容
//将传入FileOutputStream 构造方法中的参数append赋值为true即可
//public FileOutputStream(File dile,boolean append)throws
//FileNotFoundException
 
File file =new File("E:\java用文件","file.txt");
                OutputStream out = null;        //声明 OutputStream对象
                String str = "Hello";
                byte b[] = str.getBytes();      //把字符串转换为b数组
                try{
                    out = new FileOutputStream(file,true);
                                               //用 FileOutputStream实例化OutputStream,添加true!!!
                    out.write(b);              //写入数据
                    out.close();               //关闭输出流
                    System.out.println("write ok!");
                }catch (IOException ev){       //捕捉异常
                    System.out.print("write go die!");
                    ev.printStackTrace();      //打印异常
                }


//若想换行,可以在需要换行的字符串处添加转义字符"
"即可

  

 


字符流

读取文件内容
 
File file =new File("E:\java用文件","file.txt");

                Reader read = null;       //声明Reader对象
                char c[] = new char[(int)file.length()];
                try
                {
                    read = new FileReader(file);    //使用FileReader实例化Reader
                    read.read(c);
                    read.close();
                }catch (IOException ee){
                    System.out.println("read go die !");
                    ee.printStackTrace();
                }

                System.out.println(c);

  



使用while方法读取
   
 File file =new File("E:\java用文件","file.txt");

                Reader read = null;       //声明Reader对象
                char c[] = new char[(int)file.length()];
                int temp = 0;
                int len = 0;
                try
                {
                    read = new FileReader(file);    //使用FileReader实例化Reader
                    while((temp = read.read())!=-1)
                    {                     //用while读取 到达结尾时temp = -1
                        c[len] = (char)temp; 
                        len++;
                    }
                    read.close();
                }catch (IOException ee){
                    System.out.println("read go die !");
                    ee.printStackTrace();
                }

                System.out.println(c);
 

  


写入
 
//way1
File file =new File("E:\java用文件","file.txt");

                Writer out = null;
                String str = "lllllll
";
                char c[] = str.toCharArray();
                try{
                    out = new FileWriter(file,true);
                    out.write(c);
                    out.close();
                    System.out.println("write  ok!");
                }catch (IOException ee) {
                    System.out.print("write  go die!");
                    ee.printStackTrace();
                }


//way2
File file =new File("E:\java用文件","file.txt");

                Writer out = null;
                String str = "lllllll
";

                try{
                    out = new FileWriter(file,true);
                    out.write(str);
                    out.close();
                    System.out.println("write  ok!");
                }catch (IOException ee) {
                    System.out.print("write  go die!");
                    ee.printStackTrace();
                }
 

  



字节流
字节输入流
   
File file = new File ("E:\java用文件","file.txt");

                InputStream input = null;    //声明InputStream对象
                byte b[]=new byte[(int)file.length()];

                int temp = 0;
                int len = 0;
                try{
                    input = new FileInputStream(file);
                                      //使用FileInputStream为  InputStream实例化
                    while((temp=input.read())!=-1)
                    {
                        b[len]=(byte)temp;
                        len++;

                    }
                    input.close();
                }catch(IOException eq){
                    eq.printStackTrace();

                }
                System.out.println(new String(b));
 

  


字节输出流
 
File file = new File ("E:\java用文件","file.txt");

                OutputStream out = null;

                String str = "el psy congroo";
                byte b[] = str.getBytes();
                try{
                    out = new FileOutputStream(file,true);

                    out.write(b);
                    out.close();
                    System.out.println("write   ok!!!");
                }catch(IOException ew){
                    ew.printStackTrace();
                    System.out.println("write go die");

                }

  



对象序列化
对象序列化是将对象状态转换为可保持或传输的格式的过程
若需要某个对象能支持序列化机制,必须实现Serializable接口


 
class student implements Serializable{

    String number;
    String name;
    String sex;
    String birthday;
    String department;

    public student(String number,String name,String sex,String birthday,String department){
        this.number = number;
        this.name = name;
        this.sex = sex;
        this.birthday = birthday;
        this.department = department;


    }

}
 

  


对象输出流

 
File file =new File("E:\java用文件","file.txt");

                String name = jtname.getText();
                String number = jtnumber.getText();
                String sex = jtsex.getText();
                String birthday = jtbirthday.getText();
                String department = jtdepartment.getText();

                student stu = new student(number,name,sex,birthday,department);

                FileOutputStream fout = null;
                ObjectOutputStream oos = null;
                try{
                    fout = new FileOutputStream(file,true);  //实例化文件输出流
                    oos = new ObjectOutputStream(fout);      //实例化对象输出流
                    oos.writeObject(stu);                    //把对象写入输出流
                    oos.close();                             //关闭对象输出流
                }catch (IOException er){
                    er.printStackTrace();
                    System.out.println("writeobject go die");
                }
 

  


对象输入流

 
File file =new File("E:\java用文件","file.txt");
                FileInputStream fis = null;
                ObjectInputStream ois = null;
                try{
                    fis = new FileInputStream(file);
                    ois = new ObjectInputStream(fis);
                    student stu = (student)ois.readObject();   //读取stu对象需要强制转换

                    ois.close();


                    jname.setText(stu.name);
                    jsex.setText(stu.sex);
                    jbirthday.setText(stu.birthday);
                    jdepartment.setText(stu.department);


                }catch (IOException ex){     //捕捉IOException异常
                    ex.printStackTrace();

                }catch(ClassNotFoundException we){    //捕捉ClassNotFoundException异常
                    we.printStackTrace();

                }

  

 

原文地址:https://www.cnblogs.com/Hyouka/p/5702109.html