package project2;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Project2 {
public static void main(String[] args) {
String FirstName = "";
String LastName = "";
ArrayList aList = new ArrayList();
try {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\vanchi\\Desktop\\test.txt"));
if(!br.ready())
throw new IOException();
while ((line = br.readLine())!=null){
aList.add(line);
//System.out.println(line);
String tmp[] = line.split(",");
FirstName = tmp[0];
LastName = tmp[1];
System.out.println(FirstName + "\t" + LastName);
}
br.close();
}catch (IOException e){
e.printStackTrace();
}
/* int sz = aList.size();
for(int i=0; i<sz; i++){
System.out.println(aList.get(i).toString()); */
}
如何閱讀下一行?
回答
確保文件的每一行確實至少有一個','。使您的代碼更健壯:
while ((line = br.readLine())!=null){
aList.add(line);
String tmp[] = line.split(",");
if (tmp.length < 2) {
continue;
}
FirstName = tmp[0];
LastName = tmp[1];
System.out.println(FirstName + "\t" + LastName);
}
而且,我相信你並不需要:
if(!br.ready())
throw new IOException();
你可以僅僅刪除這些行。
像魔術一樣工作謝謝 – Natalie007
爲了增加對實際內容的理解,可以在繼續之前添加一個打印輸出,以說明爲什麼跳過該行:System.out.println(「Skipping line:」+ line); – JenEriC
不客氣,娜塔莉) – AnatolyG
所以,在這裏你:)
V1: 包natalie.person.v1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class RunMe {
public static void main(String[] args) {
Set<String> allPersons = new HashSet<>(); // or use java.util.TreeSet() to have the list sorted
File dataFolder = new File("/home/agudkov/projects/rest/Natalie/data"); // change the path, pls
for (File file : dataFolder.listFiles()) {
if (!file.getName().endsWith(".txt")) { // check the file has .txt ext
continue;
}
addPersonsFromFile(file, allPersons);
}
for (String person : allPersons) {
System.out.println(person);
}
}
private static void addPersonsFromFile(File dataFile, Set<String> toSet) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(dataFile));
String line;
while ((line = br.readLine()) != null) {
String tmp[] = line.split(",");
if (tmp.length < 2) {
continue;
}
String firstName = tmp[0];
String lastName = tmp[1];
toSet.add(firstName + "\t" + lastName);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {}
}
}
}
}
V2有2類:
package natalie.person.v2;
import java.util.Objects;
public class Person implements Comparable<Person> {
public static Person parse(String line) {
String[] tmp = line.split(",");
if (tmp.length >= 2) {
return new Person(tmp[0], tmp[1]);
}
return null;
}
private final String firstName;
private final String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public int hashCode() {
int hash = 7;
hash = 79 * hash + Objects.hashCode(this.firstName);
hash = 79 * hash + Objects.hashCode(this.lastName);
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if (!Objects.equals(this.firstName, other.firstName)) {
return false;
}
if (!Objects.equals(this.lastName, other.lastName)) {
return false;
}
return true;
}
@Override
public int compareTo(Person o) {
int result = 0;
if (firstName != null) {
result = firstName.compareTo(o.firstName);
if (result == 0 && lastName != null) {
result = lastName.compareTo(o.lastName);
}
}
return result;
}
@Override
public String toString() {
return firstName + '\t' + lastName;
}
}
package natalie.person.v2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class RunMe {
public static void main(String[] args) {
Set<Person> allPersons = new HashSet<>(); // or use java.util.TreeSet() to have the list sorted
File dataFolder = new File("/home/agudkov/projects/rest/Natalie/data"); // change the path, pls
for (File file : dataFolder.listFiles()) {
if (!file.getName().endsWith(".txt")) { // check the file has .txt ext
continue;
}
addPersonsFromFile(file, allPersons);
}
for (Person person : allPersons) {
System.out.println(person);
}
}
private static void addPersonsFromFile(File dataFile, Set<Person> toSet) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(dataFile));
String line;
while ((line = br.readLine()) != null) {
Person person = Person.parse(line);
if (person == null) {
continue;
}
toSet.add(person);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {}
}
}
}
}
是[email protected] – Natalie007
- 1. 如何閱讀下TD格
- 2. 閱讀文件的下一行
- 3. 如何從某一行開始閱讀?
- 4. 如何閱讀DOM的一部分行?
- 5. 我如何閱讀一行字?
- 6. 如何閱讀Scanner.nextLine()的最後一行?
- 7. 如何閱讀Java中的文本文件的下一行?
- 8. 在閱讀下一行之前,我該如何停止?
- 9. python閱讀下一個()
- 10. 閱讀下一個值
- 11. 如何閱讀新行
- 12. 如何逐行閱讀pdf
- 13. PHP - 如何通過閱讀它一行一行地
- 14. 閱讀XML的第一行
- 15. 閱讀在同一行
- 16. 如何閱讀
- 17. 如何閱讀?
- 18. 如何閱讀
- 19. 如何閱讀
- 20. 如何閱讀「」
- 21. 如何閱讀
- 22. 我如何閱讀以下查詢
- 23. 閱讀一個和下一個觀察
- 24. 如何使用通用換行符下載和閱讀URL?
- 25. Emacs eshell。如何閱讀命令行的內容按下RET
- 26. 閱讀JSON行
- 27. 閱讀下面的另一列
- 28. 如何從指定的子閱讀下一子
- 29. 如何通過閱讀更多鏈接調用下一頁
- 30. 如何閱讀gmon.out?
有什麼問題你的代碼?有沒有編譯錯誤?例外? – PakkuDon
線程「main」java.lang.ArrayIndexOutOfBoundsException中的異常異常:1在project2.Project2.main – Natalie007
我想讀取文本文件中有兩個名稱的行,用逗號分隔natalie,sophenska brad,holer – Natalie007