java FileReader,逐行读取文本

时间:2017-05-03 11:26:43

标签: java exception while-loop filereader

我有两节课。首先,类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>

这是来自filereader的代码:

    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;
    }
    }

这里是我可以从文本文件中编辑我想要的字符串的代码,我使用&#34; substring&#34;这很有效。但如果它到达最后一行,它在特定子串中没有值,则会出现错误....

如果我删除第1行和第2行以及文本文件中的alst行,则会出错: &#34;线程中的异常&#34; main&#34; java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:12     at java.lang.String.substring(String.java:1963)     在packagepackagechain.test4.main(test4.java:18)&#34;

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

错误: 线程&#34; main&#34;中的例外情况显示java.lang.NullPointerException     at packagepackagechain.test4.main(test4.java:16)

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!");
        }


    }
}   

2 个答案:

答案 0 :(得分:1)

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!");
    } 

    }
}

答案 1 :(得分:0)

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

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!
    }
  }
}