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();
        }
  • 相关阅读:
    浅谈SharePoint 2013 站点模板开发 转载自http://www.cnblogs.com/jianyus/p/3511550.html
    SharePoint 入门书籍推荐 转载来源http://www.cnblogs.com/jianyus/p/3513238.html
    php简易计算器
    php的常量
    php数据类型的转换
    php的date()函数
    php流程控制语句
    php的运算符
    php简介
    手机页面操作栏的创建及WebFont的使用
  • 原文地址:https://www.cnblogs.com/bjsze/p/14355668.html
Copyright © 2011-2022 走看看