本文介绍了如何从.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 我试图在这两个点之间读取它。 有什么建议吗? 谢谢。解决方案 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//contentBGENDI am trying to read it between those tow points.Any suggestions?Thank You. 解决方案 这篇关于如何从.txt文件中读取但在文件中的两个点之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 15:42