java IO文件读写例子(OutputStream,InputStream,Writer,Reader)

时间:2022-03-11 10:01:03

一,File创建文件

File file = new File("D:" + File.separator + "yi.txt"); 

    代码示例:

    

java IO文件读写例子(OutputStream,InputStream,Writer,Reader)java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
package com.hbut.io;

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

public class FileDemo {   
   
    public static void main(String[] args) {   
        File file = new File("D:" + File.separator + "yi.txt");   
        if (file.exists()) {   
            file.delete();  
            System.out.println("文件已存在");
        } else {   
            try {  
                boolean states=false;
                states = file.createNewFile(); 
                if(states==true)
                {
                    System.out.println("文件创建成功");
                }
            } catch (IOException e) {   
                // TODO Auto-generated catch block   
                e.printStackTrace();   
            }   
        }   
    }   
}  
View Code

 

二,OutputStream(字节流)写入文件

out= new FileOutputStream(file);//获取实际的字节流输出对象,内容覆盖

String info="hello";//要输入的内容 

byte[] b=info.getBytes();//将字符转化为字节数组

out.write(b);

     代码示例

java IO文件读写例子(OutputStream,InputStream,Writer,Reader)java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
package com.hbut.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class OutputDemo {   
    public static void main(String[] args) {   
        File file = new File("D:" + File.separator + "yi.txt");//指定要写入的文件   
        OutputStream out=null;//定义字节流输出对象   
        try {      
            out= new FileOutputStream(file);//获取实际的字节流输出对象,内容覆盖   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        }   
        String  info="hello";//要输入的内容   
        byte[] b=info.getBytes();//将字符转化为字节数组   
        try {   
            out.write(b);   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        try {   
            out.close();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }   
}  
View Code

 

三,InputStream(字节流)读出文件

InputStream In = null;// 定义字节流输入对象

In = new FileInputStream(file);// 获取实际的字节流输入对象

byte[] b = new byte[1024];// 开辟空间,读取内容

len = In.read(b);// 读取

System.out.println(new String(b, 0, len));//输出读取的内容

    代码示例

java IO文件读写例子(OutputStream,InputStream,Writer,Reader)java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
package com.hbut.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
    public static void main(String[] args) {
        File file = new File("D:" + File.separator + "yi.txt");// 指定要读取的文件
        InputStream In = null;// 定义字节流输入对象
        try {
            // out= new FileOutputStream(file,true);//是否字节追加函数
            In = new FileInputStream(file);// 获取实际的字节流输入对象
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        int len = 0;// 输入数组长度
        byte[] b = new byte[1024];// 开辟空间,读取内容
        // byte[] b=new byte[(int)file.length()];//根据文件大小开辟空间
        try {
            len = In.read(b);// 读取
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        try {
            In.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(new String(b, 0, len));
    }
}
View Code

 

四,Writer(字符流)写入文件

Writer write = null;// 定义字符输出流

write = new FileWriter(file);

String infor = "hello,WriterDemo";//即将写入文件的信息

write.write(infor);//写入文件

     代码示例

java IO文件读写例子(OutputStream,InputStream,Writer,Reader)java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
package com.hbut.io;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {
    public static void main(String[] args) {
        File file = new File("D:" + File.separator + "WriterDemo.txt");// 指定要写入的文件
        Writer write = null;// 定义字符输出流
        try {
            write = new FileWriter(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String infor = "hello,WriterDemo";
        try {
            write.write(infor);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            write.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
View Code

 

五,Reader(字符流)读出文件内容

Reader read = null;// 定义字符输入流   

read = new FileReader(file); 

char[] b=new char[1024];//设置字符的长度 

int len=read.read(b);//文件内容读入到 b[] 

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

    代码示例

java IO文件读写例子(OutputStream,InputStream,Writer,Reader)java IO文件读写例子(OutputStream,InputStream,Writer,Reader)
package com.hbut.io;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class ReaderDemo {   
    public static void main(String[] args) {   
        File file = new File("D:" + File.separator + "yi.txt");// 指定要读出的文件   
        Reader read = null;// 定义字符输入流   
        try {   
            read = new FileReader(file);   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        char[] b=new char[1024];//设置字符的长度   
        try {   
            int len=read.read(b);   
            System.out.println(new String(b));
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
        try {   
            read.close();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }   
    }   
}  
View Code