zoukankan      html  css  js  c++  java
  • IO文件的读取与写入

    文件读取:

    private static String Data() {
            // 1.定义目标文件
            File srcFile = new File("D:/text.txt");
            // 2.创建一个流,指向目标文件
            InputStream is = null;
            StringBuffer data = new StringBuffer();
            try {
                is = new FileInputStream(srcFile);
                //3.创建一个用来存储读取数据的缓冲数组
                byte[]array = new byte[128];
                //4.循环往外流(count为每次读取数组中的有效字节总数)
                int count = is.read(array);
                // 5.循环打印
                
                while (count != -1) {
                    // 将byte[] -》 String
                    // 将byte数组读取到的有效字节转换成字符串
                    String string = new String(array, 0, count);
                    data.append(string);
                    count = is.read(array);
                }
                System.out.println(data.toString());
                return data.toString();
            } catch (IOException e) {
    
            }
            //System.out.println(postResault);
            return null;
        }

    文件写入:

    public static void main(String[] args) throws IOException {
            File file = new File("D:\EDI\a.txt");
            FileOutputStream outputStream = new FileOutputStream(file,true);
            OutputStreamWriter stream = new OutputStreamWriter(outputStream);
            BufferedWriter writer = new BufferedWriter(stream);
            List<String> list = new ArrayList<String>();
            list.add("123466788");
            list.add("qwersa");
            String line = null;
            for (int i = 0; i < list.size(); i++) {
                line = list.get(i);
                writer.write("哈哈
    我真是一个大帅哥");
                writer.newLine();
            }
            writer.close();
            stream.close();
            outputStream.close();
        }
  • 相关阅读:
    第一章
    第一章 计算机系统漫游
    hihocoder #1014 : Trie树
    第一章
    来个小目标
    poj 1056 IMMEDIATE DECODABILITY
    poj 2001 Shortest Prefixes
    __name__ 指示模块应如何被加载
    Python 常用函数time.strftime()简介
    CentOS安装beEF做XSS平台
  • 原文地址:https://www.cnblogs.com/bjsze/p/14355668.html
Copyright © 2011-2022 走看看