java的io操作(將字符串寫入到txt文件中)
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
public class WriteStringToTxt {
public void WriteStringToFile(String filePath) {
try {
File file = new File(filePath);
PrintStream ps = new PrintStream(new FileOutputStream(file));
ps.println("http://www.dhdzp.com");// 往文件里寫入字符串
ps.append("http://www.dhdzp.com");// 在已有的基礎(chǔ)上添加字符串
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void WriteStringToFile2(String filePath) {
try {
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.append("在已有的基礎(chǔ)上添加字符串");
bw.write("abc\r\n ");// 往已有的文件上添加字符串
bw.write("def\r\n ");
bw.write("hijk ");
bw.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void WriteStringToFile3(String filePath) {
try {
PrintWriter pw = new PrintWriter(new FileWriter(filePath));
pw.println("abc ");
pw.println("def ");
pw.println("hef ");
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void WriteStringToFile4(String filePath) {
try {
RandomAccessFile rf = new RandomAccessFile(filePath, "rw");
rf.writeBytes("op\r\n");
rf.writeBytes("app\r\n");
rf.writeBytes("hijklllll");
rf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void WriteStringToFile5(String filePath) {
try {
FileOutputStream fos = new FileOutputStream(filePath);
String s = "http://www.dhdzp.coml";
fos.write(s.getBytes());
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String filePath = "E:\\link.txt";
// new WriteStringToTxt().WriteStringToFile(filePath);
// new WriteStringToTxt().WriteStringToFile2(filePath);
// new WriteStringToTxt().WriteStringToFile3(filePath);
// new WriteStringToTxt().WriteStringToFile4(filePath);
new WriteStringToTxt().WriteStringToFile5(filePath);
}
}
相關(guān)文章
SpringBoot異步調(diào)用方法實(shí)現(xiàn)場(chǎng)景代碼實(shí)例
這篇文章主要介紹了SpringBoot異步調(diào)用方法實(shí)現(xiàn)場(chǎng)景代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04
mybatis的坑-integer類型為0的數(shù)據(jù)if?test失效問題
這篇文章主要介紹了mybatis的坑-integer類型為0的數(shù)據(jù)if?test失效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
使用eclipse導(dǎo)入javaWeb項(xiàng)目的圖文教程
這篇文章主要介紹了如何使用eclipse導(dǎo)入別人的javaWeb項(xiàng)目,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
SpringBoot?Knife4j框架&Knife4j的顯示內(nèi)容的配置方式
Knife4j框架是基于Swagger2開發(fā)的在線API文檔生成工具,主要功能包括自動(dòng)生成API文檔、接口文檔展示、接口測(cè)試工具、接口權(quán)限控制和在線調(diào)試,該框架支持通過注解自動(dòng)生成詳細(xì)的接口文檔,開發(fā)者可以直接在文檔界面進(jìn)行接口測(cè)試和調(diào)試2024-09-09
使用java的HttpClient實(shí)現(xiàn)多線程并發(fā)
這篇文章主要介紹了使用java的HttpClient實(shí)現(xiàn)多線程并發(fā)的相關(guān)資料,需要的朋友可以參考下2016-09-09

