zoukankan      html  css  js  c++  java
  • 字节写入读出//字符读取写入//带缓存读取写入

     
     
     
    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();
            }
            
            
            
            
            
            
    
        }
    
    }
    View Code

    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();
            }
    
        }
    
    }
    View Code

    package LIU;
    
    import java.io.*;
    
    public class Huancunliu {
    
        public static void main(String[] args) {
    
    
            
            //带缓存,读
            //Writer 接口的实现类
            FileWriter fw;
            try {
                
                File f=new File("d:\test1.txt");
    //            fw = new FileWriter(f,true);
    //            
    //            //缓存写入类,构造时需要传入Writer实例
    //            BufferedWriter bw=new BufferedWriter(fw);
    //            
    //            bw.write("	这是清空之前写入的字符串");
    //            //自动管理缓存
    //            //自动写入:1,缓存满了。2,缓存关闭之前。
    //            
    //            bw.flush();//主动清空缓存,写入数据
    //            bw.write("	这是清空之后写入的字符串");
    //            bw.close();
    //            fw.close();  
    //            System.out.println("写入完毕");
                
                //缓存读取
                
                FileReader fr=new FileReader(f);
                BufferedReader br=new BufferedReader(fr);
                
                
                //前两次读
    //            String str=br.readLine();//读取一整行
    //            System.out.println(str);
    //            String str1=br.readLine();//接着读取下一行
    //            System.out.println(str1);
                
    //            while(str!=null)//判断读出的内容是不是空的
    //            {
    //                System.out.println(str);
    //                str=br.readLine();
    //            }
                
                while(true)
                {
                    String str=br.readLine();
                    if(str==null)
                    {
                        break;
                    }
                    System.out.println(str);
                }
            
            } catch (IOException e) {
                
                e.printStackTrace();
            }
            
            
            
    
        }
    
    }
    View Code

  • 相关阅读:
    java.util.concurrent学习
    mysql慢查优化总结
    mysql怎么限制某些查询语句的执行?
    数据库操作提交事务如果不关闭,会有什么样的后果?
    apache的500错误是写到哪个文件里面
    apache也可以做负载均衡,跟nignx的区别是什么?
    ajax提交请求为啥url要用这个函数encodeURI
    MySQL性能调优与架构设计读书笔记
    java枚举的作用
    linux的命令
  • 原文地址:https://www.cnblogs.com/1ming/p/5279891.html
Copyright © 2011-2022 走看看