繁体   English   中英

java FileReader,逐行读取文本

[英]java FileReader, Read text Line by Line

我有两节课。 首先,是TEXT类:在这里,我阅读了一个6行的文本文件。 我只想逐行阅读,但这可行。 但我想从第三行开始,也想从最后一行开始,我只希望以

这是文本文件代码。

<?xml version="1.0" encoding="iso-8859-1"?>
<ICONS ERROR="false" USERNAME="WAZ" FORMAT="FLAT" RECORDS="3">
<icon ID="55"  NAM="A" />
<icon ID="87"  NAM="B" />
<icon ID="53"  NAM="C" />
</ICONS>

这是来自文件读取器的代码:

    package packagechain;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.stream.Stream;

   public class Text {

     String fileName;
     FileReader fr;
     BufferedReader in;
     Stream<String> lines;
     Iterator<String> l;
     boolean hasLine;

    public Text() throws FileNotFoundException{
         fileName = "E:/test30.xml";
         fr = new FileReader(fileName);
         in = new BufferedReader(fr);   
         lines = in.lines();
         l = lines.iterator();
         hasLine = true;
    }

    public String nextline() {
        String nl;

        if(l.hasNext()) {
            nl = l.next();
            //System.out.println(""+nl);

        }
        else {
            System.out.println("No new line!");
            hasLine = false;
            nl=null;
        }
        return nl;
    }
    }

这是我可以在文本文件中编辑我想要的字符串的代码,我使用“ substring”,并且可以正常工作。 但是,如果到最后一行,在特定子字符串中没有任何值,就会出现错误...。

如果删除文本文件中的第1行和第2行以及第Alst行,则会发生错误:“线程“主”中的异常” java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:java.lang.String.substring(String.java:1963 ),位于packagepackagechain.test4.main(test4.java:18)“

如果我在文本文件中添加第一行和第二行以及最后一行,则会出错。

错误:程序包packagechain.test4.main(test4.java:16)中的线程“ main”中的异常java.lang.NullPointerException

and here is the code:

 package packagechain;

import java.io.FileNotFoundException;

public class test4 {

    public static void main(String[] args) {
         Text m;
          String s;

          try {
             m = new Text();
             while(m.hasLine) {
                s = m.nextline();

                String r = s.substring(10,12);

                System.out.println(r);

             }
          } catch (FileNotFoundException e) {
             System.out.println("File not found!");
        }


    }
}   

您的Text类仅读取第一行,而您的主类正在为每次迭代实例化一个新的Text对象。 您的Text类可以使用方法行读取所有文件行,然后遍历它们并打印每一行。

package packagechain;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.stream.Stream;

public class Text {
     String fileName;
     FileReader fr;
     BufferedReader in;
     Stream<String> lines;
     Iterator<String> l;
     boolean hasLine;

    public Text() throws FileNotFoundException{
         fileName = "....4-line.txt";
         fr = new FileReader(fileName);
         in = new BufferedReader(fr);   
         lines = in.lines();
         l = lines.iterator();
         hasLine = true;
    }

    public String nexline() {

        if(l.hasNext()) {
            String nl = l.next();
            System.out.println("Next line; "+nl);
            return nl
        }
        else {
            System.out.println("No new line!");
            hasLine = false;
            return null;
        } 
    }    
}

主班:

package packagechain;

public class MainProgram {

  public static void main(String[] args) {

      Text m;
      String s;
      try {
         m = new Text();
         while(m.hasLine) {
            s = m.nexline();
            //EXAMPLE: for each line, print a substring starting from its third character
            if(s != null) System.out.println(s.substring(2));
            //Here I edit in future these Line , so it's important that i get line by line from my other class, becaus I Have to edit each line itself!
         }
      } catch (FileNotFoundException e) {
         System.out.println("File not found!");
    } 

    }
}

在您的程序中,每个迭代文本类对象和缓冲区读取器对象都被重新分配,因此它们仅返回第一行

package packagechain;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Text {


public BufferedReader getBufferReader() {

     BufferedReader in;

    String fileName = "...4-line.txt";

    FileReader fr = null;

    try {
        fr = new FileReader(fileName);
        in = new BufferedReader(fr);



    }  
    catch(FileNotFoundException fnfe){
        System.out.println("Can't open");

    }
    catch(IOException ioe){
        System.out.println("No new line!");
    }
    return  in;
    }
public void readLine(BufferedReader in){
    try {
 String line = in.readLine();


        System.out.println("Next line; "+line);

    catch(IOException ioe){
        System.out.println("No new line!");
    }
 }
}

第二类代码是:

package packagechain;

public class MainProgram {

  public static void main(String[] args) {
    while(true){
      Text m = new Text();
      BufferedReader in=m.getBufferReader();
      m.readLine(in);

      //Here I edit in future these Line , so it's important that i get line by line from my other class, becaus I Have to edit each line itself!
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM