如何在JAVA中读取.txt文件的特定部分
我一直在试图找出如何从.txt文件中读取。我知道如何读取整个文件,但在文件中的两个特定点之间读取时遇到困难。 我还尝试使用scanner类,这是我到目前为止所拥有的:如何在JAVA中读取.txt文件的特定部分,java,Java,我一直在试图找出如何从.txt文件中读取。我知道如何读取整个文件,但在文件中的两个特定点之间读取时遇到困难。 我还尝试使用scanner类,这是我到目前为止所拥有的: public void readFiles(String fileString)throws FileNotFoundException{ file = new File(fileString); Scanner scanner = null; line="";
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文件如下所示:
BGSTART
//内容
BGEND
当我运行程序时,面板上没有任何内容
我试图在这两点之间阅读它。我没有很多从txt文件中阅读的经验。
有什么建议吗?
谢谢。根据@SubOptimal的问题,假设
BGSTART
和BGEND
在不同的行上,您需要执行以下操作:
boolean tokenFound = false;
while (scanner.hasNextLine())
{
line = scanner.nextLine();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
}
else if (line.equals("BGEND"))
{
tokenFound = false;
}
if(tokenFound)
{
System.out.println(line);
lb1.append(line); //attaches to a JTextArea.
window2.add(lb1);//adds to JPanel
}
}
一些改进:
try {
Scanner scanner = new Scanner(new FileInputStream(file));
//Moved the rest of the code within the try block.
//As it was before, if there where any problems loading the file, you would have gotten an error message (File not found)
//as per your catch block but you would then have gotten an unhandled null pointer exception when you would have tried to
//execute this bit: scanner.hasNextLine()
boolean tokenFound = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
} else if (line.equals("BGEND")) {
tokenFound = false;
}
if ((tokenFound) && (!line.equals("BGSTART"))) {
System.out.println(line);
//I am not sure what is happening here.
//lb1.append(line); //attaches to a JTextArea.
//window2.add(lb1);//adds to JPanel
}
}
} catch (Exception e) {
System.out.println("File not found.");
}
文件内容:
do not show line one
do not show line two
BGSTART
this is a line
this is another line
this is a third line
BGEND
do not show line three
do not show line four
根据@SubOptimal的问题,假设
BGSTART
和BGEND
在不同的行上,您需要执行以下操作:
boolean tokenFound = false;
while (scanner.hasNextLine())
{
line = scanner.nextLine();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
}
else if (line.equals("BGEND"))
{
tokenFound = false;
}
if(tokenFound)
{
System.out.println(line);
lb1.append(line); //attaches to a JTextArea.
window2.add(lb1);//adds to JPanel
}
}
一些改进:
try {
Scanner scanner = new Scanner(new FileInputStream(file));
//Moved the rest of the code within the try block.
//As it was before, if there where any problems loading the file, you would have gotten an error message (File not found)
//as per your catch block but you would then have gotten an unhandled null pointer exception when you would have tried to
//execute this bit: scanner.hasNextLine()
boolean tokenFound = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
} else if (line.equals("BGEND")) {
tokenFound = false;
}
if ((tokenFound) && (!line.equals("BGSTART"))) {
System.out.println(line);
//I am not sure what is happening here.
//lb1.append(line); //attaches to a JTextArea.
//window2.add(lb1);//adds to JPanel
}
}
} catch (Exception e) {
System.out.println("File not found.");
}
文件内容:
do not show line one
do not show line two
BGSTART
this is a line
this is another line
this is a third line
BGEND
do not show line three
do not show line four
根据@SubOptimal的问题,假设
BGSTART
和BGEND
在不同的行上,您需要执行以下操作:
boolean tokenFound = false;
while (scanner.hasNextLine())
{
line = scanner.nextLine();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
}
else if (line.equals("BGEND"))
{
tokenFound = false;
}
if(tokenFound)
{
System.out.println(line);
lb1.append(line); //attaches to a JTextArea.
window2.add(lb1);//adds to JPanel
}
}
一些改进:
try {
Scanner scanner = new Scanner(new FileInputStream(file));
//Moved the rest of the code within the try block.
//As it was before, if there where any problems loading the file, you would have gotten an error message (File not found)
//as per your catch block but you would then have gotten an unhandled null pointer exception when you would have tried to
//execute this bit: scanner.hasNextLine()
boolean tokenFound = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
} else if (line.equals("BGEND")) {
tokenFound = false;
}
if ((tokenFound) && (!line.equals("BGSTART"))) {
System.out.println(line);
//I am not sure what is happening here.
//lb1.append(line); //attaches to a JTextArea.
//window2.add(lb1);//adds to JPanel
}
}
} catch (Exception e) {
System.out.println("File not found.");
}
文件内容:
do not show line one
do not show line two
BGSTART
this is a line
this is another line
this is a third line
BGEND
do not show line three
do not show line four
根据@SubOptimal的问题,假设
BGSTART
和BGEND
在不同的行上,您需要执行以下操作:
boolean tokenFound = false;
while (scanner.hasNextLine())
{
line = scanner.nextLine();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
}
else if (line.equals("BGEND"))
{
tokenFound = false;
}
if(tokenFound)
{
System.out.println(line);
lb1.append(line); //attaches to a JTextArea.
window2.add(lb1);//adds to JPanel
}
}
一些改进:
try {
Scanner scanner = new Scanner(new FileInputStream(file));
//Moved the rest of the code within the try block.
//As it was before, if there where any problems loading the file, you would have gotten an error message (File not found)
//as per your catch block but you would then have gotten an unhandled null pointer exception when you would have tried to
//execute this bit: scanner.hasNextLine()
boolean tokenFound = false;
while (scanner.hasNextLine()) {
String line = scanner.nextLine().trim();
//line, not scanner.
if (line.equals("BGSTART")) //tag in the txt to locate position
{
tokenFound = true;
} else if (line.equals("BGEND")) {
tokenFound = false;
}
if ((tokenFound) && (!line.equals("BGSTART"))) {
System.out.println(line);
//I am not sure what is happening here.
//lb1.append(line); //attaches to a JTextArea.
//window2.add(lb1);//adds to JPanel
}
}
} catch (Exception e) {
System.out.println("File not found.");
}
文件内容:
do not show line one
do not show line two
BGSTART
this is a line
this is another line
this is a third line
BGEND
do not show line three
do not show line four
为什么不使用子字符串?找到BGSTART和BGEND后,您可以在以下代码行中的某个位置捕获它之间的字符串:
StringBuilder sb= new StringBuilder();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
sb.append(line);
}
String capturedString = sampleString.substring(sb.toString().indexOf("BGSTART"),
sb.toString().indexOf("BGEND"));
希望这有帮助为什么不使用子字符串?找到BGSTART和BGEND后,您可以在以下代码行中的某个位置捕获它之间的字符串:
StringBuilder sb= new StringBuilder();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
sb.append(line);
}
String capturedString = sampleString.substring(sb.toString().indexOf("BGSTART"),
sb.toString().indexOf("BGEND"));
希望这有帮助为什么不使用子字符串?找到BGSTART和BGEND后,您可以在以下代码行中的某个位置捕获它之间的字符串:
StringBuilder sb= new StringBuilder();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
sb.append(line);
}
String capturedString = sampleString.substring(sb.toString().indexOf("BGSTART"),
sb.toString().indexOf("BGEND"));
希望这有帮助为什么不使用子字符串?找到BGSTART和BGEND后,您可以在以下代码行中的某个位置捕获它之间的字符串:
StringBuilder sb= new StringBuilder();
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
sb.append(line);
}
String capturedString = sampleString.substring(sb.toString().indexOf("BGSTART"),
sb.toString().indexOf("BGEND"));
希望这有助于几乎很好,但您正在做:
if (scanner.equals("BGSTART")) //tag in the txt to locate position
您应该在行
变量而不是扫描仪
中查找文件内容所以试试这个:
if (line.equals("BGSTART")) //tag in the txt to locate position
几乎不错,但你正在做:
if (scanner.equals("BGSTART")) //tag in the txt to locate position
您应该在行
变量而不是扫描仪
中查找文件内容所以试试这个:
if (line.equals("BGSTART")) //tag in the txt to locate position
几乎不错,但你正在做:
if (scanner.equals("BGSTART")) //tag in the txt to locate position
您应该在行
变量而不是扫描仪
中查找文件内容所以试试这个:
if (line.equals("BGSTART")) //tag in the txt to locate position
几乎不错,但你正在做:
if (scanner.equals("BGSTART")) //tag in the txt to locate position
您应该在行
变量而不是扫描仪
中查找文件内容所以试试这个:
if (line.equals("BGSTART")) //tag in the txt to locate position
令牌
BGSTART
和BGEND
是否始终位于单独的行上?请签出RandomAccessFile
。这是一个关于它的说明是的,它们在单独的行上。令牌BGSTART
和BGEND
始终在单独的行上吗?请查看RandomAccessFile
。这是一个关于它的说明是的,它们在单独的行上。令牌BGSTART
和BGEND
始终在单独的行上吗?请查看RandomAccessFile
。这是一个关于它的说明是的,它们在单独的行上。令牌BGSTART
和BGEND
始终在单独的行上吗?请查看RandomAccessFile
。这是一个关于它的例子是的,它们在一个单独的行上。你的代码按预期打印,但它会用每个单词而不是短语创建一个新行。我尝试将line更改为line=scanner.nextLine(),但它不打印任何内容。我还得到一个异常“java.util.NoSuchElementException”,似乎在同一行。@Varoag:对不起,我的错,我做了一些更改,您的代码现在是这样的吗?(人们通常从文件中读取行,以某种方式读取您的next
调用nextLine
。)我添加了更改后的代码。现在它没有空格。基本上,文本是一整行。您建议使用行拆分器吗?@Varoag:为了避免多次编辑,您的BGSTART和BDEND是否会在没有其他单词的情况下位于单独的行上?此外,单词之间的空格也会被省略,您的代码也会按预期打印,但它会使用每个单词而不是短语创建新行。我尝试将line更改为line=scanner.nextLine(),但它不打印任何内容。我还得到一个异常“java.util.NoSuchElementException”,似乎在同一行。@Varoag:对不起,我的错,我做了一些更改,您的代码现在是这样的吗?(人们通常从文件中读取行,以某种方式读取您的next
调用nextLine
。)我添加了更改后的代码。现在它没有空格。基本上,文本是一整行。您建议使用行拆分器吗?@Varoag:为了避免多次编辑,您的BGSTART和BDEND是否会在没有其他单词的情况下位于单独的行上?此外,单词之间的空格也会被省略,您的代码也会按预期打印,但它会使用每个单词而不是短语创建新行。我尝试将line更改为line=scanner.nextLine(),但它不打印任何内容。我还得到一个异常“java.util.NoSuchElementException”,似乎在同一行。@Varoag:对不起,我的错,我做了一些更改,您的代码现在是这样的吗?(人们通常从文件中读取行,以某种方式读取您的next
调用nextLine
。)我添加了更改后的代码。现在它没有空格。基本上,文本是一整行。您建议使用行拆分器吗?@Varoag:为了避免多次编辑,您的BGSTART和BDEND是否会在没有其他单词的情况下位于单独的行上?此外,单词之间的空格也会被省略,您的代码也会按预期打印,但它会使用每个单词而不是短语创建新行。我尝试将line更改为line=scanner.nextLine(),但它不打印任何内容。我还得到一个异常“java.util.NoSuchElementException”,似乎在同一行。@Varoag:对不起,我的错,我做了一些更改,您的代码现在是这样的吗?(人们通常以某种方式从文件中读取行