1. 程式人生 > >刪除檔案中特定字串

刪除檔案中特定字串

java檔案中刪除特定字串
(1)java檔案讀取;尤其注意在向檔案寫的時候,每次呼叫PrintWriter的print方法,均會將檔案清空重寫,也就是覆蓋,因此需要先開設一個字元緩衝區。
(2)命令列呼叫java程式,兩種方式,一是直接在eclipse中呼叫,不需要加引號,多個字串直接用空行隔開;二是在命令列中呼叫,類名+字串引數,同樣不需要加引號,空格隔開。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

//從文字檔案中刪掉某個指定的字串
public class Replace { public static void main(String[] args) throws FileNotFoundException { // TODO Auto-generated method stub File filesource=new File(args[0]); //File filesource=new File("d://test1.txt"); Scanner input=new Scanner(filesource); StringBuffer temp=new
StringBuffer(); while(input.hasNext()) { String tem=input.nextLine(); temp.append(tem.replace("John", "")+"\r\n"); } PrintWriter output=new PrintWriter(filesource); output.print(temp); input.close(); output.close(); } }