Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem was with the wtf method. Your endless loop was in there when you wrote:</p> <pre><code>while(line==null){ bw.write(ltw); bw.newLine(); } </code></pre> <p>BufferedReader states it <code>returns null if the end of the stream has been reached.</code> Since your files are initially empty (I'm assuming), this will constantly be true, and you will continue writing new lines and your String.</p> <p>I was playing around with your code and rewrote it to something like this.</p> <pre><code>import java.util.*; import java.io.*; public class fread { public static void main (String[] args) { Scanner scan = new Scanner(System.in); String even = "2.txt"; String odd = "1.txt"; String line = ""; int lineNumber = 0; while (scan.hasNext() ) { line = scan.nextLine(); if (lineNumber++ % 2 == 1) writeText(even, line); else writeText(odd, line); } } static void writeText(String filename, String ltw) { try { BufferedWriter bw = new BufferedWriter(new FileWriter(filename, true)); bw.write(ltw); bw.newLine(); bw.close(); } catch ( IOException e) { e.printStackTrace(); } } } </code></pre> <p>It uses input redirection, so typing <code>java fread &lt; database.txt</code> will give you your results, however this only does appends to a file. If your files do not exist, or are initially empty, this will work as you expect. You'll have to specify this code to the needs of the program. Regardless, you should be able to take my writeText method and incorporate it into your program to be able to to get yours working.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload