[英]How to read from a txt file and and seperate the text based on numbers and strings in Java
該程序正在從文本文件中讀取。 文本文件的每一行都以一個從 -2 到 2 的數字開頭。數字后面是一個句子。 請參閱下面的 txt 文件的前三行:
1 Campanella gets the tone just right -- funny in the middle of sad in the middle of hopeful .
-2 Nothing more than an amiable but unfocused bagatelle that plays like a loosely-connected string of acting-workshop exercises .
1 It 's a sharp movie about otherwise dull subjects .
1 ... it 's as comprehensible as any Dummies guide , something even non-techies can enjoy .
-1 -LRB- Green is -RRB- the comedy equivalent of Saddam Hussein , and I 'm just about ready to go to the U.N. and ask permission for a preemptive strike .
唯一應該閱讀的行是具有數字、一個空格和按該順序排列的文本的行。 不應考慮最后兩行,因為它們在文本之前分別具有...
和-
。 不過前三句還不錯。
我有一個名為placeholder
的 class 具有以下字段:
public class placeholder implements Comparable<placeholder> {
protected int score;
protected String text;
public placeholder(int score, String text) {
this.score = score;
this.text = text;
}
}
我想要一個名為readFile
的方法逐行到 go 並將每一行存儲到一個名為reviewsDB
的列表中。 列表中的每個 object 都將是placeholder
類型,行首的數字將由score
值表示,以下單詞將是text
值。 我可以在以下區域輸入什么代碼來分隔數字和文本之間的每一行?
public static List<placeholder> readFile(String filename) {
File movieReviews = new File("reviews.txt");
try {
Scanner scanner = new Scanner(movieReviews);
scanner.nextLine();
List<placeholder> reviewsDB = new ArrayList<placeholder>();
while (scanner.hasNextLine()) {
int sentenceScore = 0;
String sentenceText = null;
//code to separate the number and text in each line here
placeholder newSentence = new placeholder(sentenceScore, sentenceText);
reviewsDB.add(newSentence);
}
return reviewsDB;
}
catch (Exception e) {
System.out.println("Something went wrong");
return null;
}
}
Files#lines
將文件讀入 stream"-?\\d\\s\\w+.*"
過濾符合您條件的行line.split("\\s",2)
Placeholder
對象列表示例代碼:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Example {
public static void main(String[] args) {
List<placeholder> list= readFile("path to your file");
list.forEach(System.out::println);
}
public static List<placeholder> readFile(String filename) {
List<Placeholder> reviewsDB = new ArrayList<>();
try (Stream<String> content = Files.lines(Paths.get(filename))) {
reviewsDB = content
.filter(line -> line.matches("-?\\d\\s\\w+.*"))
.map(line -> line.split("\\s",2))
.map(arr -> new placeholder(Integer.parseInt(arr[0]), arr[1]))
.collect(Collectors.toList());
} catch (IOException ex) {
ex.printStackTrace();
}
return reviewsDB;
}
}
您可以使用正則表達式。 最好匹配模式。 您可能有 n 個字符,也可能有正負。 如果您在開頭也有+
,則可以添加(-|+)
。
希望你沒有科學記數法。
while (scanner.hasNextLine()) {
int sentenceScore = 0;
String sentenceText = null;
String line = scanner.nextLine();
Matcher m = p.matcher(line);
if (m.matches()) {
System.out.println(m.group(1));
System.out.println(m.group(2));
}
// code to separate the number and text in each line here
placeholder newSentence = new placeholder(sentenceScore, sentenceText);
reviewsDB.add(newSentence);
}
我使用了下面的正則表達式
Pattern p = Pattern.compile("^(-?\\d+)(.*)");
-
是可選的 - -?
意思是然后一位或多位數字 - \d+
然后第二組是第一組之后的任何字符 - (.*)
你可以在這里玩你的輸入我在這里測試了你的輸入。
您可以使用Files.readAllLines(Path, Charset)
獲取表示文件內容的字符串列表。 然后您可以遍歷列表並使用String.split(Regex, Limit)
將字符串分成幾部分。 然后您可以從零件創建一個新的占位符對象。
看:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.