【问题标题】:Write lines to files every 100 lines每 100 行向文件写入行
【发布时间】:2020-09-08 23:39:53
【问题描述】:

这是我从文件中读取 20 万行的代码。

包含正确URL代码的行将其写入true.txt,包含错误URL代码的行将其写入wrong.txt

代码在读取所有行时写入这些文件。我想对它做一个小改动:当代码读取一百行时,它会更新错误和正确的file.txt。谁能指导我怎么可能?

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

【问题讨论】:

  • 您可以计算写入的行数和每 100 行 flush 您的输出文件。 tutorialspoint.com/java/io/writer_flush.htm
  • 为什么要每 100 行写入一次,BufferedWriter 已经在缓冲区满时自动写入。正如@MaximilianPeters 提到的,您可以使用flush 手动写入文件,但如果您确实需要,请使用它

标签: java filereader file-handling filewriter bufferedwriter


【解决方案1】:

这是你的答案,只需添加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();

【讨论】:

    猜你喜欢
    • 2017-05-04
    • 2014-02-04
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多