[英]How can I read every letter from the file?
I have this part of code.我有这部分代码。 I can read all lines from the code.
我可以从代码中读取所有行。 But I want take (read) every letter separately and put it into array.
但我想分别读取(读取)每个字母并将其放入数组。 How can I do it?
我该怎么做? For Example: In file are numbers 00010 and I want put it into array like this: array[0,0,0,1,0]
例如:在文件中是数字 00010,我想把它放入这样的数组中:array[0,0,0,1,0]
public void readTest()
{
try
{
InputStream is = getResources().getAssets().open("test.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String st = "";
StringBuilder sb = new StringBuilder();
while ((st=br.readLine())!=null)
{
sb.append(st);
}
br.close();
}catch (IOException e)
{
Log.d(TAG, "Error: " + e);
}
}
Use br.read()
.使用
br.read()
。 It returns the character as integer它将字符作为整数返回
ArrayList<char> charArray = new ArrayList<>();
int i;
while ((i = br.read()) != -1) {
char c = (char) i;
charArray.add(c);
}
You should add read every string and add it's letters to array by iterating through it, like this:您应该添加 read 每个字符串并通过迭代将其字母添加到数组中,如下所示:
while ((st=br.readLine())!=null) {
sb.append(st);
for (int i = 0; i < st.length(); i++) {
char c = st.charAt(i);
yourArray.add(c);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.