java實(shí)現(xiàn)寫(xiě)入并保存txt文件的代碼詳解
更新時(shí)間:2020年02月05日 11:22:01 作者:V
在本篇文章里小編給大家整理了關(guān)于java實(shí)現(xiàn)寫(xiě)入并保存txt文件的代碼實(shí)例內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。
java如何實(shí)現(xiàn)寫(xiě)入并保存txt文件?
實(shí)例代碼如下:
package TEST;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class BufferedWriterDemo {
public static void main(String[] args) throws IOException {
write("E:\\1.txt"); //運(yùn)行主方法
}
public static void write(String path)
throws IOException {
//將寫(xiě)入轉(zhuǎn)化為流的形式
BufferedWriter bw = new BufferedWriter(new FileWriter(path));
//一次寫(xiě)一行
String ss = "測(cè)試數(shù)據(jù)";
bw.write(ss);
bw.newLine(); //換行用
//關(guān)閉流
bw.close();
System.out.println("寫(xiě)入成功");
}
}
java創(chuàng)建txt文件并存入內(nèi)容
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class txtExport {
private static String path = "D:/";
private static String filenameTemp;
public static void main(String[] args) throws IOException {
txtExport.creatTxtFile("你好");
txtExport.writeTxtFile("你好");
}
/**
* 創(chuàng)建文件
*
* @throws IOException
*/
public static boolean creatTxtFile(String name) throws IOException {
boolean flag = false;
filenameTemp = path + name + ".txt";
File filename = new File(filenameTemp);
if (!filename.exists()) {
filename.createNewFile();
flag = true;
}
return flag;
}
/**
* 寫(xiě)文件
*
* @param newStr
* 新內(nèi)容
* @throws IOException
*/
public static boolean writeTxtFile(String newStr) throws IOException {
// 先讀取原有文件內(nèi)容,然后進(jìn)行寫(xiě)入操作
boolean flag = false;
String filein = newStr + "\r\n";
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
// 文件路徑
File file = new File(filenameTemp);
// 將文件讀入輸入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存該文件原有的內(nèi)容
for (int j = 1; (temp = br.readLine()) != null; j++) {
buf = buf.append(temp);
// System.getProperty("line.separator")
// 行與行之間的分隔符 相當(dāng)于“\n”
buf = buf.append(System.getProperty("line.separator"));
}
buf.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
flag = true;
} catch (IOException e1) {
// TODO 自動(dòng)生成 catch 塊
throw e1;
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return flag;
}
}
以上就是本次介紹的全部相關(guān)知識(shí)點(diǎn),希望腳本之家整理的內(nèi)容能夠幫助到大家。
相關(guān)文章
JAVA實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷代碼
這篇文章主要介紹了JAVA實(shí)現(xiàn)的簡(jiǎn)單萬(wàn)年歷代碼,涉及Java日期操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
java網(wǎng)絡(luò)編程學(xué)習(xí)java聊天程序代碼分享
java聊天程序代碼分享,大家參考使用吧2013-12-12
springboot實(shí)現(xiàn)敏感字段加密存儲(chǔ)解密顯示功能
這篇文章主要介紹了springboot實(shí)現(xiàn)敏感字段加密存儲(chǔ),解密顯示,通過(guò)mybatis,自定義注解+AOP切面,Base64加解密方式實(shí)現(xiàn)功能,本文通過(guò)代碼實(shí)現(xiàn)給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02
postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù)
這篇文章主要介紹了postman中實(shí)現(xiàn)傳遞@RequestBody參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10

