因此,我逐行打开并读取文件,每行当前都被读入一个字符串,整个文件被捕获为另一个字符串,它只是附加的行字符串。我不需要两者,关键是行和文件字符串都可用。
在这个问题的另一端是一个只保存字符串数组的类,该数组由另一个活动读取和显示。我们可以假设文件中的每一行构成了数组中的另一个字符串。到目前为止,我在这个数组中有硬编码的文本,但显然,我需要使用第一个活动中的行字符串动态填充它。
我已经看过如何在java中对动态数组进行排序,但在Android应用程序中从未这样做过。我知道数组列表会更好地适应未知文件长度的动态性质,但是将这些行放入后续类的最佳方法是什么?我之前已经为listviews序列化了列表 - 使用getter和setter,但是我无法理解如何从我的行字符串到另一个类中的数组字符串。我已经阅读了25篇以上的帖子,可以使用一些帮助。
这是将文件读入字符串的方法,您可以从打开的文件中查看我的两个字符串选项。祝酒词仅用于调试帮助。:
private void onFileClick(Option o) throws FileNotFoundException {
String filename = o.getName();
String aDataRow;
String aBuffer = null;
Toast.makeText(this, "Selected: "+ currentDir + "/" + o.getName(), Toast.LENGTH_SHORT).show();
try {
File myFile = new File("/" + currentDir + "/" + filename);
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();
Intent outlineInt = new Intent(getApplicationContext(), FragActivity.class);
startActivity(outlineInt);
finish();
}
这是我需要将字符串输入的类:
public class Content {
static String[] Paragraphs = {
"Heading 1\n\nExcepteur pour-over occaecat squid biodiesel umami gastropub, nulla laborum salvia dreamcatcher fanny pack.\n\nUllamco culpa retro ea, trust fund excepteur eiusmod direct trade banksy nisi lo-fi cray messenger bag. Nesciunt esse carles selvage put a bird on it gluten-free, wes anderson ut trust fund twee occupy viral. Laboris small batch scenester pork belly, leggings ut farm-to-table aliquip yr nostrud iphone viral next level. Craft beer dreamcatcher pinterest truffaut ethnic, authentic brunch. Esse single-origin coffee banksy do next level tempor. Velit synth dreamcatcher, magna shoreditch in american apparel messenger bag narwhal PBR ennui farm-to-table.",
"Heading 1\n\nVinyl williamsburg non velit, master cleanse four loko banh mi. Enim kogi keytar trust fund pop-up portland gentrify.\n\n Non ea typewriter dolore deserunt Austin. Ad magna ethical kogi mixtape next level. Aliqua pork belly thundercats, ut pop-up tattooed dreamcatcher kogi accusamus photo booth irony portland. Semiotics brunch ut locavore irure, enim etsy laborum stumptown carles gentrify post-ironic cray. Butcher 3 wolf moon blog synth, vegan carles odd future.",
"Heading 2\n\nWhatever belongs in H2 goes here now",
"Heading 3\n\nWhatever belongs in H3 goes here now",
"Heading 4\n\nWhatever belongs in H4 finally goes here",
"Heading 5\n\nExcepteur pour-over occaecat squid biodiesel umami gastropub, nulla laborum salvia dreamcatcher fanny pack.\n\nUllamco culpa retro ea, trust fund excepteur eiusmod direct trade banksy nisi lo-fi cray messenger bag.\n\nNesciunt esse carles selvage put a bird on it gluten-free, wes anderson ut trust fund twee occupy viral. Laboris small batch scenester pork belly, leggings ut farm-to-table aliquip yr nostrud iphone viral next level. Craft beer dreamcatcher pinterest truffaut ethnic, authentic brunch. Esse single-origin coffee banksy do next level tempor. Velit synth dreamcatcher, magna shoreditch in american apparel messenger bag narwhal PBR ennui farm-to-table.",
};
}
请记住,我永远不知道任何给定文件中会有多少行(或标题)。
分享任何好的想法或经验?感谢。