私はcsv
ファイルのフォーマットはid,text
です。次に例を示します。See you later
Javaでは、ファイル内の行の特定の部分をどのように上書きするのですか?
に、
helloText,How are you
goodbyeMessage,Some new text for a change
errorMessage,Oops something went wrong
は、今私はSome new text for change
あるgoodbyeMessage
のテキスト部分を編集したいたとえば言うことができますcsv
の結果は、このようになります。
helloText,How are you
goodbyeMessage,See you later
errorMessage,Oops something went wrong
私はファイルに書き込むことができるコードを持っていますが、コードの実行が終了すると、これはcsv
ファイルになります:
helloText,How are you
goodbyeMessage,Some new text for a change
errorMessage,Oops something went wronggoodbyeMessage,See you later
FileWriterのappend
の値をtrue
に設定しているため、これが発生していることがわかりました。もし私がすべてを拭くのではない。
私はFileWriter.newLine()
を使って試してみましたが、それはより良く見えるようにしましたが、これは私が達成しようとしているものではありません。私はまだファイルに同じ数の行が必要です。 MyApp.java
public static void main(String[] args) throws FileNotFoundException, IOException {
PropsWriter pw = new PropsWriter("test_props.txt");
pw.updateElementText("goodbyeMessage", "See you later");
}
PropsWriter.java
/**
* Updates the text of a given element in the properties file.
*
* @param id The id of the element
* @param newText The text that will replace the original text.
*
* @throws IOException If I/O error occurs
*/
public void updateElementText(String id, String newText) throws IOException {
Assertions.checkNotNull(id, "Id must not be null.");
Assertions.checkNotNull(id, "Id must not be an empty string.");
File file = new File(pathName);
BufferedReader br = new BufferedReader(new FileReader(file));
BufferedWriter wr = new BufferedWriter(new FileWriter(file, true));
try {
String line;
while((line = br.readLine()) != null) {
if(line.contains(id)) {
//returns true
System.out.println("Is line there: " + line.contains(id));
//returns helloText
System.out.println("ID: " + extractId(line));
//returns How are you
System.out.println("TEXT: " + extractText(line));
//returns Some new text for a change
System.out.println("NEW_TEXT: " + newText);
// This is where I am trying to replace the old text
// with new text that came from the main method.
line = line.replaceAll(extractText(line), newText);
//wr.newLine();
wr.write(line);
}
}
} catch(IOException e) {
e.printStackTrace();
} finally {
wr.close();
}
}
/**
* Gets the id part of a line that is stored in the
* properties file.
*
* @param element The element the id is got from.
* @return String representation of the id.
*/
private static String extractId(String line) {
final int commaOccurence = getFirstCommaOccurrence(line);
return line.substring(0, commaOccurence);
}
/**
* Gets the text part of a line that is stored in the
* properties file.
*
* @param element The element the text is got from.
* @return String representation of the text.
*/
private static String extractText(String line) {
final int commaOccurence = getFirstCommaOccurrence(line);
return line.substring(commaOccurence + 1, line.length());
}
/**
* Gets the first occurrence of a comma in any given line of a text file.
* @param element
* @return
*/
private static int getFirstCommaOccurrence(String line) {
return line.indexOf(",");
}
あなたはただそれを言った。 'FileWriter'を' true'に設定しないでください(新しいものだけが追加されるため)。ファイル全体を読み込み、全ての行を保存する必要があります(例えば 'List')。次に、データを操作します。その後、**完全**テキストを書き換えます(例えば、上記の 'List 'を使用して)。しかし、どのラインを追加する必要があるかを正確に知っていれば(そしてその場所を見つける必要はありません)、もっと低レベルのFile-Writerがあなたのためにもっと良いかもしれません。 –
Zabuza
ファイルに何千もの行が含まれている場合、同じことをしますか? – cod3min3
行を検索する必要がある場合は、すべての行を繰り返し処理する必要があります。すべての行を保存する必要はありません(ファイルが大きい場合)。正確な場所を覚えて、より低レベルのFile-Writerを使用することもできます。たとえば、 'RandomAccessFile'の場合:https://examples.javacodegeeks.com/core-java/io/randomaccessfile/java-randomaccessfile-example – Zabuza