java - How can I read from the next line of a text file, and pause, allowing me to read from the line after that later? -
i wrote program generates random numbers 2 text files , random letters third according 2 constant files. need read each text file, line line, , put them together. program suggestion found here doesn't situation. when try approach reads lines until it's done without allowing me option pause it, go different file, etc.
ideally find way read next line, , later go line after that. maybe kind of variable hold place in reading or something.
public static void mergeproductcodestofile(string prefixfile, string inlinefile, string suffixfile, string productfile) throws ioexception { try (bufferedreader br = new bufferedreader(new filereader(prefixfile))) { string line; while ((line = br.readline()) != null) { try (printwriter out = new printwriter(new bufferedwriter(new filewriter(productfile, true)))) { out.print(line); //this print next digit right } catch (filenotfoundexception e) { system.err.println("file error: " + e.getmessage()); } } } }
edit: digits being created according following. basically, constants tell how many digits create in each line , how many lines create. need combine these without deleting either text file.
public static void writerandomcodestofile(string codefile, char fromchar, char tochar, int numberofcharacterspercode, int numberofcodestogenerate) throws ioexception { (int = 1; <= product_count; i++) { int = 0; if (codefile == "inline.txt") { (i = 1; <= characters_per_code; i++) { int digit = (int)(math.random() * 10); try (printwriter out = new printwriter(new bufferedwriter(new filewriter(codefile, true)))) { out.print(digit); //this print next digit right } catch (filenotfoundexception e) { system.err.println("file error: " + e.getmessage()); system.exit(1); } } } if ((codefile == "prefix.txt") || (codefile == "suffix.txt")) { (i = 1; <= characters_per_code; i++) { random r = new random(); char digit = (char)(r.nextint(26) + 'a'); digit = character.touppercase(digit); try (printwriter out = new printwriter(new bufferedwriter(new filewriter(codefile, true)))) { out.print(digit); } catch (filenotfoundexception e) { system.err.println("file error: " + e.getmessage()); system.exit(1); } } } //this take text file next line if (i >= characters_per_code) { { random r = new random(); char digit = (char)(r.nextint(26) + 'a'); try (printwriter out = new printwriter(new bufferedwriter(new filewriter(codefile, true)))) { out.println(""); //this return new line next loop } catch (filenotfoundexception e) { system.err.println("file error: " + e.getmessage()); system.exit(1); } } } } system.out.println(codefile + " created."); }// end writerandomcodestofile()
being respectfull code, this:
public static void mergeproductcodestofile(string prefixfile, string inlinefile, string suffixfile, string productfile) throws ioexception { try (bufferedreader prefixreader = new bufferedreader(new filereader(prefixfile)); bufferedreader inlinereader = new bufferedreader(new filereader(inlinefile)); bufferedreader suffixreader = new bufferedreader(new filereader(suffixfile))) { stringbuilder line = new stringbuilder(); string prefix, inline, suffix; while ((prefix = prefixreader.readline()) != null) { //assuming nothing fails , files equals in # of lines. inline = inlinereader.readline(); suffix = suffixreader.readline(); line.append(prefix).append(inline).append(suffix).append("\r\n"); // write ... } } {/*close writers*/} }
some exceptions may thrown.
i hope don't implement in 1 single method. can make use of iterators too, or simple reader class (method).
i wouldn't use list load data @ least guarantee files low sized , can spare memory usage.
Comments
Post a Comment