如何在txt文件的开头设置光标? [英] How to set cursor at the beginning of the txt file?

查看:86
本文介绍了如何在txt文件的开头设置光标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与主题相同: 如何在Java中txt文件的开头设置光标? (即使用扫描仪类)

Same as topic: How to set cursor at the beginning of the txt file in the java? (im using scanner class)

推荐答案

我认为我首先错误地解释了这个问题.

I think I interpreted the question wrong first.

我想您可以使用RandomAccessFile来完成您想做的事情. 这是代码的简单快照,可以演示RandomAccessFile的使用.

I guess you can use RandomAccessFile to do what you wish to do. Here is a simple snapshot of the code that would demonstrate the use of the the RandomAccessFile.

package one;
import java.io.File;
import java.io.RandomAccessFile;
public class one {

    public static void main(String args[])
    {
     try
     {
      RandomAccessFile rac = new RandomAccessFile(new File("/home/.../src/one/a.txt"), "r");
      rac.seek(0);
      System.out.println(rac.readLine());
      System.out.println(rac.readLine());
      //Puts the pointer back to first position 
      rac.seek(0);
      System.out.println(rac.readLine());
     }
     catch(Exception e)
     {
        e.printStackTrace();
     }
    }
}

我的a.txt如下所示:

My a.txt looks like as below:

12345678910
212345678910
312345678910
412345678910
512345678910
612345678910

希望这会有所帮助

这篇关于如何在txt文件的开头设置光标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆