java remove lines from file based on line number -
java remove lines from file based on line number -
is there java library remove lines based on line number?
my aim remove lines based on line numbers.
example
pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit down amet, ante. donec european union libero sit down amet quam egestas semper. aenean ultricies mi vitae est. mauris placerat eleifend leo. quisque sit down amet est et sapien ullamcorper pharetra. vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit down amet, wisi. aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. donec non enim in turpis pulvinar facilisis. ut felis. praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, european union vulputate magna eros european union erat. aliquam erat volutpat. nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metusremove lines 3,4,5 output should be
pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit down amet, ante. eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. donec non enim in turpis pulvinar facilisis. ut felis. praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, european union vulputate magna eros european union erat. aliquam erat volutpat. nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metusi have made working method
import org.apache.commons.io.fileutils; //... private string removelines(list<integer> removelines, file text) throws ioexception { list<string> textlines = fileutils.readlines(text, standardcharsets.utf_8); if (argsstrings.contains(flags.debug_flag)) { string format = string.format("%d %d need removed", removelines.size(), textlines.size()); logger.getlogger(app.class.getname()).log(level.info, format); } int = textlines.size(); while (!removelines.isempty()) { integer line = new integer(i + 1); if (argsstrings.contains(flags.debug_flag)) { string format = string.format("%4d. line inspected", line); logger.getlogger(app.class.getname()).log(level.info, format); } if (removelines.contains(line)) { if (argsstrings.contains(flags.debug_flag)) { string format = string.format("%4d. line removed, line text below%n%s", line, textlines.get(i)); logger.getlogger(app.class.getname()).log(level.info, format); } textlines.remove(i); removelines.remove(line); } i--; } stringbuilder sb = new stringbuilder(); (string line : textlines) { sb.append(line); sb.append(system.lineseparator()); } homecoming sb.tostring(); }
is there simpler way this?
since don't seem mind reading entire file memory, seek this:
private string removelines(list<integer> removelines, file text) throws ioexception { list<string> textlines = fileutils.readlines(text, standardcharsets.utf_8); for(integer removeline : removelines) { textlines.remove(removeline - 1); // -1, depending on how specify removelines } stringbuilder builder = new stringbuilder(); for(string line : textlines) { builder.append(line).append(system.lineseparator()); } homecoming builder.tostring(); }
alternatively, this, using lineiterator
:
private string removelines(list<integer> removelines, file text) throws ioexception { stringbuilder builder = new stringbuilder(); lineiterator = fileutils.lineiterator(text, standardcharsets.utf_8); seek { (int = 1; it.hasnext(); i++) { string line = it.nextline(); if(!removelines.contains(i)) { builder.append(line).append(system.lineseparator()); } } } { lineiterator.closequietly(it); } homecoming builder.tostring(); }
java file file-io numbers line
Comments
Post a Comment