Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将数组读写到文件_Java_Android - Fatal编程技术网

Java 将数组读写到文件

Java 将数组读写到文件,java,android,Java,Android,你好。 我要做的事情是将数组中的一些信息保存到文件中,然后将其读回另一个数组。目标是保存主题(用户的十六进制颜色代码),以便他们可以共享主题或备份主题。 下面是我将数组写入文件的代码 String filename = "my.theme"; String[] numbers = new String[] {"1, 2, 3"}; FileOutputStream outputStream; try { outputStream = openFileOutput(filename, C

你好。 我要做的事情是将数组中的一些信息保存到文件中,然后将其读回另一个数组。目标是保存主题(用户的十六进制颜色代码),以便他们可以共享主题或备份主题。 下面是我将数组写入文件的代码

String filename = "my.theme";
String[] numbers = new String[] {"1, 2, 3"};
FileOutputStream outputStream;

try {
    outputStream = openFileOutput(filename, Context.MODE_APPEND);
    for (String s : numbers) {  
        outputStream.write(s.getBytes());  
    } 
    outputStream.close();
} catch (Exception e) {
    e.printStackTrace();
}
输出是一个包含以下内容的文件:

1,2,3
现在如何将其读回另一个数组?
为了我的目标。你能用这个方法提出其他建议吗?也可以保存为xml。谢谢:)

您可以使用Scanner类,这个小示例将帮助您开始:

String input = "1,2,3";
Scanner scn = new Scanner(input);  // Scanner also accepts a file!
scn.useDelimiter(","); // Since the integers are "comma" separated.
while(scn.hasNext())
{
    System.out.println(scn.nextInt()); // here you can store your integers back into your array
}
scn.close();
输出:

1
2
3

你在谷歌搜索过“Java读取文件到数组”吗<代码>扫描程序,
缓冲读取程序
,等等。如果您有兴趣将数据保存为XML,请查看JAXB。作为介绍:@JonnyHenly不,我搜索了除此之外的所有东西。Tnx@Beethoven可以把它读回去吗?谢谢,我要一杯look@NikanDalvand是的,它是用来读和写的。@cricket_007 OP已经写入了一个文件,可以用扫描仪进行回读,这有什么问题吗?啊,我想我错过了文件已经写入的部分。谢谢你能给我一个文件示例吗?这一个毁了我的应用程序!字符串文件名=Environment.getExternalStorageDirectory().toString()+“/”+“theme.test”;使用Environment.getExternalStorageDirectory().getAbsolutePath()+“/”+“theme.test”;另外,不要忘记在清单中提到读/写权限。@cricket\u 007发生在每个人身上;)