java - How to count position of empty row? -
i've got flat file this:
[doc] date=14.01.15 symbol=muka [content] [pos1] name=muka2,0 0,9l ml [doc] date=14.01.15 symbol=muka [content] [doc] date=14.01.15 symbol=muka [content] [pos1] name=muka2,0 0,9l ml
i want delete [doc] statement content empty. i'm trying checking line have string "[content]" , next line empty. want number of line (number of row line empty after "[content]") , add list. [doc] empty [content] part have 4 lines. when [content] position can substract 4 , delete lines between position , position -4.
the file after execution have this:
[doc] date=14.01.15 symbol=muka [content] [pos1] name=muka2,0 0,9l ml [doc] date=14.01.15 symbol=muka [content] [pos1] name=muka2,0 0,9l ml
i'm trying function:
public void countdesiredlines() throws ioexception { bufferedreader reader = new bufferedreader(new filereader( "d:\\temp.txt")); int lines = 0; boolean zawiera = false; while (reader.readline() != null) { lines++; string line = reader.readline(); if (zawiera == true) { zawiera = false; if ("".equals(line)) { pozycje.add(lines); } } if (line.startswith("[content]")) { zawiera = true; } reader.close(); system.out.println("wartość pod: " + pozycje.size()); // (int = 0; < pozycje.size(); i++) { // system.out.println("wartość pod: "+ pozycje.get(i).tostring()); // } } }
i've got errors like:
wartość pod: 0 java.io.ioexception: stream closed @ java.io.bufferedreader.ensureopen(bufferedreader.java:115) @ java.io.bufferedreader.readline(bufferedreader.java:310) @ java.io.bufferedreader.readline(bufferedreader.java:382) @ textformatter.countdesiredlines(textformatter.java:190) @ textformatter.main(textformatter.java:51)
i'm new in java. if have clues or advices please let me know. time.
you calling readline()
twice each iteration, read past end of file. change:
while (reader.readline() != null) { lines++; string line = reader.readline();
to
string line = null; while ((line = reader.readline()) != null) { lines++;
also, calling reader.close()
inside loop. have move outside loop logic work.
Comments
Post a Comment