zoukankan      html  css  js  c++  java
  • javaIO流--Writer,Reader

    Writer

    /**
     *<li> Writer中定义的一个重要的方法:
     *        public void writer(String str)throws IOException;
     */        
    package com.java.demo;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.Writer;
    
    public class TestDemo {
        public static void main(String args[]) throws Exception{
            //设置文件路径
            File fl = new File("e:"+File.separator+"hello" + File.separator+"demo" +File.separator+"java.txt" );
            if(!fl.getParentFile().exists()){ //父目录不存在          
                fl.getParentFile().mkdirs();//创建父目录,但是不创建文件
            }
            String str = "努力不一定成功,但是不努力有可能成功!";
            //创建文件;
            Writer wt = new FileWriter(fl);
            wt.write(str); //接收字符串写入java.txt文件中
            wt.close();//关闭IO流
        }  
    }

    Reader

    /**
     *<li> Reader中定义的方法:
     *        读取全部内容到字节数组中:public int read(char[] ch)throws IOException ;
     */        
    package com.java.demo;
    import java.io.*;
    
    public class TestDemo {
        public static void main(String args[]) throws Exception{
            //设置文件路径
            File fl = new File("e:"+File.separator+"hello" + File.separator+"demo" +File.separator+"java.txt" );
            if(fl.exists()){ //如果文件存在
                Reader rd = new FileReader(fl);
                char ch[] =new char[1024];
                rd.read(ch) ;
                ch.clone();
                System.out.println(ch);
            }
        }  
    }
  • 相关阅读:
    UITextField最大字符数和最大字节数的限制
    Python profiling
    Glow Android 优化实践
    当 NSDictionary 遇见 nil
    TCP/IP详解2 学习笔记---mbuf
    行业代码获取最近代码
    词语、句子相似度比较
    从word得到表格数据插入数据库(6位行业代码)
    python遍历数组获取下标
    计算机浮点数表示
  • 原文地址:https://www.cnblogs.com/hu1056043921/p/7388010.html
Copyright © 2011-2022 走看看