Java 查找文本文件的特定行(而不是按行号),并将其存储为新字符串

Java 查找文本文件的特定行(而不是按行号),并将其存储为新字符串,java,arrays,text-files,Java,Arrays,Text Files,我正在尝试使用FileReader和BufferedReader类读取java中的文本文件。在一个在线教程之后,我上了两门课,一门叫ReadFile,另一门叫FileData。 然后,我尝试提取文本文件的一小部分(即在“ENTITIES”和“ENDSEC”行之间)。最后,我想告诉程序在上面提到的数据之间找到一条特定的线,并将其存储为Xvalue,我可以稍后使用。 我真的很难想出如何做的最后一部分…任何帮助将非常感谢 //文件数据类 package textfiles; impo

我正在尝试使用FileReader和BufferedReader类读取java中的文本文件。在一个在线教程之后,我上了两门课,一门叫ReadFile,另一门叫FileData。 然后,我尝试提取文本文件的一小部分(即在“ENTITIES”和“ENDSEC”行之间)。最后,我想告诉程序在上面提到的数据之间找到一条特定的线,并将其存储为Xvalue,我可以稍后使用。 我真的很难想出如何做的最后一部分…任何帮助将非常感谢

//文件数据类

    package textfiles;

    import java.io.IOException; 


    public class FileData {

public static void main (String[] args) throws IOException {

    String file_name = "C:/Point.txt";

    try {

        ReadFile file = new ReadFile (file_name);
        String[] aryLines = file.OpenFile();

        int i;
        for ( i=0; i < aryLines.length; i++ ) {
        System.out.println( aryLines[ i ] ) ;
}

    }

    catch (IOException e) {
        System.out.println(e.getMessage() );
    }

 }

 }
打包文本文件;
导入java.io.IOException;
公共类文件数据{
公共静态void main(字符串[]args)引发IOException{
字符串文件_name=“C:/Point.txt”;
试一试{
ReadFile file=新的ReadFile(文件名);
字符串[]aryLines=file.OpenFile();
int i;
对于(i=0;i
//ReadFile类

    package textfiles;

    import java.io.IOException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.lang.String;

    public class ReadFile {

private String path;

public ReadFile (String file_path) {
    path = file_path;
}

public String[] OpenFile() throws IOException {

    FileReader fr = new FileReader (path);
    BufferedReader textReader = new BufferedReader (fr); 

     int numberOfLines = readLines();
      String[] textData = new String[numberOfLines];
     String nextline = "";

     int i;
             // String Xvalue; 

    for (i=0; i < numberOfLines; i++) {
         String oneline = textReader.readLine();

         int j = 0;

         if (oneline.equals("ENTITIES")) {
             nextline = oneline;
             System.out.println(oneline);
             while (!nextline.equals("ENDSEC")) {
                 nextline = textReader.readLine();
                 textData[j] = nextline;

            //  xvalue = ..........

                 j = j + 1;
                 i = i+1;
             }
         }       
         //textData[i] = textReader.readLine();
     }

     textReader.close( );
     return textData;

}

int readLines() throws IOException {

    FileReader file_to_read = new FileReader (path);
    BufferedReader bf = new BufferedReader (file_to_read);

    String aLine;
    int numberOfLines = 0;

    while (( aLine = bf.readLine()) != null ) {
        numberOfLines ++;
    }

    bf.close ();

    return numberOfLines;

}

}
打包文本文件;
导入java.io.IOException;
导入java.io.FileReader;
导入java.io.BufferedReader;
导入java.lang.String;
公共类读取文件{
私有字符串路径;
公共读取文件(字符串文件\u路径){
路径=文件路径;
}
公共字符串[]OpenFile()引发IOException{
FileReader fr=新的FileReader(路径);
BufferedReader textReader=新的BufferedReader(fr);
int numberOfLines=readLines();
字符串[]文本数据=新字符串[numberOfLines];
字符串nextline=“”;
int i;
//字符串Xvalue;
对于(i=0;i
当我做对后,您想存储实体和ENDSEC之间的每一行吗

如果是,您可以简单地定义一个StringBuffer,并将介于两者之间的所有内容附加到关键字

// This could you would put outside the while loop
StringBuffer xValues = new StringBuffer();

// This would be in the while loop and you append all the lines in the buffer
xValues.append(nextline);
如果您想在这些关键字之间存储更具体的数据,那么您可能需要使用正则表达式,取出所需的数据并将其放入设计的数据结构(我们自己定义的类)

顺便说一句,我认为通过以下代码,您可以更轻松地阅读该文件:

BufferedReader reader = new BufferedReader(new
InputStreamReader(this.getClass().getResourceAsStream(filename)));

try {
   while ((line = reader.readLine()) != null) {
     if(line.equals("ENTITIES") {
       ...
     }

} (IOException e) {
   System.out.println("IO Exception. Couldn't Read the file!");
}
然后,您不必首先读取文件有多少行。你只要开始读到结尾:)

编辑:

我还是不知道我是否理解正确。因此,如果实体点10 1333.888 20 333.5555 ENDSEC是一条线,那么您可以使用拆分(“”方法

让我举例说明:

String line = "";
String[] parts = line.split(" ");
float xValue = parts[2]; // would store 10
float yValue = parts[3]; // would store 1333.888
float zValue = parts[4]; // would store 20
float ...    = parts[5]; // would store 333.5555
EDIT2:

或者每个点(x,y,…)都在另一条线上

因此,文件内容如下所示:

ENTITIES POINT 
10 
1333.888 // <-- you want this one as xValue 
20 
333.5555 // <-- and this one as yvalue?
ENDSEC

BufferedReader reader = new BufferedReader(new
InputStreamReader(this.getClass().getResourceAsStream(filename)));

try {
   while ((line = reader.readLine()) != null) {
     if(line.equals("ENTITIES") {

        // read next line
        line = reader.readLine();
        if(line.equals("10") {
         // read next line to get the value
         line = reader.readLine(); // read next line to get the value
         float xValue = Float.parseFloat(line);
        }

        line = reader.readLine();

        if(line.equals("20") {
           // read next line to get the value
           line = reader.readLine(); 
           float yValue = Float.parseFloaT(line);
        }
     }

} (IOException e) {
   System.out.println("IO Exception. Couldn't Read the file!");
}
实体点
10

1333.888/当我做对后,您想存储实体和ENDSEC之间的每一行吗

如果是,您可以简单地定义一个StringBuffer,并将介于两者之间的所有内容附加到关键字

// This could you would put outside the while loop
StringBuffer xValues = new StringBuffer();

// This would be in the while loop and you append all the lines in the buffer
xValues.append(nextline);
如果您想在这些关键字之间存储更具体的数据,那么您可能需要使用正则表达式,取出所需的数据并将其放入设计的数据结构(我们自己定义的类)

顺便说一句,我认为通过以下代码,您可以更轻松地阅读该文件:

BufferedReader reader = new BufferedReader(new
InputStreamReader(this.getClass().getResourceAsStream(filename)));

try {
   while ((line = reader.readLine()) != null) {
     if(line.equals("ENTITIES") {
       ...
     }

} (IOException e) {
   System.out.println("IO Exception. Couldn't Read the file!");
}
然后,您不必首先读取文件有多少行。你只要开始读到结尾:)

编辑:

我还是不知道我是否理解正确。因此,如果实体点10 1333.888 20 333.5555 ENDSEC是一条线,那么您可以使用拆分(“”方法

让我举例说明:

String line = "";
String[] parts = line.split(" ");
float xValue = parts[2]; // would store 10
float yValue = parts[3]; // would store 1333.888
float zValue = parts[4]; // would store 20
float ...    = parts[5]; // would store 333.5555
EDIT2:

或者每个点(x,y,…)都在另一条线上

因此,文件内容如下所示:

ENTITIES POINT 
10 
1333.888 // <-- you want this one as xValue 
20 
333.5555 // <-- and this one as yvalue?
ENDSEC

BufferedReader reader = new BufferedReader(new
InputStreamReader(this.getClass().getResourceAsStream(filename)));

try {
   while ((line = reader.readLine()) != null) {
     if(line.equals("ENTITIES") {

        // read next line
        line = reader.readLine();
        if(line.equals("10") {
         // read next line to get the value
         line = reader.readLine(); // read next line to get the value
         float xValue = Float.parseFloat(line);
        }

        line = reader.readLine();

        if(line.equals("20") {
           // read next line to get the value
           line = reader.readLine(); 
           float yValue = Float.parseFloaT(line);
        }
     }

} (IOException e) {
   System.out.println("IO Exception. Couldn't Read the file!");
}
实体点
10

1333.888/我不知道您具体要找的是哪一行,但以下是一些您可能希望用于执行此类操作的方法:

private static String START_LINE = "ENTITIES";
private static String END_LINE = "ENDSEC";

public static List<String> getSpecificLines(Srting filename) throws IOException{
    List<String> specificLines = new LinkedList<String>();
    Scanner sc = null;
    try {
        boolean foundStartLine = false;
        boolean foundEndLine = false;
        sc = new Scanner(new BufferedReader(new FileReader(filename)));
        while (!foundEndLine && sc.hasNext()) {
            String line = sc.nextLine();
            foundStartLine = foundStartLine || line.equals(START_LINE);
            foundEndLine = foundEndLine || line.equals(END_LINE);
            if(foundStartLine && !foundEndLine){
                specificLines.add(line);
            }
        }
    } finally {
        if (sc != null) {
            sc.close();
        }
    }
    return specificLines;
}

public static String getSpecificLine(List<String> specificLines){
    for(String line : specificLines){
        if(isSpecific(line)){
            return line;
        }
    }
    return null;
}

public static boolean isSpecific(String line){
    // What makes the String special??
}
private静态字符串START\u LINE=“ENTITIES”;
私有静态字符串END_LINE=“ENDSEC”;
公共静态列表getSpecificLines(Srting文件名)引发IOException{
List specificLines=newlinkedlist();
扫描仪sc=空;
试一试{
布尔线=假;
布尔值foundEndLine=false;
sc=新的扫描仪(新的BufferedReader(新的文件读取器(文件名));
而(!foundEndLine&&sc.hasNext()){
字符串行=sc.nextLine();
Foundstartine=Foundstartine | |行。等于(起始|行);
foundEndLine=foundEndLine | | line.equals(END|u line);
if(foundline&!foundEndLine){
具体行。添加(行);
}
}
}最后{
如果(sc!=null){
sc.close();
}
}
返回指定行;
}
公共静态字符串getSpecificLine(列出specificLines){
用于(字符串行:specificLines){
如果(isSpecific(行)){
回流线;
}
}
返回nul