0

Here is my code which reads 0.2 million lines from file.

The line which contain correct URL code write it into true.txt and line which contain wrong URL code write it into wrong.txt.

The code writes in these files when it read all lines. I want to do a small change in it: When code has read one hundred lines it updates on wrong and true file.txt. Any one guide me how it is possible?

public static void main(String[] args) throws IOException {
    int coded = 0;
    int counter = 0;
    int timeout = 0;
    boolean canConnect = false;
    Scanner in = new Scanner(System.in);
    System.out.print("Enter the file name:");
    String filename = in .nextLine();

    BufferedReader br = null;
    FileWriter fw = null;
    FileWriter ft = null;
    String f = "truefile.txt";
    String strLine = "";

    br = new BufferedReader(new FileReader(filename));
    fw = new FileWriter("wrongfile.txt");
    ft = new FileWriter(f);

    while ((strLine = br.readLine()) != null) {

        System.out.println(strLine);
        if (strLine.contentEquals("arrow-uee.com")) {
            System.out.println("timeout");
        } else {
            try {
                URL u = new URL("http://" + strLine);
                HttpURLConnection huc = (HttpURLConnection) u.openConnection();
                huc.setUseCaches(false);
                huc.setConnectTimeout(timeout);
                huc.setRequestMethod("GET");

                coded = huc.getResponseCode();

                if (coded == HttpURLConnection.HTTP_OK) {
                    canConnect = true;
                }
                int code = coded / 100;
                if (code == 4 || code == 5 || coded == 303) {
                    fw.write(strLine + ":error:" + coded + "\n" + "\n");
                    System.out.println("domain is not ok error:" + coded);
                } else {
                    ft.write(strLine + "\n" + "\n");
                    System.out.println("domain is ok:");
                }
                counter++;
                System.out.println("total domain checked:" + counter);
            } catch (FileNotFoundException e) {
                System.err.println("File not found");
            } catch (IOException e) {
                System.out.println("domain not exist fatal error");
            }
        }

    }
    br.close();
    ft.close();
    fw.close();
}
bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • You could count the number of lines written and every 100 lines `flush` your output files. https://www.tutorialspoint.com/java/io/writer_flush.htm – Maximilian Peters May 22 '20 at 08:57
  • why would you want to write it every 100 lines, BufferedWriter is already writing it out automatically when it's buffer is full. As @MaximilianPeters mentioned, you can use `flush` to manually write to file, but use it if you really need to – tanyehzheng May 23 '20 at 05:20

1 Answers1

0

here is your answer simply add flush

public static void main(String[] args) throws IOException {
int coded = 0;
int counter = 0;
int timeout = 0;
boolean canConnect = false;
Scanner in = new Scanner(System.in);
System.out.print("Enter the file name:");
String filename = in .nextLine();

BufferedReader br = null;
FileWriter fw = null;
FileWriter ft = null;
String f = "truefile.txt";
String strLine = "";

br = new BufferedReader(new FileReader(filename));
fw = new FileWriter("wrongfile.txt");
ft = new FileWriter(f);

while ((strLine = br.readLine()) != null) {

    System.out.println(strLine);
    if (strLine.contentEquals("arrow-uee.com")) {
        System.out.println("timeout");
    } else {
        try {
            URL u = new URL("http://" + strLine);
            HttpURLConnection huc = (HttpURLConnection) u.openConnection();
            huc.setUseCaches(false);
            huc.setConnectTimeout(timeout);
            huc.setRequestMethod("GET");

            coded = huc.getResponseCode();

            if (coded == HttpURLConnection.HTTP_OK) {
                canConnect = true;
            }
            int code = coded / 100;
            if (code == 4 || code == 5 || coded == 303) {
                fw.write(strLine + ":error:" + coded + "\n" + "\n");
                System.out.println("domain is not ok error:" + coded);
            } else {
                ft.write(strLine + "\n" + "\n");
                System.out.println("domain is ok:");
            }
            counter++;
            System.out.println("total domain checked:" + counter);
        } catch (FileNotFoundException e) {
            System.err.println("File not found");
        } catch (IOException e) {
            System.out.println("domain not exist fatal error");
        }
    }
  ft.flush();
   fw.flush();
 }
br.close();
ft.close();
fw.close();