在 Txt 文件中拆分字符串
Split string in Txt files
假设我有一个名为 "Keys.txt"
:
的 txt 文件
Keys.txt:
Test 1
Test1 2
Test3 3
我想把字符串拆分成一个数组,但我不知道该怎么做
我希望结果是
在这样的数组中:
Test
1
Test1
2
Test2
3
我有这个启动代码:
FileReader fr = new FileReader("Keys.txt");
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
br.close();
System.out.println(str);
String str = "Test 1 Test1 2 Test2 3";
String[] splited = str.split("\s+");
您可以按照以下步骤操作:
- 读取字符串中的当前行,然后在空格处拆分字符串(一个或多个),您将得到一个可以将元素存储在列表中的数组。
- 对每一行重复该操作。
- 将列表转换为数组(
List.toArray()
)。
例如:
List<String> list = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("Keys.txt"))) {
String str;
while ((str = br.readLine()) != null) {
String[] token = str.split("\s+");
list.add(token[0]);
list.add(token[1]);
}
}
String[] array = list.toArray(new String[list.size()]);
请注意,通过使用 Java 8 流和 java.nio API(可从 Java 7 获得),您可以更简洁:
String[] array = Files.lines(Paths.get("Keys.txt"))
.flatMap(s -> Arrays.stream(s.split("\s+"))
.collect(Collectors.toList())
.stream())
.toArray(s -> new String[s]);
您可以使用 String.split()
方法(在您的情况下是 str.split("\s+");
)。
它将在一个 或更多 个空白字符上拆分输入字符串。正如 Java API 文档所述 here:
\s
- 空白字符:[ \t\n\x0B\f\r]
X+
- X
, 一次或多次.
您可以将所有行存储在一个字符串中,以空格分隔,然后将其拆分为您想要的数组。
FileReader fr = new FileReader("Keys.txt");
BufferedReader br = new BufferedReader(fr);
String str="", l="";
while((l=br.readLine())!=null) { //read lines until EOF
str += " " + l;
}
br.close();
System.out.println(str); // str would be like " Text 1 Text 2 Text 3"
String[] array = str.trim().split(" "); //splits by whitespace, omiting
// the first one (trimming it) to not have an empty string member
FileReader fr;
String temp = null;
List<String> wordsList = new ArrayList<>();
try {
fr = new FileReader("D://Keys.txt");
BufferedReader br = new BufferedReader(fr);
while ((temp = br.readLine()) != null) {
String[] words = temp.split("\s+");
for (int i = 0; i < words.length; i++) {
wordsList.add(words[i]);
System.out.println(words[i]);
}
}
String[] words = wordsList.toArray(new String[wordsList.size()]);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
试试这个
假设我有一个名为 "Keys.txt"
:
Keys.txt:
Test 1
Test1 2
Test3 3
我想把字符串拆分成一个数组,但我不知道该怎么做 我希望结果是 在这样的数组中:
Test
1
Test1
2
Test2
3
我有这个启动代码:
FileReader fr = new FileReader("Keys.txt");
BufferedReader br = new BufferedReader(fr);
String str = br.readLine();
br.close();
System.out.println(str);
String str = "Test 1 Test1 2 Test2 3";
String[] splited = str.split("\s+");
您可以按照以下步骤操作:
- 读取字符串中的当前行,然后在空格处拆分字符串(一个或多个),您将得到一个可以将元素存储在列表中的数组。
- 对每一行重复该操作。
- 将列表转换为数组(
List.toArray()
)。
例如:
List<String> list = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("Keys.txt"))) {
String str;
while ((str = br.readLine()) != null) {
String[] token = str.split("\s+");
list.add(token[0]);
list.add(token[1]);
}
}
String[] array = list.toArray(new String[list.size()]);
请注意,通过使用 Java 8 流和 java.nio API(可从 Java 7 获得),您可以更简洁:
String[] array = Files.lines(Paths.get("Keys.txt"))
.flatMap(s -> Arrays.stream(s.split("\s+"))
.collect(Collectors.toList())
.stream())
.toArray(s -> new String[s]);
您可以使用 String.split()
方法(在您的情况下是 str.split("\s+");
)。
它将在一个 或更多 个空白字符上拆分输入字符串。正如 Java API 文档所述 here:
\s
- 空白字符:[ \t\n\x0B\f\r]
X+
- X
, 一次或多次.
您可以将所有行存储在一个字符串中,以空格分隔,然后将其拆分为您想要的数组。
FileReader fr = new FileReader("Keys.txt");
BufferedReader br = new BufferedReader(fr);
String str="", l="";
while((l=br.readLine())!=null) { //read lines until EOF
str += " " + l;
}
br.close();
System.out.println(str); // str would be like " Text 1 Text 2 Text 3"
String[] array = str.trim().split(" "); //splits by whitespace, omiting
// the first one (trimming it) to not have an empty string member
FileReader fr;
String temp = null;
List<String> wordsList = new ArrayList<>();
try {
fr = new FileReader("D://Keys.txt");
BufferedReader br = new BufferedReader(fr);
while ((temp = br.readLine()) != null) {
String[] words = temp.split("\s+");
for (int i = 0; i < words.length; i++) {
wordsList.add(words[i]);
System.out.println(words[i]);
}
}
String[] words = wordsList.toArray(new String[wordsList.size()]);
br.close();
} catch (Exception e) {
e.printStackTrace();
}
试试这个