我现在要在一个.txt文件中读一些数据,原来的数据是一些字符串,比如:jajjALIjkkjjklizou现在我要一个一个的读出,也就是说第一次读j,然后是a,一直这样下去,我写了个,可老是报错,麻烦大家给看下,谢谢。
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;/**
* @author huajun
*
*/
public class FileInput { private ArrayList<Character> collection = new ArrayList<Character>(); private int charlength; private Character[] returns = null; public Character[] Input() {
File file = new File("f:/test.txt"); try {
BufferedReader in = new BufferedReader(new FileReader(file));
while ((charlength = in.read()) != -1) {
System.out.println((char) charlength);
collection.add((char) charlength);
}
in.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println(collection);
charlength = collection.size();
System.out.println(charlength);
System.out.println("before returns!");
for (int m = 0; m < charlength; m++) {
System.out.println(m);
System.out.println(collection.get(m));
returns[m] = collection.get(m);//就是这里出不来,字符都能被读到Arraylist里面,可转换的时候不行。
} return returns;
}}
为什么?
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.*;/**
* @author huajun
*
*/
public class FileInput { private ArrayList<Character> collection = new ArrayList<Character>(); private int charlength; private Character[] returns = null; public Character[] Input() {
File file = new File("f:/test.txt"); try {
BufferedReader in = new BufferedReader(new FileReader(file));
while ((charlength = in.read()) != -1) {
System.out.println((char) charlength);
collection.add((char) charlength);
}
in.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println(collection);
charlength = collection.size();
System.out.println(charlength);
System.out.println("before returns!");
for (int m = 0; m < charlength; m++) {
System.out.println(m);
System.out.println(collection.get(m));
returns[m] = collection.get(m);//就是这里出不来,字符都能被读到Arraylist里面,可转换的时候不行。
} return returns;
}}
为什么?
returns[m] = (Character)collection.get(m);
private Character[] returns = null;在charlength = collection.size(); 这句之后加上
returns = new Character[charlength];这样就可以了
private ArrayList<Character> collection = new ArrayList<Character>();
private int charlength;
private Character[] returns = null;