每100行将行写入文件

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

包含正确URL代码的行将其写入scope.getgrouped = function (arrlst) { var Names = []; //get Names angular.forEach(arrlst,function (value,key) { if (Names.indexOf(value.Name) === -1) Names.push(value.Name); }); var resultArr = [] angular.forEach(Names,function (Name) { //get record for the current Name var lstcurrent = []; lstcurrent.length = 0; angular.forEach(arrlst,function (row) { if (row.Name === Name) { lstcurrent.push(row); } }); if (lstcurrent.length === 1) resultArr.push(lstcurrent[0]); else { for (var i = 0; i < 4; i++) { if (lstcurrent.length > 0 && lstcurrent[0]["id"].length > 1) { resultArr.push(lstcurrent[0]); lstcurrent.splice(0,1); } if (lstcurrent.length === 1) { resultArr.push(lstcurrent[0]); break; } for (var j = 1; j < lstcurrent.length; j++) { if ((lstcurrent[0].Country.indexOf(lstcurrent[j].Country[0]) !== -1) && (lstcurrent[0].Unit.indexOf(lstcurrent[j].Unit[0]) !== -1) && i == 0) { if (lstcurrent[0].Department.indexOf(lstcurrent[j].Department[0]) === -1) lstcurrent[0].Department.push(lstcurrent[j].Department[0]); lstcurrent[0].id.push(lstcurrent[j].id[0]); lstcurrent.splice(j,1); j = j - 1; continue; } //Country + Department else if ((lstcurrent[0].Country.indexOf(lstcurrent[j].Country[0]) !== -1) && (lstcurrent[0].Department.indexOf(lstcurrent[j].Department[0]) !== -1) && i === 1) { if (lstcurrent[0].Unit.indexOf(lstcurrent[j].Unit[0]) === -1) lstcurrent[0].Unit.push(lstcurrent[j].Unit[0]); lstcurrent[0].id.push(lstcurrent[j].id[0]); lstcurrent.splice(j,1); j = j - 1; continue; } //Unit + Department else if ((lstcurrent[0].Unit.indexOf(lstcurrent[j].Unit[0]) !== -1) && (lstcurrent[0].Department.indexOf(lstcurrent[j].Department[0]) !== -1) && i === 2) { if (lstcurrent[0].Country.indexOf(lstcurrent[j].Country[0]) === -1) lstcurrent[0].Country.push(lstcurrent[j].Country[0]); lstcurrent[0].id.push(lstcurrent[j].id[0]); lstcurrent.splice(j,1); j = j - 1; continue; } else if (i > 2) { if (lstcurrent.length === 1) { resultArr.push(lstcurrent[0]); } else { resultArr.push(lstcurrent[j - 1]); lstcurrent.splice(j - 1,1); i = 0; break; } } } } } })}; ,包含错误URL代码的行将其写入true.txt

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

file.txt
iCMS 回答:每100行将行写入文件

这就是您的答案,只需添加同花顺

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();
本文链接:https://www.f2er.com/2297467.html

大家都在问