Hans
Hans

Reputation: 25

java FileReader, Read text Line by Line

I have two classes. First, the class TEXT: Here I read a text file with 6 lines. I only want to read line by line, but this works. but I want to start from the third line and also skipo the last one, I only want that the lines which start with

Here is textfile code.

<?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>

and here is the code from the 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;
    }
    }

and here is the code where i can edit my string which i want from the textfile , i use "substring" and this works. but if it comes to the last line where it is no value at the specific substring it comes to an error....

Error if i delete line 1 and 2 and the alst line in my textfile: "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 12 at java.lang.String.substring(String.java:1963) at packagepackagechain.test4.main(test4.java:18)"

Error if I add line 1 and two and the last one in my textfile ..

Error: Exception in thread "main" 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!");
        }


    }
}   

Upvotes: 0

Views: 1745

Answers (2)

Araf
Araf

Reputation: 530

In your program each of iteration text class object and buffer reader object are reallocated so they only return first line

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

Second class code is:

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

Upvotes: 0

Spock
Spock

Reputation: 327

Your Text class reads just the first line and your main class is instanciating a new Text object for each iteration. Your Text class could use the method lines to read all file lines and then iterates through them an prints each line.

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

Main class:

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

    }
}

Upvotes: 1

Related Questions