我想将从文件中获取的字符串转换为数组列表。我尝试过这种方式,但它不起作用:
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$
有人能帮我吗?