導航:首頁 > 編程語言 > java替換文件內容

java替換文件內容

發布時間:2022-05-12 05:06:28

java 如何修改文件的某一行內容

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileNotFoundException;
importjava.io.FileReader;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.PrintWriter;
publicclassDay02_B{
staticStringpath="K:/Test/Name.txt";//路徑
publicstaticvoidmain(String[]args){
FilefileText=newFile(path);//文件
if(fileText.canExecute())//如果文件存在就繼續
setText(fileText,"剛","xx");//「剛」指定改為:「XX」
}
privatestaticvoidsetText(FilefileText,Stringtarget,Stringsrc){//修改
BufferedReaderbr=null;
PrintWriterpw=null;
StringBufferbuff=newStringBuffer();//臨時容器!
Stringline=System.getProperty("line.separator");//平台換行!
try{
br=newBufferedReader(newFileReader(fileText));
for(Stringstr=br.readLine();str!=null;str=br.readLine()){
if(str.contains(target))
str=str.replaceAll(target,src);
buff.append(str+line);
}
pw=newPrintWriter(newFileWriter(fileText),true);
pw.println(buff);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!=null)
try{
br.close();
}catch(IOExceptione){
e.printStackTrace();
}
if(pw!=null)
pw.close();
}
}
}

② java怎樣高效修改文件部分內容

整體思路如下:
1、用 FileInputStream 讀取文件內容;
2、修改內容,string操作;
3、用 FileOutputStream 寫文件內容;
參考例子如一下:
import java.io.*;

public class TestBufferStream{
public static void main(String[] args){
try{
BufferedReader in = new BufferedReader(new FileReader("in.txt"));
BufferedWriter out = new BufferedWriter(new FileWriter("out.txt"));
String s = null;
while((s = in.readLine()) != null){
out.write(s);
out.newLine();
}
out.flush();
in.close();
out.close();
}catch(IOException e){
e.printStackTrace();
}

}
}

③ Java中怎樣找到文件中指定字元串並替換

java的String類中使用Replace方法可以將字元串中的特定字元或者文字替換成為我們想要的內容。

下面我們就用實例說明下Replace的用法。如何替換文字、特殊字元、以及如何替換第一個匹配對象。
1.定義一個類文件StringReplace.java

2.類內容如下:
public class StringReplace
{
public static void main(String[] args){
String info = "百d度,經3驗,歡迎H你";
info = info.replace(',',':');//將字元串,替換成":"
System.out.println(info);//替換後輸出
info=info.replace("歡迎","需要");//將歡迎二字換成需要
System.out.println(info);//替換後輸出
info=info.replaceAll("[0-9a-zA-Z]","\\$");//使用正則表達式將數字字母替換為$
System.out.println(info);//輸出結果
info = info.replaceFirst("\\$","#"); //使用正則表達式將第一個$替換為#
System.out.println(info);//輸出結果
}
}

3.下面我們就可以直接在命令行中用java命令或java運行環境來編譯運行上面的代碼啦。希望對java初學者有幫助。

④ java io怎麼替換原文件的內容

  1. 先讀取文件內容

    通過工具類FileUtils.readFileasString(Filename)存儲至變數中filestr

  2. 替換變數中的文件內容 filestr.replace(元字元串,替換字元串)

  3. 寫回源文件修改後的內容

    通過工具類FileUtils.writeStringToFile(file, data);

  4. 需要導入commons-io的jar包

⑤ java 文本文件部分內容修改

1.把整個TXT文件讀取出來(FileReader)存到一個String對象里!
2.正則替換String對象,替換表達式。比如
String
a
=
"13730000666";
String
b
=
a.replaceAll("(\\d{4})(\\d{4})(\\d{3})","$1****$3");
System.out.println(b);
輸出:
1373****666
3.再將String對象寫回TXT文件。

⑥ java替換文件中的內容 回答滿意追加100分

文件內容不是很大的話,替換用replace

String s=readfile(file) ;//讀取出文件的內容,readfile方法可以自己寫

String s2=s.replace("</tabs>","你想替換的字元串");

s2就是你要的結果

如果是追加
先得到</tabs>的索引位置,方法是indexOf("</tabs>") 比如結果是x,然後
int x=indexOf("</tabs>");
s2=s.subString(0,x)+"你要追加的內容"+s.subString(x,-1);

你懂正則表達式的話也可以用正則表達式

⑦ java替換文件指定內容 替換c:\1.txt里的指定內容然後輸出到d:\2.txt怎麼實現

首先看c中是否有1.txt,如果有,就讀取它的內容,
然後替換修改保存。
其次檢查d中是否有2.txt,如果有,就將它的內容修改為剛才替換修改的即可。

⑧ java怎麼修改文本文件每行里的某一內容

1.把整個TXT文件讀取出來(FileReader)存到一個String對象里!
2.正則替換String對象,替換表達式。比如
String
a
=
"13730000666";
String
b
=
a.replaceAll("(\\d{4})(\\d{4})(\\d{3})","$1****$3");
System.out.println(b);
輸出:
1373****666
3.再將String對象寫回TXT文件。

⑨ java中替換文本內容,使用傳過來的字元串變數,只知道該字元串在文本中獨佔一行,請問如何查找替換他

用bufferedReader進行讀取,按行讀,String str=「」;
while ((str=bf.readLine())!=null){
if(str.equals(targetStr)){
//進行操作。

}

}
然後將str與傳過來的字元串變數進行比較,看是否相同。
然後按你的規則進行查找替換。

⑩ java 替換文件內容

代碼如下:
/***
* 方法:
* @Title: replaceContentToFile
* @Description: TODO
* @param @param path 文件
* @param @param str 開始刪除的字元
* @param @param con 追加的文本
* @return void 返回類型
* @throws
*/
public static void replaceContentToFile(String path, String str ,String con){
try {
FileReader read = new FileReader(path);
BufferedReader br = new BufferedReader(read);
StringBuilder content = new StringBuilder();

while(br.ready() != false){
content.append(br.readLine());
content.append("\r\n");
}
System.out.println(content.toString());
int dex = content.indexOf(str);
if( dex != -1){
System.out.println(content.substring(dex, content.length()));
content.delete(dex, content.length());
}
content.append(con);
br.close();
read.close();
FileOutputStream fs = new FileOutputStream(path);
fs.write(content.toString().getBytes());
fs.close();

} catch (FileNotFoundException e) {
e.printStackTrace();

} catch (IOException e){
e.printStackTrace();

}

}

閱讀全文

與java替換文件內容相關的資料

熱點內容
手機號碼如何加密 瀏覽:424
沈陽程序員培訓學校 瀏覽:538
一般伺服器如何配置 瀏覽:895
圖片怎樣加密發郵件 瀏覽:619
萬虹電腦文件夾密碼忘記了怎麼辦 瀏覽:631
rc108單片機 瀏覽:867
戰雷如何改變伺服器 瀏覽:674
mactelnet命令 瀏覽:51
壓縮袋壓縮了拿出來 瀏覽:401
安卓手機相機怎麼設置許可權 瀏覽:121
美女程序員轉行做主播 瀏覽:671
辦理解壓房產 瀏覽:575
道路工程概論pdf 瀏覽:388
超棒數學速演算法大全 瀏覽:937
小米易語言登錄源碼 瀏覽:31
磚牆內加密鋼筋 瀏覽:992
鄉關何處pdf 瀏覽:84
小豬領贊小程序源碼 瀏覽:336
python曲線如何原路返回 瀏覽:430
pdf快速看圖破解版 瀏覽:294