最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Java中如何保存字符串到文件中且指定编码

Java crifan 1118浏览 0评论
折腾:
【未解决】用Java实现百度模拟登陆
期间,已经可以用OkHttp抓取百度主页的html字符串了。
现在想要保存字符串到文件中,且指定UTF-8编码。
java save string to file
How do I save a String to a text file using Java? – Stack Overflow
但是尝试去使用PrintWriter,结果
import PrintWriter;
提示找不到:
然后就去注释掉了:
//import PrintWriter;
然后在粘贴了示例代码后:
try (PrintWriter out = new PrintWriter("filename.txt")) {
    out.println(text);
}
智能提示,是不是java.io.PrintWriter:
然后就可以去导入真正的路径:
import java.io.PrintWriter;
然后就可以了。
然后再去试试代码:
try (PrintWriter out = new PrintWriter("baidu_com.html")) {
    out.println(respBodyStr);
}
调试看看结果
不过在此之前出错:
【已解决】OkHttp获取响应的body的字符串出错:Exception in thread “main” java.lang.IllegalStateException: closed
不过又出现了:
【已解决】OkHttp返回响应的字符串是乱码
终于可以考虑去保存得到的html的字符串了。
自己试了试:
import java.io.PrintWriter;

        CrifanUtil.saveStrToFile("debug/baidu_com.html", respBodyStr);

    public static void saveStrToFile(String fullFilePath, String strToSave) {
        try (PrintWriter printWriter = new PrintWriter(fullFilePath)) {
            printWriter.println(strToSave);
        }
    }
结果报错:
异常没有处理
Unhandled exception: java.io.FileNotFoundException
而去看源码,的确是有异常可能抛出:
算了,参考别人完整的解释,再去试代码:
Java Write to File – 4 Ways to Write File in Java – JournalDev
  1. FileWriter: FileWriter is the simplest way to write a file in java, it provides overloaded write method to write int, byte array and String to the File. You can also write part of the String or byte array using FileWriter. FileWriter writes directly into Files and should be used only when number of writes are less.
  2. BufferedWriter: BufferedWriter is almost similar to FileWriter but it uses internal buffer to write data into File. So if the number of write operations are more, the actual IO operations are less and performance is better. You should use BufferedWriter when number of write operations are more.
  3. FileOutputStream: FileWriter and BufferedWriter are meant to write text to the file but when you need raw stream data to be written into file, you should use FileOutputStream to write file in java.
  4. Files: Java 7 introduced Files utility class and we can write a file using it’s write function, internally it’s using OutputStream to write byte array into file.
然后
Commons IO – Download Apache Commons IO
下载得到:commons-io-2.6-bin.zip
解压得到:commons-io-2.6.jar
导入到IDEA中
结果期间遇到:
【间接解决】Java中的函数默认值
【总结】
目前用:
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.commons.io.FileUtils;

class CrifanUtil {
    public static void saveStrToFile(String fullFilePath, String strToSave) {
        Logger.debug("saveStrToFile: fullFilePath={}, strToSave={}", fullFilePath, strToSave);
        saveStrToFile(fullFilePath, strToSave, Charset.forName("UTF-8"));
    }

    public static void saveStrToFile(String fullFilePath, String strToSave, Charset strCharset) {
        Logger.debug("saveStrToFile: fullFilePath={}, strToSave={}, strCharset={}", fullFilePath, strToSave, strCharset);
        try{
            File destFile = new File(fullFilePath);
            FileUtils.write(destFile, strToSave, strCharset);
        } catch ( IOException err){
            Logger.error("Save file {} error {}", fullFilePath, err);
        }
    }
}

    CrifanUtil.saveStrToFile("debug/baidu_com.html", respBodyStr);
即可实现保存字符串到文件中:

转载请注明:在路上 » 【已解决】Java中如何保存字符串到文件中且指定编码

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.177 seconds, using 22.03MB memory