Java Android Studio:将文件加载到数组中
我已经创建了一个可以将数组保存到文件的类,但是,我需要它将文件加载回名为SampleGridViewAdapter的活动中的数组中 名为gallerydump_img.txt的文本文件格式:Java Android Studio:将文件加载到数组中,java,android,android-studio,Java,Android,Android Studio,我已经创建了一个可以将数组保存到文件的类,但是,我需要它将文件加载回名为SampleGridViewAdapter的活动中的数组中 名为gallerydump_img.txt的文本文件格式: https://mywebsite.com/path/samplefile.rtf https://anotherwebsite.com/ https://thirdwebsite.com/example/ 我尝试了strings=LIST[I],其中strings是文件的输出,I是循环,LIST是逐行输
https://mywebsite.com/path/samplefile.rtf
https://anotherwebsite.com/
https://thirdwebsite.com/example/
我尝试了strings=LIST[I]
,其中strings
是文件的输出,I
是循环,LIST
是逐行输出文件数据的数组。更多代码如下:
@Override
protected void onPostExecute(String[] strings) {
for(int i = 0; i < strings.length; i++) {
Log.e("GalleryFileDump", strings[i]);
ArrayToFile.writeArrayToFile(strings, Environment.getExternalStorageDirectory() + "/gallerydump_img.txt", "Eww, errors. Want a cookie? :: Unable to write to file gallerydump.bin. Check the report below for more information. :)");
strings = LIST[i]
}
}
@覆盖
受保护的void onPostExecute(字符串[]字符串){
for(int i=0;i
谢谢你的帮助。谢谢 我猜上面的内容没有编译?如果没有,那也没关系。我只是想确定我理解这个问题 无论如何,将字符串序列化和反序列化到文件的一种方法如下:
String[] readFile(String filename)
{
String[] strings;
BufferedReader reader = null;
try
{
reader = new BufferedReader( new InputStreamReader( new FileInputStream(filename)));
String str = reader.readLine();
while( null != str )
{
strings.add(str);
str = reader.readLine();
}
}
catch( IOException e )
{
e.printStackTrace();
}
if( null != reader )
{
reader.close();
}
return strings.toArray(new String[strings.size()]);
}
void writeFile(String filename, String[] strings )
{
PrintWriter writer = null;
try
{
writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename)));
for( int idx = 0; idx < strings.length; idx++ )
{
writer.println(strings[idx]);
}
}
catch( IOException e )
{
e.printStackTrace();
}
if( null != writer )
{
writer.close();
}
}
String[]读取文件(字符串文件名)
{
字符串[]字符串;
BufferedReader reader=null;
尝试
{
reader=新的BufferedReader(新的InputStreamReader(新的FileInputStream(文件名));
String str=reader.readLine();
while(null!=str)
{
strings.add(str);
str=reader.readLine();
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
if(null!=读取器)
{
reader.close();
}
返回strings.toArray(新字符串[strings.size()]);
}
void writeFile(字符串文件名,字符串[]字符串)
{
PrintWriter=null;
尝试
{
writer=new PrintWriter(new OutputStreamWriter(new FileOutputStream(filename));
for(int-idx=0;idx
这是您想要阅读的内容
public static List<String> readLines() {
File f = new File("gallerydump_img.txt");
BufferedReader r;
List<String> lines = new ArrayList<String>();
try {
r = new BufferedReader(new FileReader(f));
String line;
while (true) {
if ((line = r.readLine()) == null)
break;
lines.add(line);
}
} catch (Exception e) {
e.printStackTrace(); // file not found
}
return lines;
}
publicstaticlist readLines(){
文件f=新文件(“gallerydump_img.txt”);
缓冲读取器r;
列表行=新的ArrayList();
试一试{
r=新的BufferedReader(新的文件读取器(f));
弦线;
while(true){
如果((line=r.readLine())==null)
打破
行。添加(行);
}
}捕获(例外e){
e、 printStackTrace();//未找到文件
}
回流线;
}
这就是你想写的
public static void writeLines(List<String> lines) {
File f = new File("gallerydump_img.txt");
try {
PrintWriter pw = new PrintWriter(f);
for (String line : lines)
pw.println(line);
pw.close();
} catch (FileNotFoundException e) {
e.printStackTrace(); // file not found
}
}
公共静态无效写入线(列表行){
文件f=新文件(“gallerydump_img.txt”);
试一试{
PrintWriter pw=新的PrintWriter(f);
用于(字符串行:行)
pw.println(行);
关闭();
}catch(filenotfounde异常){
e、 printStackTrace();//未找到文件
}
}
我的问题是我的strings=LIST[i]
没有将strings
添加到列表的i
插槽中。当我尝试时,它根本不会改变LIST
。抱歉,回答晚了。列表是字符串[]吗?如果是这样的话,您可以执行LIST[i]=strings[i];。或者,如果List是字符串[],则可以执行List[i]=strings;对不起,另一个链接不起作用。这是新的()如何声明/初始化列表?它看起来像是一个长度为0I的数组,我使用了static public String[]LIST={}代码>。