私が書いたコードは.CSVファイルではなく、.txtだけで動作します。私の.CSVファイルに書き込めません
私のコードの目的は、field1からユーザー入力を受け取り、ファイル内にユーザー入力のインスタンスがあるかどうかを確認するために.CSVファイルをチェックすることです。存在する場合、フィールド2からのユーザー入力に置き換えられます。
これは私の.txtファイルでは動作しますが、.CSVファイルでは動作しません。ここで
は、ボタンを押すだけで活性化されたコードは(ボタンを保存)です:
try{
// Input the file location into Path variable 'p'
//Cannot write to CSV files
//Path p = Paths.get("C:\\Users\\myname\\Documents\\Stock Take Program\\tiger.csv");
Path p = Paths.get("C:\\Users\\myname\\Desktop\\test.txt");
//Read the whole file to a ArrayList
List<String> fileContent = new ArrayList<>(Files.readAllLines(p));
//Converting user input from editSerialField to a string
String strSerial = editSerialField.getText();
//Converting user input from editLocationField to a string
String strLocation = editLocationField.getText();
//This structure looks for a duplicate in the text file, if so, replaces it with the user input from editLocationField.
for (int i = 0; i < fileContent.size(); i++)
{
if (fileContent.get(i).equals(strSerial))
{
fileContent.set(i, strLocation);
}
break;
}
// write the new String with the replaced line OVER the same file
Files.write(p, fileContent);
}catch(IOException e)
{
e.printStackTrace();
}
私の質問はどのように私は.CSVの内容を更新し、交換に対応するためにコードを更新することができ、あります私の.txtファイルで動作するのと同じ方法で、ユーザー入力を使用してファイルを作成します。
テキストファイルに書き込むときは、最初の行だけが置換されますが、.CSVファイルに書き込むときには何も置き換えられません。
とにかく、.CSVファイル内のテキストを置き換えるためにコードを別に書く必要がありますか。
ご協力いただきありがとうございます。
csvファイルは、パスにスペースのない場所にある場合はどうなりますか? –
それは違いはありません。 – juiceb0xk
タイトルに「ファイルが見つかりません」と表示されているので、私は混乱しています –