使用以下代码从文件中读取最后四行但是将第一行返回为null为什么?如何解决这个问题呢?请帮我?
public void read(){
StringBuilder text = new StringBuilder();
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath()+File.separator+"GPS");
dir.mkdirs();
String fname = "gps.txt";
File file = new File (dir, fname);
String[] last4 = new String[4];
int count=0;
try {
BufferedReader br = new BufferedReader(new FileReader(file));
while(br.ready()){
last4[count++%4]=br.readLine();
}
for (int i=0; i<4;i++){
text.append(last4[(i+count)%4]);
text.append('\n');
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
答案 0 :(得分:1)
您必须检查文件是否存在。根据您的代码复制粘贴,
public void read() throws FileNotFoundException{
StringBuilder text = new StringBuilder();
String fname = "asdf.txt";
String path = Environment.getExternalStorageDirectory()+File.separator+"GPS"+File.separator+fname;
//You have to check if file exists
File file = new File(path);
if(!file.exists()){
//TODO do smth if your file doesnt exist
return;
}
BufferedReader br = null;
String[] last4 = new String[4];
int count=0;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(path));
while ((sCurrentLine = br.readLine()) != null ) {
String str = sCurrentLine;
last4[count%4] = str;
count++;
}
for (int i=0; i<4;i++){
text.append(last4[i]);
text.append('\n');
}
System.out.println(text.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
答案 1 :(得分:0)
试试这个。
private static BufferedReader innerReader;
private static BufferedReader innerReader1;
private static final int UNTIL_LINE = 4;
public static int countLine(Reader reader) throws IllegalArgumentException
{
int countLine = 0;
if(reader == null)
{
throw new IllegalArgumentException("Null reader");
}
String line;
innerReader1 = new BufferedReader(reader);
try
{
while((line = innerReader1.readLine()) != null)
{
countLine++;
}
}catch(IOException e){}
return countLine;
}
public static List<String> loadFile(Reader reader, int countLine)
throws IllegalArgumentException{
List<String> fileList = new ArrayList<String>();
if(reader == null)
{
throw new IllegalArgumentException("Null Reader");
}
String line;
int thisLine = 0;
innerReader = new BufferedReader(reader);
try
{
while((line = innerReader.readLine()) != null)
{
if (line == null || line.trim().isEmpty())
throw new IllegalArgumentException(
"Line Empty");
thisLine++;
if(thisLine > countLine-UNTIL_LINE)
{
fileList.add(line);
}
}
} catch (IOException e) {
}
return fileList;
}
测试代码。
int lines = 0;
List<String> test = new ArrayList<String>();
try {
lines = countLine(new FileReader("YourFile.txt"));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
test = loadFile(new FileReader("YourFile.txt"), lines);
} catch (IOException e) {
e.printStackTrace();
}
for(String s : test)
{
System.out.println(s);
}
完整文件示例:
1行
2行
3行
4行
5行
6行
结果:
3行
4行
5行
6行
答案 2 :(得分:0)
您确定该文件存在且您具有读取权限吗?在阅读之前创建父目录很奇怪。检查logcat以查找堆栈跟踪。另外,请确保您在"android.permission.WRITE_EXTERNAL_STORAGE"
中声明了AndroidManifest
权限。
public static String[] tail(File file, int tail) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(file));
List<String> lines = new ArrayList<>();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
lines.add(line);
}
reader.close();
return lines.subList(Math.max(0, lines.size() - tail), lines.size()).toArray(new String[tail]);
}
使用示例:
try {
File gps = new File(Environment.getExternalStorageDirectory(), "GPS/gps.txt");
String[] lastFourLines = tail(gps, 4);
} catch (IOException e) {
// Are you sure the file exists?
// Did you declare "android.permission.WRITE_EXTERNAL_STORAGE" permission?
}