5 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊
有兩種解決方案:
不要關(guān)閉掃描儀。保持其打開狀態(tài),直到不需要為止。換句話說,在循環(huán)之后關(guān)閉它。
通過調(diào)用 重新創(chuàng)建掃描儀
sc = new Scanner(new File("addressBook.txt"));
。但是,由于這將創(chuàng)建一個(gè)新的掃描儀,因此它將再次從第一行開始讀取。

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊
sc.close();
您應(yīng)該在 while 循環(huán)之后調(diào)用,而不是在其中調(diào)用。按照您的邏輯,掃描儀從循環(huán)本身的第二次迭代開始就無法使用。

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超5個(gè)贊
您的主要問題是您deleteByName()
在循環(huán)內(nèi)調(diào)用刪除原始文件然后重用Scanner
.
你應(yīng)該這樣做:
找到所有
name
呼叫
deleteByName()
與所有發(fā)現(xiàn)names
。
public final class AddressBookManager {
private final File file;
public AddressBookManager(File file) {
this.file = file;
}
public void modifyContact(String oldName, String newName) throws IOException {
if (isContactExists(oldName))
updateContactName(oldName, newName);
}
private boolean isContactExists(String name) throws FileNotFoundException {
try (Scanner scan = new Scanner(file)) {
while (scan.hasNextLine()) {
String str = scan.nextLine();
if (str.startsWith(name + ',')) {
String[] parts = str.split(",");
System.out.format("Contact found. Name '%s', phone number '%s', address '%s', email '%s'\n", parts[0], parts[1], parts[2],
parts[3]);
return true;
}
}
System.out.println("No contact found with name '" + name + '\'');
return false;
}
}
private void updateContactName(String curName, String newName) throws IOException {
File tmp = new File(file.getParent(), "TempFile.txt");
try (BufferedReader in = new BufferedReader(new FileReader(file));
BufferedWriter out = new BufferedWriter(new FileWriter(tmp))) {
String str;
while ((str = in.readLine()) != null) {
if (str.startsWith(curName))
str = newName + str.substring(str.indexOf(','));
out.write(str);
out.newLine();
}
}
System.out.println("remove old file: " + file.delete());
System.out.println("rename temp file: " + tmp.renameTo(file));
}
public static void main(String... args) throws IOException {
AddressBookManager addressBookManager = new AddressBookManager(new File("d:/addressBook.txt"));
String curName = "oleg";
String newName = getNewName(curName);
addressBookManager.modifyContact(curName, newName);
}
private static String getNewName(String curName) {
try (Scanner scan = new Scanner(System.in)) {
System.out.print("Enter new name for (" + curName + "): ");
return scan.nextLine();
}
}
}

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
問題是當(dāng)您使用系統(tǒng)關(guān)閉掃描儀時(shí)。來自系統(tǒng)的輸入流也被關(guān)閉。因此,即使您使用 System.in 創(chuàng)建一個(gè)新的掃描儀,您也將無法重用該掃描儀。如果您使用的是 java 7,您可以使用 try 和資源來通過 Java 本身關(guān)閉所有 autocCleasable 資源。這將解決該問題。
public static void modifyContact(String namee) {
File file = new File("addressBook.txt");
try (Scanner sca = new Scanner(System.in);
Scanner sc = new Scanner(file);
FileWriter pw = new FileWriter(file, true);) {
String[] s;
boolean foundPerson = false;
String newName = sca.nextLine();
while (sc.hasNextLine()) {
s = sc.nextLine().split(",");
if (s[0].equals(namee)) {
s[0] = s[0].replace(s[0], newName);
System.out.println("Name is " + namee + " phone number is " + s[1] + " ,address is " + s[3]
+ " and email is " + s[2]);
foundPerson = true;
deleteByName(namee);
pw.write(s[0] + "," + s[1] + "," + s[2] + "," + s[3]);
}
}
if (!foundPerson) {
System.out.println("No contact found with " + namee);
}
} catch (IOException ex) {
// System.out.println(ex.getMessage());
}
}

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
我在循環(huán)后添加
sc.close(); deleteByName(namee);
看起來效果很好。謝謝大家的幫助。
添加回答
舉報(bào)