gpt4 book ai didi

Java - 从文本文件创建字符串数组

转载 作者:行者123 更新时间:2023-12-02 07:45:22 25 4
gpt4 key购买 nike

我有一个像这样的文本文件:

abc def jhi
klm nop qrs
tuv wxy zzz

我想要一个像这样的字符串数组:

String[] arr = {"abc def jhi","klm nop qrs","tuv wxy zzz"}

我已经尝试过:

try
{
FileInputStream fstream_school = new FileInputStream("text1.txt");
DataInputStream data_input = new DataInputStream(fstream_school);
BufferedReader buffer = new BufferedReader(new InputStreamReader(data_input));
String str_line;
while ((str_line = buffer.readLine()) != null)
{
str_line = str_line.trim();
if ((str_line.length()!=0))
{
String[] itemsSchool = str_line.split("\t");
}
}
}
catch (Exception e)
{
// Catch exception if any
System.err.println("Error: " + e.getMessage());
}

请大家帮帮我......所有答案将不胜感激...

最佳答案

如果您使用 Java 7,由于the Files#readAllLines method,它可以在两行内完成。 :

List<String> lines = Files.readAllLines(yourFile, charset);
String[] arr = lines.toArray(new String[lines.size()]);

关于Java - 从文本文件创建字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12857242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com