输入输出流

package LIU;

import java.io.*;
public class TestFile3 {

    public static void main(String[] args) {
        //字节写入读出
        
        
        try{
            //写入
            FileOutputStream fos=new FileOutputStream("d:\test1.txt",true);
            
            //String str2="追加写入";
            String str="
大家下午好 ";
            fos.write(str.getBytes());//覆盖写入
            
            //追加写入
            //FileOutputStream(name,true)
            
            
            fos.close();
            //读出
            FileInputStream fis=new FileInputStream("d:\test1.txt");
            byte[]b=new byte[200];
            int i=fis.read(b);
            String str1=new String(b,0,i);
            System.out.println("读取内容:"+str1);
            fis.close();
            
        }catch(Exception e)
        {
            e.printStackTrace();
        }
        


package LIU;


import java.io.*;
public class TsetFile4 {

    public static void main(String[] args) {
        
        try {
            //读取
            FileReader fr=new FileReader("d:\test1.txt");
            char[]c=new char[200];
            
            int i=fr.read(c);
            String str=new String(c,0,i);
            System.out.println("读取内容:"+str);
            fr.close();
            //写入
            FileWriter fw=new FileWriter("d:\test1.txt",true);
            fw.write("
新写入的内容");
            fw.close();
            
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }



原文地址:https://www.cnblogs.com/1ming/p/5277972.html