字符串写入文件,读取文件到字符串

PHP

读取文件使用 file_get_contents()

 $respone = file_get_contents("d:/20200810/de_scan_002.xml");

写入,可以使用 error_log()

error_log($respone, 3, "d:/de_scan_result.txt");

Java

读取

    public static void readToBuffer(StringBuffer buffer, String filePath) throws IOException {
        InputStream is = new FileInputStream(filePath);
        String line; // 用来保存每行读取的内容
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        line = reader.readLine(); // 读取第一行
        while (line != null) { // 如果 line 为空说明读完了
            buffer.append(line); // 将读到的内容添加到 buffer 中
            buffer.append("\n"); // 添加换行符
            line = reader.readLine(); // 读取下一行
        }
        reader.close();
        is.close();
    }


    public static String readFile(String filePath) throws IOException {
        StringBuffer sb = new StringBuffer();
        readToBuffer(sb, filePath);
        return sb.toString();
    }

写入

    public static void writeFile(String path, String content) {
//        String str= content;
        FileWriter writer;
        try {
            writer = new FileWriter(path);
            writer.write(content);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

参考:
https://www.cnblogs.com/mafeng/p/5224723.html
https://blog.csdn.net/fuyuwei2015/article/details/44257639