我想用Java多次读取同一个文件

时间:2018-06-19 09:20:17

标签: java buffer bufferedreader filereader

我想在修改文件时在同一个执行中多次读取文件。我做了这些代码,但是当我第二次阅读文件时,它说没有更多行。

该文件是像这样的.csv:

"v1";"v2";"v3";"v4";"v5"
"33455";"15.06.2018 20:48:44";555;555;43
"234455";"15.06.2018 20:49:10";555;555;42

代码:

public static void main(String[] args) throws ParseException, FileNotFoundException, IOException 
{
    String csvFile = "file.csv";

    readCSVFile(csvFile);

    Date lastFileModifiedData = getFileLastModifiedData(csvFile);

    while(true)
    {        
        Date fileModifiedData = getFileLastModifiedData(csvFile);
        if (!lastFileModifiedData.equals(fileModifiedData))
        {
            readCSVFile(csvFile);
            lastFileModifiedData=fileModifiedData;
        }

    }
}

public static void readCSVFile(String csvFile) throws ParseException, FileNotFoundException, IOException
{
    String line;
    String cvsSplitBy = ";";

    FileReader fr = new FileReader(csvFile);

    try (Scanner scan = new Scanner(fr)) {
        if (scan.hasNextLine())line = scan.nextLine(); 
        while (scan.hasNextLine()) 
        {      
            line = scan.nextLine();
            String[] details = line.split(cvsSplitBy);

            details[0] = details[0].replace("\"","");
            details[1] = details[1].replace("\"","");


            System.out.println("VarName=" + details[0] + " , TimeString=" + details[1] + " , VarValue=" + details[2] + " , Validity=" + details[3] + " , Time_ms=" + details[4]);;

        }
    }
    fr.close();

}

0 个答案:

没有答案