首页 \ 问答 \ 读取txt文件内容并存储在数组中(Reading txt file contents and storing in array)

读取txt文件内容并存储在数组中(Reading txt file contents and storing in array)

我一直在尝试读取txt文件。 txt文件包含例如行

First Line
Second Line
Third Line
.
.
.

现在我使用以下代码

InputStream is = null;
try {
    is = getResources().getAssets().open("myFile.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
}

ArrayList<String> arrayOfLines = new ArrayList<String>();

Reader reader;
//char[] buffer = new char[2048];
try {
    Reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;
    while   ((n = reader.read()) != -1) {

    }
}catch (Exception e) {
    e.printStackTrace();
}

我的问题是,我如何在arrayList中存储每一行​​。 Ofc我们必须使用"/n"检查但是如何检查。


I have been trying to read a txt file. The txt file contains lines e.g

First Line
Second Line
Third Line
.
.
.

Now I am using following code

InputStream is = null;
try {
    is = getResources().getAssets().open("myFile.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
    e.printStackTrace();
}

ArrayList<String> arrayOfLines = new ArrayList<String>();

Reader reader;
//char[] buffer = new char[2048];
try {
    Reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    int n;
    while   ((n = reader.read()) != -1) {

    }
}catch (Exception e) {
    e.printStackTrace();
}

My question is, how can i store each line in the arrayList. Ofc We have to use a check for "/n" but how.


原文:https://stackoverflow.com/questions/11918747
更新时间:2022-07-04 06:07

最满意答案

我对基本<cfexit>行为的回忆是:

  1. 在CFC中使用, cfexit退出cfc函数。 但是继续处理呼叫页面。
  2. 如果在函数内,但不在cfc内,则处理中止。

更新:我刚刚确认了CF9.0.1下的行为

结果(使用cfexit)

Start calling page 
Called test()
Finish calling page 
Called on requestEnd

结果(使用cfabort)

Start calling page 
Called test()
Called on requestEnd

Test.cfm

Start calling page <br />
<cfset createObject("component", "Foo").test()>
Finish calling page <br />

Foo.cfc

<cfcomponent>
    <cffunction name="test" output="true">
        Called test()<br />
        <cfexit>
    </cffunction>
</cfcomponent>

My recollection of how a basic <cfexit> behaves is:

  1. Used within a CFC, cfexit exits the cfc function. But processing of the calling page continues.
  2. If within a function, but NOT inside a cfc, then processing is aborted.

Update: I just confirmed that behavior under CF9.0.1

Results (using cfexit)

Start calling page 
Called test()
Finish calling page 
Called on requestEnd

Results (using cfabort)

Start calling page 
Called test()
Called on requestEnd

Test.cfm

Start calling page <br />
<cfset createObject("component", "Foo").test()>
Finish calling page <br />

Foo.cfc

<cfcomponent>
    <cffunction name="test" output="true">
        Called test()<br />
        <cfexit>
    </cffunction>
</cfcomponent>

相关问答

更多
  • 如果你的每一个CFC实例都是单例(即你只需要在你的应用程序中使用它的一个实例),那么你肯定希望查看Dependancy Injection。 CF有三种主要的Dependancy Injection框架; ColdSpring,WireBox和DI / 1。 我建议你看看DI / 1或WireBox,因为ColdSpring还没有更新一段时间。 DI / 1的wiki页面位于: https : //github.com/framework-one/di1/wiki/Getting-Started-with- ...
  • 我对基本行为的回忆是: 在CFC中使用, cfexit退出cfc函数。 但是继续处理呼叫页面。 如果在函数内,但不在cfc内,则处理中止。 更新:我刚刚确认了CF9.0.1下的行为 结果(使用cfexit) Start calling page Called test() Finish calling page Called on requestEnd 结果(使用cfabort) Start calling page Called test() Called on requestEn ...
  • 在你的查询中你已经指定了dbtype =“query”。 要告诉它查询您之前在某个页面上运行的查询。 在CFC中,查询可能不可用,因为它没有被传入,因此错误。 也许你的意思是在下面使用它并查询你的数据源而不是查询? SELECT * FROM tbl_name In your query you've specified ...
  • 我认为你称映射是错误的。 在application.cfc中使用您的定义: this.mappings["plugins"] 然后通过“插件”在其他代码中引用,所以: var aName = new plugins.theCFC() var aName = createObject("component","plugins.theCFC").init() HTH,如果不在调用页面上发布您的代码。 I think you ...
  • com.adobe.coldfusion.mail中的getter / setter实际上不是函数,而是访问器。 ColdFusion根据组件中的属性自动生成访问器。 属性是继承的,访问者不是! 邮件组件中的访问器除了设置/获取属性的值之外什么都不做。 相当于super.setTo(arguments.recipients); 因此是variables.to = arguments.recipients; 。 this.getTo()的等价物是variables.to等。 注意:对extends="com. ...
  • 您可以使用$ .get方法执行类似操作,但我通常会执行以下操作: $(document).ready(function() { $("#loadLink").click(function(e) { e.preventDefault(); var recordata = $(this).attr("href").substring(1); //trim '?' char $.ajax({ type: "GET", ...
  • 您应该能够在理论上没有任何关于对象名称的上下文的情况下执行它,因为它将从内部执行,并且应该检查其当前目录。 因此,以下应该做你需要的工作 var classPath = ListLast(getMetaData(this).fullname,'.'); return createObject("component", classPath).init(table, this.dsn); 这样,无论目录名称是什么都无关紧要,无论目录结构如何,它都适用于任何扩展该目录的对象,或者用于完整的示例 public f ...
  • 这是否打破了cfc的目标取向? 实现cfcs的这种模糊的“对象定位”是一种主观的。 强迫自己陷入“一切都必须是对象”,这会迫使你用CF来做事情,这会造成额外的开销。 我需要小小的妥协来确保应用程序的快速和高效。 不要担心实现“面向对象”的一些不可确定的目标。 制定一个更明确的目标,例如实现cfcs的重用或封装更改。 尝试对对象进行查看并不一定能帮助您实现这些目标,因为每个视图都会有所不同,并且可能无法重复使用。 这是否会导致CFML编译器每次都必须编译视图? Cfms也被编译和缓存。 我有几个大表单组成的标 ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的