Vitalij Kornijenko
Vitalij Kornijenko

Reputation: 559

Writing from file to file program

I'm trying to make a program that reads a line from the database.txt and writes an odd/even numbered lines to files 1.txt and 2.txt. F.e. 1st(odd) line of the database.txt goes to 1.txt and the 2nd(even) line of the database.txt goes goes to 2.txt

Here's the code I got so far:

import java.io.*;
import java.util.Scanner;

public class Main {

    public Main(){
        op(null);
    }

    public void op(String args[]){
        try{
            FileReader fr = new FileReader("database.txt");
            BufferedReader reader = new BufferedReader(fr);
            String line = reader.readLine();
            Scanner scan = null;
            int ln = 1;
            String even = "2txt";
            String odd = "1.txt";
            while ((line=reader.readLine())!=null){
                scan = new Scanner(line);
                if(ln%2==0){
                    wtf(even, line);
                }else{
                    wtf(odd, line);
                }
                ln++;
                line=reader.readLine();
            }
            reader.close();
        }
        catch (FileNotFoundException e){
            System.out.println("File not found");
        } 
        catch (IOException e) {
        System.out.println("Impossibru to read");
        }
    }

    public void wtf(String filename, String ltw){
        try
        {
            FileReader fr = new FileReader(filename);
            BufferedReader reader = new BufferedReader(fr);
            String line = reader.readLine();

            FileWriter writer = new FileWriter(filename);
            BufferedWriter bw = new BufferedWriter(writer);
            while(line==null){
                bw.write(ltw);
                bw.newLine();
            }
            bw.close();
        }
        catch ( IOException e)
        {
        }
    }
    }

At the moment its on an infinite loop reads only the 2nd line and spams it to 1.txt

Upvotes: 1

Views: 163

Answers (5)

Clark Kent
Clark Kent

Reputation: 1176

The problem was with the wtf method. Your endless loop was in there when you wrote:

while(line==null){
                bw.write(ltw);
                bw.newLine();
}

BufferedReader states it returns null if the end of the stream has been reached. Since your files are initially empty (I'm assuming), this will constantly be true, and you will continue writing new lines and your String.

I was playing around with your code and rewrote it to something like this.

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();
    }
  }
}

It uses input redirection, so typing java fread < database.txt 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.

Upvotes: 1

internals-in
internals-in

Reputation: 5038

      if(ln%2==0){
            wtf(even, line);
        }else{
            wtf(odd, line);
             } //<------------here ?
            ln++;
            line=reader.readLine();
    // }
    }
    reader.close();

update:

   while((line=reader.readLine())!=null){
               *
               *
               *
            line=reader.readLine() ; //is this line  required ?

update:

Check this also in wtf()

 while(line==null){
                bw.write(ltw);
                bw.newLine();
        }

Upvotes: 2

christopher
christopher

Reputation: 27336

Your infinite loop is happening here:

if(ln%2==0)
    wtf(even, line);

ln starts at 0. 0 % X = 0, therefore it is always considered an even number, and you just keep spamming out to the same line. Add the increment to your even clause.

Interesting side note

You have another infinite loop, because if the value is even, or 0, then you never read the next line.

Solution

if(ln%2==0){
    wtf(even, line);
    ln ++;
    line = reader.readline();

Upvotes: 1

nanofarad
nanofarad

Reputation: 41271

Within the:

if(ln%2==0)

block you also need to add ln++;

Also move the

line=reader.readLine();

to directly outside the else block.

As an alternative, this is a somewhat reworked method:

Remove all of the line=reader.readLine();s including the first.

Rewrite the loop as:

while ((line=reader.readLine())!=null){

        if(ln%2==0){
            wtf(even, line);
        }else{
            wtf(odd, line);

        }
        ln++;
}

Upvotes: 3

Pete B.
Pete B.

Reputation: 3276

You are not incrementing the line count.

  while (line!=null){
                scan = new Scanner(line);
                if(ln%2==0){
                    wtf(even, line);
                    ln++;
                }else{
                    wtf(odd, line);
                    ln++;
                    line=reader.readLine();
                }
            }
            reader.close();
        }

Upvotes: 2

Related Questions