を読み込もうとしtextfile
ていて、フィルタリングを実行したいと思っています。そのために、を使用しBufferedReader
てファイルを読み取り、すべての行を読み取るためのString
呼び出し"line"
を作成しました。がline
特定のstring
(私の場合は「#」)で始まり、line
その後(次の行)が別の特定のString
(私の場合は「h」)で始まっていないかどうかを確認したいと思います。また、「次の行」が「h」で始まる場合は、その行を削除する必要があります。
だから基本的にはすべての線を均等にしたいです。
例:
これはテキストファイルです:
#line
ham
#line2
house
#line3
#line4
heart
#line5
hand
(この例では、line4を削除する必要があります)
私はこのようなことを試みました:
if (line.startsWith("#") && nextline != "h"){
// remove this line
// There is no definition for "nextline"
}
行を読むため:
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
position2string = String.valueOf(wrongpos + 1 + ".");
while ((line = br.readLine()) != null) {
if (line.startsWith("h")) {
lineStore.add(line);
}
if (line.startsWith("#")) {
line2 = line.replace("#EXTINF:-1,", "");
line3 = +richtigeposition + 1 + ". " + line2;
richtigeposition++;
namelist.add(line3);
arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.listitem, R.id.firsttext,
namelist);
mainlist.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
br.close();
} catch (Exception e) {
Log.e("MainAcvtivity", + e.toString());
}
編集(Tim Biegeleisenのおかげで、解決策を見つけましたが、NullPointerExceptionが発生します:仮想メソッド 'int java.lang.String.length()を呼び出そうとするとbw.write(last);
、この例外が発生します。申し訳ありませんが、プログラミングはまったく初めてです):
私の現在のコード:
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
BufferedWriter bw = new BufferedWriter(new FileWriter(filePath));
String line;
position2string = String.valueOf(wrongpos + 1 + ".");
while ((line = br.readLine()) != null) {
if (last != null && (!line.startsWith("h") || !lastPound)) {
bw.write(last);
}
lastPound = line.startsWith("#");
last = curr;
if (line.startsWith("h")) {
lineStore.add(line);
}
if (line.startsWith("#")) {
line2 = line.replace("#EXTINF:-1,", "");
line3 = +richtigeposition + 1 + ". " + line2;
richtigeposition++;
namelist.add(line3);
arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.listitem, R.id.firsttext,
namelist);
mainlist.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
br.close();
//Exeption is here ==> bw.write(last);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
あなたはおそらくあなたの時間といくつかのコード行を節約する別の方法でそれを行うことができます。すべてのファイル行をリストに入れると、それを操作するのがはるかに簡単になります。
List<String> lines = Files.readAllLines(Paths.get(*path_to_file*), Charset.defaultCharset());
ファイル行をリストに保存した後、それを繰り返して必要な行を検索できるようになります。正しい境界を指定することを避けるNullPointerException
かIndexOutOfBoundsException
、忘れないでください。
for (int i = 0; i < lines.size() - 1; i++) {
if (lines.get(i).startsWith("#") && !lines.get(i + 1).startsWith("h")) {
lines.remove(i+1);
}
}
その後、リストをファイルに保存できます。
Files.write(Paths.get(*path_to_file*), lines);
私はそれをテストし、出力は次のようになりました。
---更新---
IO操作を実行するにはScanner
、FileWriter
次を使用できます。
List<String> lines = new ArrayList<>();
Scanner s = new Scanner(new File("in.txt"));
while (s.hasNext()) {
lines.add(s.next());
}
s.close();
そして出力のために:
FileWriter writer = new FileWriter("out.txt");
for (String str : lines) {
writer.write(str + System.lineSeparator());
}
writer.close();
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加