0

だから私はJavaでファイルを読み込もうとしています。最後の行が空でない限り、正常に機能します。空の場合は無視されます。しかし、この空の行も読む必要があります。

これが私のコードです:

try
        {
            BufferedReader in = new BufferedReader(new     FileReader("filename.txt"));

        String Line;

        while((Line = in.readLine()) != null)
        {
            System.out.println("L| " + Line);
        }

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

1 に答える 1

1

最初にスキャナークラスを使用します...それらは使いやすいので....次に、各行をリストに保存してから、最後の行を取得します..コードは次のとおりです。

public void readLast()throws IOException{
        FileReader file=new FileReader("E:\\Testing.txt");  //address of the file 
        List<String> Lines=new ArrayList<>();  //to store all lines
        Scanner sc=new Scanner(file);
        while(sc.hasNextLine()){  //checking for the presence of next Line
            Lines.add(sc.nextLine());  //reading and storing all lines
        }
        sc.close();  //close the scanner
        System.out.print(Lines.get(Lines.size()-1)); //displaying last one..
    }
于 2014-05-14T06:21:29.340 に答える