zoukankan      html  css  js  c++  java
  • java生成txt文件,读txt文件

    1.方法1

    public static void main(String[] args) {
            try {
                FileWriter fileWriter = new FileWriter("c:\Result.txt");
                String ss = "cName	cCode		createDate
    ";
                ss += "dbc券		111188		2017-05-14
    ";
                ss += "zc券		111199		2017-05-14
    ";
                fileWriter.write(ss);
                fileWriter.flush();
                fileWriter.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println(">>>>>>>>>>>>>>>>>>write end");
    
            try {
                FileReader fileReader = new FileReader("c:\Result.txt");
                BufferedReader br = new BufferedReader(fileReader);
                String s;
                while ((s = br.readLine()) != null) {
                    System.out.println(s);
                }
                fileReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        System.out.println(">>>>>>>>>>>>>>>>>>read end");
    }

    2.方法2

    public static void main(String[] args) {
            File f = new File("c:\temp.txt");
            OutputStream out = null;
            try{
                out = new FileOutputStream(f);
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }
            
            String ss = "cName		cCode		createDate
    ";
            ss += "dbc券		111188		2017-05-14
    ";
            ss += "zc券		111199		2017-05-14
    ";
            // 将字符串转成字节数组
            byte b[] = ss.getBytes();
            try
            {
                // 将byte数组写入到文件之中
                out.write(b);
                out.close();
            }catch (IOException e1){
                e1.printStackTrace();
            }
            
            
            System.out.println("write end>>>>>>>>>>>");
            // 以下为读文件操作
            InputStream in = null;
            try{
                in = new FileInputStream(f);
            }catch (FileNotFoundException e3){
                e3.printStackTrace();
            }
    
            // 开辟一个空间用于接收文件读进来的数据
            byte b1[] = new byte[1024];
            int i = 0;
            try{
                // 将b1的引用传递到read()方法之中,同时此方法返回读入数据的个数
                i = in.read(b1);
                in.close();
            }catch (IOException e4){
                e4.printStackTrace();
            }
    
            // 将byte数组转换为字符串输出
            System.out.println(new String(b1, 0, i));
            System.out.println("read end>>>>>>>>>>>");
    
        }    
  • 相关阅读:
    Apache-Shiro分布式环境配置(与redis集成)(转)
    springboot整合mybatis将sql打印到日志(转)
    springboot中logback打印日志(转)
    Spring Boot Junit单元测试
    玩转Spring Boot 自定义配置、导入XML配置与外部化配置
    Windows开机自启动位置
    木马开机启动的六种方法(记录)
    用Delphi开发视频聊天软件
    Delphi用Socket API实现路由追踪
    前端工程师应该都了解的16个最受欢迎的CSS框架
  • 原文地址:https://www.cnblogs.com/simpledev/p/6852787.html
Copyright © 2011-2022 走看看