java read and write

1. Standard "read", "write"

br.readLine() will report an IOException error, just throws IOException directly. In the main function, you can also not write try...catch, similar to test, throws IOException.
public static void test(BufferedReader br,BufferedReader br2, PrintWriter writer) throws IOException {
        String line = null;
        String line2 = null;
        int js =0;
        Set<String> zTitle = new HashSet<String>();
        while ((line = br.readLine()) != null) {
            String array[] = line.split("\t", -1);
            zTitle.add(array[1]);
        }
        while((line2 = br2.readLine()) != null ) {
            String array2[] = line2.split("\t", -1);
            if (array2.length < 8) {
                continue;
            } else {
                String hTitle = array2[1];
                if (js == 1) {
                    for (String s : zTitle) {
                        if (hTitle.equals(s)) {
                            writer.println(line);
                        }
                    }
                } else {
                    writer.println(line);
                    js = 1  ;
                }
            }
        }
    }


    public static void main(String[] args) {

        String inPath = "";
        String midPath = "";
        String endPath = "";
        String encoding = "utf-8";
        BufferedReader br = null;
        BufferedReader br2 = null;
        PrintWriter writer = null;
        long startTime=System.currentTimeMillis(); //Get the start time
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(inPath), encoding));
            br2 = new BufferedReader(new InputStreamReader(new FileInputStream(midPath), encoding));
            writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(endPath), encoding));
            //call the test method
            test(br, br2, writer);
            long endTime=System.currentTimeMillis(); //Get the end time
            System.out.println("用时: "+(endTime-startTime)+"ms");

        } catch (FileNotFoundException e) {
            System.out.println("The file does not exit or the path is incorrect!");
            System.exit(-1);
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (br2 != null) {
                    br.close();
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }

2. Simpler "write"

            FileWriter writer;
            writer = new FileWriter("D:\\test.txt", true);
            BufferedWriter bw = new BufferedWriter(writer);
            bw.write(str2+"\n");
            bw.close();
            writer.close();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324759507&siteId=291194637