我想将从文件中获取的字符串转换成ArrayList。我尝试了以下方法,但它并不起作用:
import java.io.*;
import java.util.*;
public class Data
{
static File file = DataSaver.file;
static List<String> data = new ArrayList<String>(512);
public static void a() throws Exception
{
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
BufferedReader reader = new BufferedReader(new InputStreamReader(dis));
if(!file.exists())
{
throw new IOException("Datafile not found.");
}
else
{
String[] string = reader.readLine().split("$");
for(int i = 0; i < string.length; i++)
{
data.add(string[i]);
}
}
dis.close();
System.out.println(data.toString()); //for debugging purposes.
}
}
输出结果:
[testdata1,testdata2]
期望的输出结果:
[testdata1,testdata2]
文件内容:
$testdata1 $testdata2 $
有人能帮我吗?
String
数组叫做 'string'? - David Bstring
是String
的别名)。此外,它并没有真正描述变量中包含的内容。 - David B