java IO File按行读写追加
Posted 老李笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java IO File按行读写追加相关的知识,希望对你有一定的参考价值。
java IO File按行读写追加
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class FileUtil2
public static void main(String[] args)
FileUtil2.write("d://wwwc", "ooooooooooooooo");
FileUtil2.read("d://wwwc");
public static void read(String path)
try (FileInputStream inputStream = new FileInputStream(path);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));)
String line = null;
while ((line = bufferedReader.readLine()) != null)
System.out.println(line);
catch (Exception e)
e.printStackTrace();
// 以追加的方式将文本输出到本地文件中
public static void write(String path, String content)
File file = new File(path);
if (!file.exists())
try
file.createNewFile();
catch (Exception e)
File parentFile = file.getParentFile();
parentFile.mkdirs();
try
file.createNewFile();
catch (Exception e2)
e2.printStackTrace();
try (FileOutputStream fos = new FileOutputStream(file, true);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");)
osw.write(content);
osw.write("\\r\\n");
catch (Exception e)
e.printStackTrace();
以上是关于java IO File按行读写追加的主要内容,如果未能解决你的问题,请参考以下文章