string - writing in text file in java -
i have text file contained large number of words, , want divide words writing ** every 4 words. did until adding first ** ( first 4 words) , have difficulties in putting other stars.
here code until (i using java)
import java.io.*; public class insert { public static void main(string args[]){ try { insert in = new insert(); int tc=4; in.insertstringinfile (new file("d:/users//im080828/desktop/souad/project/reduction/weight/outdata/d.txt"), tc, "**"); } catch (exception e) { e.printstacktrace(); } } public void insertstringinfile(file infile, int lineno, string linetobeinserted) throws exception { // temp file file outfile = new file("$$$$$$$$.tmp"); // input fileinputstream fis = new fileinputstream(infile); bufferedreader in = new bufferedreader (new inputstreamreader(fis)); // output fileoutputstream fos = new fileoutputstream(outfile); printwriter out = new printwriter(fos); string thisline = ""; int =1; while ((thisline = in.readline()) != null) { if(i == lineno) out.println(linetobeinserted); out.println(thisline); i++; } out.flush(); out.close(); in.close(); infile.delete(); outfile.renameto(infile); } }
please.. give me ideas
thanks :)
when if (i == lineno)
true if (i==4)
, behavior normal. need use modulo operator if ((i % lineno) == 0)
star every lines. http://www.dreamincode.net/forums/topic/273783-the-use-of-the-modulo-operator/
Comments
Post a Comment