import java.io.*;
public class Txt {
public static void main(String args[]) {
createFile("E://123.txt");
writeFileOverlay("E://123.txt", "123");
writeFileAppend("E://123.txt", "456\r\n789");
readFile("E://123.txt");
}
/**
* 功能描述:
* <创建文件>
*
* @param filePath 1
* @return void
* @author zhoulipu
* @date 2019/8/2 16:23
*/
private static void createFile(String filePath) {
try {
File file = new File(filePath);
// 判断文件是否存在
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 功能描述:
* <追加写入>
*
* @param filePath 1
* @param Content 2
* @return void
* @author zhoulipu
* @date 2019/8/2 16:23
*/
private static void writeFileAppend(String filePath, String Content) {
FileWriter fw = null;
try {
fw = new FileWriter(filePath, true);
fw.write(Content);
fw.flush();
fw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.flush();
fw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 功能描述:
* <覆盖写入>
*
* @param filePath 1
* @param Content 2
* @return void
* @author zhoulipu
* @date 2019/8/2 16:23
*/
private static void writeFileOverlay(String filePath, String Content) {
PrintWriter pw = null;
try {
pw = new PrintWriter(filePath);
pw.write(Content);
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.flush();
pw.close();
}
}
}
/**
* 功能描述:
* <读取文件>
*
* @param filePath 1
* @return void
* @author zhoulipu
* @date 2019/8/2 16:24
*/
private static void readFile(String filePath) {
InputStreamReader in = null;
BufferedReader br = null;
try {
File file = new File(filePath);
in = new InputStreamReader(new FileInputStream(file), "gbk");
br = new BufferedReader(in);
String s;
// 逐行读取
while ((s = br.readLine()) != null) {
System.out.println(s);
}
br.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Java文件处理:Txt 文本创建、写入和读取
最新推荐文章于 2022-05-11 17:21:52 发布