大家好,我在读取文件的方法上遇到困难。我在文档中没有找到任何内容。
我正在尝试返回一个字符数组。
public class ReadFile {
public static void main(String[] args) throws IOException {
File f = new File("word.txt");
char [] word=ReadWordFile(f);
System.out.println(word);
}
//1.Read words from File and return as arr of char
public static char [] ReadWordFile(File f) throws IOException {
BufferedReader br
= new BufferedReader(new FileReader(f));
char [] aux = null;
// Declaring a string variable
String st;
// Condition holds true till
// there is character in a string
while ((st = br.readLine()) != null)
st+=aux;
return aux;
}
您的代码中没有任何变化
aux
:
char [] aux = null;
// Declaring a string variable
String st;
// Condition holds true till
// there is character in a string
while ((st = br.readLine()) != null)
st+=aux;
return aux;
连接所有读取的字符串,然后从以下位置获取字符数组:
String st = "", read = null;
// Condition holds true till
// there is character in a string
while ((read = br.readLine()) != null)
st += read;
return st.toCharArray();