Java
 Computer >> コンピューター >  >> プログラミング >> Java

Javaを使用して.txtファイルの行を上書きするにはどうすればよいですか?


使用されるAPI

replaceAll() Stringクラスのメソッドは、正規表現と置換文字列を表す2つの文字列を受け入れ、一致した値を指定された文字列に置き換えます。

java.utilクラス (コンストラクター)File、InputStream、Path、およびStringオブジェクトを受け入れ、正規表現を使用してトークンごとにすべてのプリミティブデータ型とStrings(指定されたソースから)を読み取ります。提供されているnextXXX()メソッドを使用して、ソースからさまざまなデータ型を読み取るため。

StringBuffer classはStringの変更可能な代替手段であり、このクラスをインスタンス化した後、append()メソッドを使用してデータを追加できます。

手順

ファイルの特定の行を上書きするには-

ファイルの内容を文字列に読み込む-

  • Fileクラスをインスタンス化します。

  • コンストラクターにパラメーターとしてファイルを渡すScannerクラスをインスタンス化します。

  • 空のStringBufferオブジェクトを作成します。

  • append()メソッドを使用して、ファイルの内容を1行ずつStringBufferオブジェクトに追加します。

  • toString()メソッドを使用してStringBufferをStringに変換します。

  • Scannerオブジェクトを閉じます。

replaceAll()を呼び出します 置換する行(古い行)と置換する行(新しい行)をパラメーターとして渡す、取得した文字列のメソッド。

ファイルの内容を書き換える-

  • FileWriterクラスをインスタンス化します。

  • append()メソッドを使用して、replaceAll()メソッドの結果をFileWriterオブジェクトに追加します。

  • flush()メソッドを使用して、追加されたデータをファイルにプッシュします。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class OverwriteLine {
   public static void main(String args[]) throws IOException {
      //Instantiating the File class
      String filePath = "D://input.txt";
      //Instantiating the Scanner class to read the file
      Scanner sc = new Scanner(new File(filePath));
      //instantiating the StringBuffer class
      StringBuffer buffer = new StringBuffer();
      //Reading lines of the file and appending them to StringBuffer
      while (sc.hasNextLine()) {
         buffer.append(sc.nextLine()+System.lineSeparator());
      }
      String fileContents = buffer.toString();
      System.out.println("Contents of the file: "+fileContents);
      //closing the Scanner object
      sc.close();
      String oldLine = "No preconditions and no impediments. Simply Easy Learning!";
      String newLine = "Enjoy the free content";
      //Replacing the old line with new line
      fileContents = fileContents.replaceAll(oldLine, newLine);
      //instantiating the FileWriter class
      FileWriter writer = new FileWriter(filePath);
      System.out.println("");
      System.out.println("new data: "+fileContents);
      writer.append(fileContents);
      writer.flush();
   }
}

出力

Contents of the file: Tutorials Point originated from the idea that there exists a 
class of readers who respond better to online content and prefer to learn new skills.
Our content and resources are freely available and we prefer to keep it that way to 
encourage our readers acquire as many skills as they would like to.
We don’t force our readers to sign up with us or submit their details either.
No preconditions and no impediments. Simply Easy Learning!

new data: Tutorials Point originated from the idea that there exists a class of readers 
who respond better to online content and prefer to learn new skills.
Our content and resources are freely available and we prefer to keep it that way to 
encourage our readers acquire as many skills as they would like to.
We don’t force our readers to sign up with us or submit their details either.
Enjoy the free content

  1. Javaを使用してディレクトリ階層を作成するにはどうすればよいですか?

    ファイルという名前のクラス java.ioパッケージのは、システム内のファイルまたはディレクトリ(パス名)を表します。このクラスは、ファイル/ディレクトリに対してさまざまな操作を実行するためのさまざまなメソッドを提供します。 mkdir() このクラスのメソッドは、現在のオブジェクトによって表されるパスでディレクトリを作成します。 ディレクトリ階層の作成 新しいディレクトリの階層を作成するには、メソッド mkdirs()を使用できます。 同じクラスの。このメソッドは、存在しない親ディレクトリを含む、現在のオブジェクトによって表されるパスでディレクトリを作成します。 例 import ja

  2. Javaを使用してJSONファイルを作成/作成する方法は?

    JSONまたはJavaScriptObjectNotationは、人間が読める形式のデータ交換用に設計された、軽量のテキストベースのオープンスタンダードです。 JSONで使用される規則は、C、C ++、Java、Python、Perlなどを含むプログラマーに知られています。サンプルJSONドキュメント − {    "book": [       {          "id": "01",