简体   繁体   English

有没有办法使用单个字符串对象显示文件中的所有内容

[英]Is there a way to display all the contents in a file as it is using a single string object

I am writing an android app for which I want to write a function that parses a text file collecting all the lines and putting it into single String object and display the contents as it was in the file. 我正在编写一个Android应用程序,我想为其编写一个函数,该函数分析收集所有行的文本文件并将其放入单个String对象中,并按文件中的内容显示内容。 The display should be along with all the 'new line' characters which is present in the text file. 显示内容应与文本文件中所有“换行”字符一起显示。 How can I do this? 我怎样才能做到这一点?

Also like an end of file, Is there a way to use another identifier within a file so that I can have multiple portions within the same file ? 就像文件结尾一样,是否可以在文件中使用另一个标识符,以便在同一个文件中包含多个部分?

Thanks in advance 提前致谢

Below is a sample to read data from a text file into a String object. 下面是将文本文件中的数据读取到String对象中的示例。 In this example, the text file is part of assets. 在此示例中,文本文件是资产的一部分。 You can get an InputStream from opening files from sdcard also : 您还可以通过从sdcard打开文件来获取InputStream:

    public class AssetsTest extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
AssetManager assetManager = getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open("texts/myawesometext.txt");
String text = loadTextFile(inputStream);
textView.setText(text);
} catch (IOException e) {
textView.setText("Couldn't load file");
} finally {
if (inputStream != null)
try {
inputStream.close();
} catch (IOException e) {
textView.setText("Couldn't close file");
}
}
}
public String loadTextFile(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
byte[] bytes = new byte[4096];
int len = 0;
while ((len = inputStream.read(bytes)) > 0)
byteStream.write(bytes, 0, len);
return new String(byteStream.toByteArray(), "UTF8");
}

You can store what you parse from the text file in a single String that's no problem. 您可以将文本文件中解析的内容存储在单个String中,这没有问题。

You can also use different Strings for each line of text but it'll be pretty much resource consuming and unnecessary. 您还可以为每行文本使用不同的字符串,但是这将浪费大量资源,并且不必要。

To put it short yes you can use one String for your question. 简而言之,可以为您的问题使用一个字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将许多XML(文件对象)转换为具有所有文件内容的单个字符串 - Transforming lots of XMLs (File objects) into a single String with all the files' contents 如何使用 java 将文件内容保存为单个字符串 - how to save contents of a file as single string using java 在此过程中,使用文件和对象流在类之间发送文件内容,而无需使用String - Send file contents between classes using file & object streams without using String in this process 是否可以使用字符串的内容强制转换对象? - Is it possible to cast an Object using the contents of a String? 是否可以将单个类文件的内容加载到对象中,并将其转换为特殊类型? - Is it possible to load the contents of a single class file into an Object, and cast it into a spicific type? 在TextArea中显示文件内容 - Display file contents in a TextArea 将Set <String>的内容放到单个String中的最快方法,其中单词用空格分隔? - Fastest way to put contents of Set<String> to a single String with words separated by a whitespace? 有没有一种方法可以通过使用listview显示所有sqlite表和内容? - Is there a way of displaying all sqlite tables and contents by using listview? 有没有一种方法可以使用键盘快捷键或菜单项在Eclipse中复制字符串的内容? - Is there a way to copy the contents of a String in Eclipse using a keyboard shortcut or menu item? 如何使用宏在Powerpoint中显示文本文件的内容 - how to display the contents of a text file in powerpoint using a macro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM