如何从.txt文件中读取但在文件中的两个点之间 [英] How to read from a .txt file but between two points in the file

查看:101
本文介绍了如何从.txt文件中读取但在文件中的两个点之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我一直试图弄清楚如何从.txt文件中读取。我知道如何阅读整个文件,但我在阅读文件中两个特定点之间遇到困难。

我也在尝试使用扫描仪课程,这是我到目前为止所做的:



  public   void  readFiles( String  fileString)throws FileNotFoundException {

file = new File(fileString);
扫描仪扫描仪= null ;
line = ;


// 访问文件
尝试 {
scanner = 扫描程序(文件);
}
catch (FileNotFoundException e){
System。 out .println( 找不到文件。);
}


// 如果文件中有更多行,请转到到下一行
while (scanner.hasNext())
{
line = scanner.next();


if (scanner.emals( BGSTART)) // 标签在txt中找到位置
{
line = scanner.nextLine();
System。 out .println(line);
lb1.append(line); // 附加到JTextArea。
window2。 add (lb1); // 添加到JPanel
}
}





.txt文件如下所示:

 BGSTART 
// content
BGEND



我试图在这两个点之间读取它。

有什么建议吗?

谢谢。

解决方案

试试这个:

  public   static   void  readFiles( String  fileString)
throws FileNotFoundException {

文件file = new 文件(fileString);
Scanner scanner = null;
字符串 line = ;

// 访问文件
尝试 {
scanner = 扫描程序(文件);
} catch (FileNotFoundException e){
System.out.println( 找不到文件。);
return ; // 如果找不到文件,请不要继续
}

// 如果文件中有更多行,则转到下一行
布尔值已启动=假;
while (scanner.hasNext()){
line = scanner.nextLine();

if (line.equals( BGEND)){
started = false;
// 如果您还想立即退出循环,
// 取消评论以下行:
// break;
}

if (已开始) // 标记在txt中以找到位置
{
的System.out.println(线);
lb1.append(line); // 附加到JTextArea。
window2.add(lb1); // 添加到JPanel
}

if (line.equals( BGSTART)){
start = true;
}
}
scanner.close();
}



它是如何运作的?



  1. 它通过这些行。
  2. 该行等于 BGEND ,它将启动设置为false。
  3. 如果已启动等于 true ,则会执行该行的操作,例如打印它。
  4. 如果该行等于 BGSTART ,则将启动设置为true。



这些步骤的顺序很重要,因为BGSTART和BGEND不需要自己打印。



其他一些变化:



  • 在catch块中,你应该返回,因为如果不这样做,方法会继续扫描仪已创建。
  • 扫描仪应在最后关闭。


尝试

< pre lang =java> public void readFile( String fileString) throws IOException
{

文件file = new 文件(fileString);
Scanner scanner = null;
字符串 line = ;


// 访问文件
尝试
{
scanner = 扫描程序(文件);
}
catch (FileNotFoundException e)
{
System.out.println( 找不到文件。);
}


// 如果文件中有更多行,请转到到下一行

boolean gatheringData = false;

while (scanner.hasNext())
{
line = scanner.next();
if (gatherData)
{
if (line.equals ( BGEND))
break < /跨度>;

System.out.println(line); // 将此行添加到JTextArea

}
else if (line.equals( BGSTART))
{
gatheringData = true;
}
}
}


Hello!

I have been trying to figure out how to read from a .txt file. I know how to read a whole file, but I am having difficulties reading between two specific points in a file.
I am also trying to use the scanner class and this is what I have so far:

public void readFiles(String fileString)throws FileNotFoundException{
      
        file = new File(fileString);
        Scanner scanner = null; 
        line=""; 
       

        //access file
        try {
            scanner = new Scanner(file);
        } 
        catch (FileNotFoundException e) {
            System.out.println("File not found.");
        }
         
       
            // if more lines in file, go to next line
           while (scanner.hasNext())
            {
               line = scanner.next();
              
               
               if (scanner.equals("BGSTART")) //tag in the txt to locate position
               {
                   line = scanner.nextLine();
                   System.out.println(line);
                   lb1.append(line); //attaches to a JTextArea.
                   window2.add(lb1);//adds to JPanel
                }
            }



.txt file looks something like this:

BGSTART
//content
BGEND


I am trying to read it between those tow points.
Any suggestions?
Thank You.

解决方案

Try this:

public static void readFiles(String fileString)
        throws FileNotFoundException {

    File file = new File(fileString);
    Scanner scanner = null;
    String line = "";

    // access file
    try {
        scanner = new Scanner(file);
    } catch (FileNotFoundException e) {
        System.out.println("File not found.");
        return; // don't continue if the file is not found
    }

    // if more lines in file, go to next line
    Boolean started = false;
    while (scanner.hasNext()) {
        line = scanner.nextLine();

        if (line.equals("BGEND")) {
            started = false;
            // if you also immediately want to go out of the loop,
            // un-comment the following line:
            // break;
        }

        if (started) // tag in the txt to locate position
        {
            System.out.println(line);
            lb1.append(line); //attaches to a JTextArea.
            window2.add(lb1); //adds to JPanel
        }

        if (line.equals("BGSTART")) {
            started = true;
        }
    }
    scanner.close();
}


How does it work?


  1. It goes through the lines.
  2. It the line equals BGEND, it sets started to false.
  3. If started equals true, it performs the actions with the line such as printing it.
  4. If the line equals BGSTART, it sets started to true.


The order of these steps is important, because BGSTART and BGEND don't have to be printed themselves.

Some other changes:


  • In the catch block, you should return, because if you don't, the method continues without the scanner is created.
  • The scanner should be closed at the end.


try

public void readFile(String fileString) throws IOException
 {

   File file = new File(fileString);
   Scanner scanner = null;
   String line = "";


  //access file
   try
   {
     scanner = new Scanner(file);
   }
   catch (FileNotFoundException e)
   {
     System.out.println("File not found.");
   }


   // if more lines in file, go to next line

   boolean gatheringData = false;

   while (scanner.hasNext())
   {
     line = scanner.next();
     if ( gatheringData )
     {
       if ( line.equals("BGEND") )
         break;

       System.out.println(line); // add this line to JTextArea

     }
     else if ( line.equals("BGSTART") )
     {
       gatheringData = true;
     }
   }
 }


这篇关于如何从.txt文件中读取但在文件中的两个点之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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