如何读取java中文本文件中添加的新行

时间:2015-12-14 17:02:03

标签: java

我一直在尝试使用randomacess来读取新行,但是我无法获取前一次迭代的文件指针并将其与最新文件的行尾进行比较并只读取差异。

感谢任何帮助。

    try {
        String data =fromNode+" "+message+" "+toNode;

        File fileTo = new File(File.separator + "Users" + File.separator
                + "Apoorv" + File.separator + "Desktop" + File.separator
                + "Files" + File.separator + "to" + toNode + ".txt");
        File fileFrom = new File(File.separator + "Users" + File.separator
                + "Apoorv" + File.separator + "Desktop" + File.separator
                + "Files" + File.separator + "from" + fromNode + ".txt");

        // if file does not exists, then create it
        if (!fileTo.exists()) {
            fileTo.createNewFile();
        }

        if (!fileFrom.exists()) {
            fileFrom.createNewFile();
        }

        // true = append file
        FileWriter fileWritter = new FileWriter(fileTo, true);
        BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
        bufferWritter.write(data);
        bufferWritter.newLine();
        bufferWritter.close();


        //logic to check whether there exist a link between the nodes
        for (int i=0;i< pairUniList.size() - 1; i++){   
        if(pairUniList.get(i).getX()==fromNode && pairUniList.get(i).getY()==toNode){
            //System.out.println("values: "+ pairList.get(i).getX()+"---"+fromNode+"---->"+pairList.get(i).getY()+"---"+toNode);
        FileWriter fileWritterfrom = new FileWriter(fileFrom,
                true);
        // System.out.println("------>"+data);
        BufferedWriter bufferWritterfrom = new BufferedWriter(
                fileWritterfrom);
        bufferWritterfrom.write(data);
        bufferWritterfrom.newLine();
        bufferWritterfrom.close();

        }
        }
        System.out.println("Files have been created");

        //trying to check for the end of line

        RandomAccessFile myFile = new RandomAccessFile(fileTo,"rw");

        // Set write pointer to the end of the file
        myFile.seek(myFile.length());
        System.out.println("***");

        // Write to end of file here

    } catch (IOException e) {
        e.printStackTrace();
    }

    // ******************************************************************




}

代码示例

1 个答案:

答案 0 :(得分:1)

   try {

            FileInputStream fstream = new FileInputStream(
                    File.separator + "Users" + File.separator + "Desktop" + File.separator
                + "Files" + File.separator + "from" + fromNode + ".txt");

            @SuppressWarnings("resource")
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String line;

            while (true) {

                line = br.readLine();
                if (line == null) {
                    Thread.sleep(1000);
                } else {
                    System.out.println(line);
                }

            }

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
        }

这解决了我每秒读取新行的问题。